Go Down

Topic: Can pulseIn() function be applied to HW INT pin (Read 780 times) previous topic - next topic


Hello all,
I was wondering if it is possible to use the pulseIn() function to measure the time delay between received interrupts.

I  can use the function in my main loop if I route the interrupt to an ordinary input as well (which means I have a workaround) but not when routing to the interrupt only.  

Many thanks in advance.



Mar 09, 2010, 05:44 pm Last Edit: Mar 09, 2010, 05:55 pm by programmer Reason: 1
Usually to do this you can read the counter direct, to get a measurement of the time between pulses. You read the counter value, store it, and then read it again on the next interrupt, and you can then work out the interval, before resetting the counter or storing the counter value. You need to make sure you check the overflow flag to see if the counter has overflowed (and consequently started from the beginning again) during  the period whilst waiting for the interrupt. Also make sure you have a sensible prescaler value, that matches the period of the interval that you want to measure.

Have a look through the ATMega datasheet, and it explains the counter registers and how they operate. It's not the easiest thing to get your head around, but very powerful when you do.


Thanks for the feedback. I have found a few examples online but I need to spend the time to understand more about the inner workings.

I will post again with example code once I make progress (I am new to this so it may be a few days!)


Hi again,

Just a quick call out for clarification.

If I understand correctly, using the Timer/Counter1 register (TCNT1) to calculate the delay between pulses (interrupts) requires the external interrupt to be routed to the Input Capture Pin (ICP1) - the normal Arduino interrupts (INT0, INT1) cannot be used for this purpose.

I like the idea of using the Arduino interrupts and the workaround using pulseIn() with the interrupt routed to a free IO pin - it's a simple workaround requiring an extra jumper wire.

Am I taking the easy way out here or should I be challenging myself more???

Go Up