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.

  • 1
    Not really Pi specific. You need to read the pin and only increment the count when it changes state from low to high, i.e. if the last read was high don't bump the count in the new read.
    – joan
    Jul 9 '19 at 17:09
  • would you know of an example of how to set that up
    – uspc1
    Jul 9 '19 at 17:25

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


pi = pigpio.pi()

if not pi.connected:

last_state = pi.read(GPIO)

while True:
   now = pi.read(GPIO)
   if now != last_state:
      last_state = now
      if now == 1:
         count += 1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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