Menu

#77 GPIO.RISING triggers twice if other GPIO pin is busy

Verified
None
Low
Defect
2014-10-15
2014-09-30
No

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)

Discussion

  • James Sutherland

    Sorry, forgot to confirm version.

    import RPi.GPIO as GPIO
    GPIO.VERSION
    '0.5.7'

     
  • James Sutherland

    Better formatted code. Apparently the line of ~'s method doesn't work here.

    #!/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)
    
     
  • jg1212

    jg1212 - 2014-10-03

    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
  • James Sutherland

    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.

     
  • Ben Croston

    Ben Croston - 2014-10-04

    I eventually managed to reproduce the double callback on a rev 1 model B using the following code:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    #!/usr/bin/python
    import RPi.GPIO as GPIO
    import time
    
    PIN_IN = 23
    
    GPIO.setmode(GPIO.BCM)
    GPIO.setup(PIN_IN, 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)
        time.sleep(3)
        print('Finished callback')
    
    GPIO.add_event_detect(PIN_IN, GPIO.FALLING, callback=printFunction, bouncetime=300)
    
    try:
        while True:
            time.sleep(10)
    except KeyboardInterrupt:
        pass
    GPIO.cleanup()
    
    • I was getting the double callback when touching the end of a loose wire connected to the GPIO
    • I could not reproduce when using a properly wired switch on a breadboard
    • I could not reproduce when the 3 second delay was removed from the callback
    • Note that my code does not use another GPIO in the callback, eliminating your 'GPIO in callback' theory

    How is your circuit physically constructed? My tests suggest that this is more likely the problem than the RPi.GPIO module.

     
  • Ben Croston

    Ben Croston - 2014-10-04
    • status: New --> Verified
    • assigned_to: Ben Croston
     
  • James Sutherland

    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.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.