I want to calculate the number of pulses coming from motor1 encoder pin2 (Int0) as input , and motor 2 encoder pin3 (Int1), I am working now for the first motor (just for the first motor ) so I set timer 2 to count 50 ms then throw flag in this moment I have to send the number of pulses to my screen, but not succeeded cause i see on my screen zeros and ascii caracters but not the number of pulses.:this my code .

 volatile int  pulses = 0;

 int flag=0;
 int setbit = 0;
 int encoder_in = 2;
 void count() {


  void setup()

 pinMode(encoder_in, INPUT);
digitalWrite(2, HIGH); //Enable pullup

// initialize timer2
 noInterrupts();           // disable all interrupts

TCCR2A = 0x00;
 TCCR2B = 0x07;  
TCNT2 = 0x64;
//EIMSK = 0x03;     // Enable external interrupt INT0
//EICRA = 0x0f;    // Trigger INT0 on falling edge 

 TIMSK2 |= 0x01;  // enable timer compare interrupt
interrupts();             // enable all interrupts

TCNT2 = 0x64;            // preload timer


 //{ pulses++; }

 void loop()

 if (flag==1)


  • a) what is your question?, and b) what does "not succeed" mean? Does the print statement not work? Is the count it shows inaccurate? See stackoverflow.com/help/how-to-ask
    – Scott Seidman
    Aug 19, 2015 at 12:26
  • i see on my screen zeros and ascii caracters not number of pulses.
    – omran
    Aug 19, 2015 at 12:29
  • 1
    Separate out the problem. Just try to print ANY integer, and don't worry about counting the pulses until you have this right. Note that at first glance, your issue seems to have NOTHING to do with what you said it was in the title.
    – Scott Seidman
    Aug 19, 2015 at 14:46
  • Try changing volatile int pulses = 0; to volatile int pulses = 1; and see if you now get 1s printed. Also which ascii characters do you see? Can you give us a sample output.
    – Gerben
    Aug 19, 2015 at 19:49
  • Do you have anything attached to pins 0 or 1 of the Arduino?
    – Majenko
    Aug 19, 2015 at 21:45

2 Answers 2


Your technique looks convoluted to me. How about this?

volatile bool counting;
volatile unsigned long events;

unsigned long startTime;
const unsigned long INTERVAL = 50;  // ms

void eventISR ()
  if (counting)
  }  // end of eventISR

void setup ()
  Serial.begin (115200);
  Serial.println ();
  pinMode (2, INPUT_PULLUP);
  attachInterrupt (0, eventISR, RISING);
  }  // end of setup

void showResults ()
  Serial.print ("I counted ");
  Serial.println (events);
  }  // end of showResults

void loop ()
  if (counting)
    // is time up?
    if (millis () - startTime < INTERVAL)
    counting = false;
    showResults ();
    }  // end of if

  noInterrupts ();
  events = 0;
  startTime = millis ();
  EIFR = bit (INTF0);  // clear flag for interrupt 0
  counting = true;
  interrupts ();
  }  // end of loop


Using a timer to detect when 50 ms is up isn't particularly helpful in this case. You can't do printing inside an ISR, so you would have to set a flag that can be checked in the main loop.

If you are going to check a flag anyway, you may as well just check if the time is up, as my code does.

Inside the ISR, I stop adding to events if the flag counting is false. This is to stop events being changed at the exact moment it is being printed.

  • that's work thank you Mr Nick, but I tried to calculate for both Motor but it not Works , I Tried many Times but not works, may you can see my code:
    – omran
    Aug 20, 2015 at 7:54
  • whats the "Return" instruction in loop() exactly do. I am working to calculate for the second Motor but it not work.
    – omran
    Aug 20, 2015 at 9:06
  • may you can see my code - amend your question to add this new code so I can see it.
    – Nick Gammon
    Aug 20, 2015 at 9:23
  • whats the "Return" instruction in loop() exactly do - it returns from the loop() function, which then gets called again.
    – Nick Gammon
    Aug 20, 2015 at 9:23

Check the Baud Rate in Serial Monitor. If baud rate is not correct i.e. 9600 bps then it will show garbage.

Remove the Timer and Wire Libs

add this code in void setup()


the above is for the the external interrupt input.

and attach your sensor/incoming pulse wire on Pin 2 on arduino.

make a new function like this

  • 1
    I agree with this. But it is more appropriated as a comment. Aug 19, 2015 at 12:40
  • New to stack exchange.
    – ammar.cma
    Aug 19, 2015 at 12:43
  • Also he already enabled the Interrupt, but by registers Aug 19, 2015 at 12:44
  • i did this change before and now too i did what you told me, but I still see characters like {0¤0, A,P,. . }
    – omran
    Aug 19, 2015 at 13:16
  • I see the pulses on oscilloscope but still I cant see the number of pulses on my monitr
    – omran
    Aug 19, 2015 at 14:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.