1

With Raspberry Pi, if you have

import RPi.GPIO as GPIO  
GPIO.setmode(GPIO.BCM)  

GPIO.setup(20, GPIO.IN, pull_up_down=GPIO.PUD_UP)  
GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP)  

def my_callback(channel):  
    print "falling edge detected on ", channel  

GPIO.add_event_detect(17, GPIO.FALLING, callback=my_callback, bouncetime=300)  
GPIO.add_event_detect(20, GPIO.FALLING, callback=my_callback, bouncetime=300)  

you get the usual callback called when pin 17 falls or when pin 20 falls The callback function prints which channel was called.

My question is, what happens if both pins falls at the same time?

Unfortunately I don't have a Pi to try it right now and even if I did, the human error in -say- pressing one pin attached button before another also counts. What happens when it is done at the exact same time?

2
  • This is known as a "race condition" in programming. You can find more information searching on that term. Basically, the result is unpredictable and may change from event to event. You need to account for that in your software if the order matters to you.
    – Brick
    Sep 30 '17 at 18:33
  • Aren't the multiple callbacks simply queued? Each gets called, although the exact sequencing might vary. That's always been my understanding, and @CoderMike's test below seems to confirm.
    – bobstro
    Sep 30 '17 at 19:02
2

Both events fire in quick succession. I wired both GPIO 17 and GPIO 20 to the same button and got the following :

falling edge detected on 20
falling edge detected on 17

Edit : code added

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)  

GPIO.setup(20, GPIO.IN, pull_up_down=GPIO.PUD_UP)  
GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP)  

def my_callback(channel):  
    print("falling edge detected on ", channel) 

GPIO.add_event_detect(17, GPIO.FALLING, callback=my_callback, bouncetime=300)  
GPIO.add_event_detect(20, GPIO.FALLING, callback=my_callback, bouncetime=300)

while True:
    time.sleep(0.1)
    print(".")
3
  • I'm not sure why this was downvoted without any commentary. Based on my admittedly not deep understanding of callbacks, they are queued. If 2 are received simultaneously, they both go into the queue in a more-or-less random order, but the callback for each is still made.
    – bobstro
    Sep 30 '17 at 18:37
  • could you share your code so skeptics can confirm independently? I think you've got it right.
    – bobstro
    Sep 30 '17 at 19:10
  • Possible reason for down-vote: The answer implies a deterministic order by only giving one result. Agree that the result should be essentially random in a real use case. Wiring to a single button may or may not test that in a more general setting because it avoids the race condition on the button push and detection of the button push. @bobstro
    – Brick
    Oct 1 '17 at 13:47

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.