Counting pulses at 20 kHz?


I would like to read a flow sensor using an arduino diecimila. The sensor sends a known number of pulses for each liter that passes through it. The frequency of the signal can be up to 20 kHz at maximum flow, and i need to count the pulses in order to get the flowrate. So, how can you do this using an arduino?

Edit: Spelling Mistake


Hello. I'm not sure how to do this with Arduino libraries, but maybe I can give you a starting point by saying how you can do it with the mega168. If I were in your position, I would configure Timer/Counter1 (the 16-bit timer) to use the T1 pin as its clock source (see the mega168 datasheet). Every pulse on this pin will then increment the timer1 counter register TCNT1. You can check the value of TCNT1 at fixed intervals to get a measure of pulses/time, which you can in turn convert into a flow rate. The nice thing about this solution is that the pulse counting is done purely in hardware and hence doesn't hog processing cycles.

Another solution would be to interrupt on every incoming pulse and increment your own global pulse-count variable, but this would be much more processor-intensive, especially if you're dealing with high flow rates.

  • Ben

check the FrecuencyTimer2 library on the playground.

allows you to set timer2 at high rates like 40khz