#Counting pulses
1 messages · Page 1 of 1 (latest)
@rare halo The difference with pulse counting vs the button press is that with that button press, you're doing something while the button is pressed, but with counting, you want to detect the start of a press (or pulse).
You want to detect a transition from high to low logic level (or vice versa), and only count those transitions.
A built-in module (on most boards) that does that is countio. http://docs.circuitpython.org/en/7.1.x/shared-bindings/countio/index.html
In 7.x a countio.Count object will count falling edges (high to low).
You're presumably interested in pulse count per time, say per second (I don't know what the pulse rate of what flow sensor would be.)
So the code might look something like
import countio
import time
import board
sensor_pin = board.D10 # say
counter = countio.Counter(sensor_pin)
counter.reset() # zero it out
while True:
count = counter.count()
report(count) # do something with it
counter.reset()
time.sleep(1)
Counting pulses