GPIO.RISING triggers twice if other GPIO pin is busy
A Python module to control the GPIO on a Raspberry Pi
Brought to you by:
croston
GPIO.add_event_detect triggers twice but only if the function called works with another GPIO. Just finished tested this on two seperate pi B+ units, one with nothing connected to it except a wire to serve as a momentary switch.
Doesn't seem to be a bounce issue based on timing of when the second press happens. Also bouncetime changes have no effect on behavior. GPIO.PUD_DOWN is enabled but just to be safe have also tried a 10k resistor between GPIO26 and ground.
Code to reproduce (while code is running toggle momentary switch on GPIO 21):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | #!/usr/bin/python import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BCM) GPIO.setup(21, GPIO.IN, pull_up_down = GPIO.PUD_DOWN) GPIO.setup(26, GPIO.OUT) def printFunction(channel): print("Button 1 pressed!") localtime = time.asctime( time.localtime(time.time()) ) print "Local current time :", localtime GPIO.output(26, True) time.sleep(1) GPIO.output(26, False) time.sleep(1) GPIO.output(26, True) time.sleep(1) GPIO.output(26, False) GPIO.add_event_detect(21, GPIO.RISING, callback=printFunction, bouncetime=300) while True: time.sleep(10) |
This code below will not reproduce the problem. Notice the only difference is that GPIO 26 is not being toggled True False.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | #!/usr/bin/python import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BCM) GPIO.setup(21, GPIO.IN, pull_up_down = GPIO.PUD_DOWN) def printFunction(channel): print("Button 1 pressed!") localtime = time.asctime( time.localtime(time.time()) ) print "Local current time :", localtime GPIO.add_event_detect(21, GPIO.RISING, callback=printFunction, bouncetime=300) while True: time.sleep(10) |
Sorry, forgot to confirm version.
Better formatted code. Apparently the line of ~'s method doesn't work here.
#!/usr/bin/python
I am having this same issue. GPIO.RISING gets called twice every time. I'm not using any GPIO.output in my function. Just a download and and email function call, which takes at least a few seconds to run. It seems to be related to the amount of time that a function takes to complete. I can not try right now, but I believe it would still get called twice if you replaced your GPIO.output lines with print lines, keeping the time.sleep lines.
Any help would be appreciated.
Thanks.
Last edit: jg1212 2014-10-03
I did try this with a time.sleep(10) in place of the GPIO pin and I was unable to reproduce the issue that way. Might be interesting to try to reproduce with this a print loop or something else that makes the system real busy.
I eventually managed to reproduce the double callback on a rev 1 model B using the following code:
How is your circuit physically constructed? My tests suggest that this is more likely the problem than the RPi.GPIO module.
I tested this with a bare wire as earlier described and also with an Adafruit touch sensor switch. http://www.adafruit.com/product/1374
I'll see if I can reproduce it with a normal momentary switch.
Is there any difference in your testing between GPIO.FALLING GPIO.RAISING and GPIO.BOTH? I tried Raising and Both, I noticed you used Falling in your test.