I am trying to count number of edges detected in a minute. My code:

global future_time
future_time = (take_present_time()+pd.Timedelta('1m')).strftime('%Y-%m-%d %H:%M:%S:%f')        
global present_time
present_time = take_present_time().strftime('%Y-%m-%d %H:%M:%S:%f')
pulse_counter = 0
while present_time < future_time:#True:
    present_time = take_present_time().strftime('%Y-%m-%d %H:%M:%S:%f')
    # Count the pulses occuring within 1 minute
    print("Lets detect falling edge")
    if GPIO.event_detected(read_pin):
       pulse_counter +=1

Present output:

False # Meaning, no edge detected

There is an edge and it is changing. But I don't know why it is not getting detected by the GPIO.event_detected. How I know there is actually an edge? I tried same GPIO.add_event_detect(read_pin,GPIO.FALLING, callback=do_this_when_edge_detected), it basically redirects this to a callback function, where I just count the pulses. The problem with this approach is, it never exits the add_event_detect function and it remains forever in that loop only. Hence, I am trying above approach, by which, I can count the pulses per minute.

Based on the @Joan's answer:

enter image description here

1 Answer 1


This task might be simpler and more reliable if you use pigpio.

import time
import pigpio


pi = pigpio.pi()

if not pi.connected:

cb = pi.callback(COUNT_GPIO)

while True:

   print(cb.tally()) # edges detected
   cb.reset_tally()  # reset count


Note that the pigpio daemon must be running (sudo pigpiod) and pigpio uses Broadcom GPIO numbers.

  • I ran it but I am getting zeros only. Please, find my updated question.
    – Mainland
    Sep 12, 2023 at 14:06
  • Most likely is a mismatch between the software's idea of the GPIO you are using and yours. Check with pinout.xyz
    – joan
    Sep 12, 2023 at 16:37
  • Why have you removed the sleep? You need to let it sleep for at least as long as one edge will take.
    – joan
    Sep 12, 2023 at 16:47
  • Sorry. My signal was changing so fast and I was just checking to see if it catches these. In all cases, with and without sleep(60), it was just giving 0. On the other hand, my other code was incrementing at each edge detection.
    – Mainland
    Sep 13, 2023 at 1:48
  • Now I updated my question with sleep(60) response.
    – Mainland
    Sep 13, 2023 at 2:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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