Looking for a simple input pulse counter for no more then 2000 counts per minute. Right now I have it set
if gpio.input(countswitch): data["count"] += 1
which works but I get 100 plus counts for each pulse I only want one count per pulse.
Raspberry Pi Stack Exchange is a question and answer site for users and developers of hardware and software for Raspberry Pi. It only takes a minute to sign up.Sign up to join this community
Here is a pigpio Python example. You use the same logic in any programming language.
In practice you would use GPIO callbacks on level change rather than polling.
The time.sleep(0.01) is kind to other programs which want to run.
#!/usr/bin/env python import time import pigpio GPIO=4 pi = pigpio.pi() if not pi.connected: exit() count=0 last_state = pi.read(GPIO) while True: now = pi.read(GPIO) if now != last_state: last_state = now if now == 1: count += 1 print(count) time.sleep(0.01)