4

I created a quick program that uses the GPIO library to create an interrupt with a callback that is initiated when a (pink) button is pressed. It works, but when the button is pressed and then released slightly late I get another callback. Why is this? The interrupt is configured to only occur on a rising edge so I thought it should only occur on the button press. Is this just button bounce that occurs when I release the button?

The circuit is simply a button placed between pin 16 and a ground pin.

The python I'm using is below:

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

pinkPin = 16

#set the pin to be high to start, low when pressed
GPIO.setup(pinkPin,   GPIO.IN,   pull_up_down=GPIO.PUD_UP)

def pinkCall(channel):
    print "Pink Callback"

def main():
    GPIO.add_event_detect(pinkPin,   GPIO.RISING, callback=pinkCall,   bouncetime=500)

    try:
        raw_input()
    except KeyboardInterrupt:
        GPIO.cleanup()

    GPIO.cleanup()

if __name__ == "__main__":
    main()
1
  • 1
    Can you check the button in an oscilloscope? It is quite frequent to have a "false" trigger when sampling fast enough due to internal construction of a button.
    – Marco Poli
    Commented Oct 26, 2013 at 15:54

3 Answers 3

3

Yes, it's contact bounce. But note that the RISING edge you're listening for is the button release, not the button press. So I guess when you do a slow push you're getting a "fake" spike when you press and then the real rising edge when you release.

One way to remove the false call is within your pinkCall method, wait a little bit (like 0.1 seconds) and then check the value of GPIO.input(pinkPin). If it's still high, then it was a "real" rising edge, and the button has really been released. If it's low, it was just a spike and can be ignored. Of course this has the disadvantage that you're not reacting immediately to the button release - if this is important then you can use a capacitor as Milliways suggested.

5

Any mechanical switch will have contact bounce.

You need to either handle this in hardware or software. The add_event_detect() allows for bounce on press, but if you hold the button down you will get bounce on release which will cause a multiple trigger.

The traditional solution is to use a latch (2 interconnected NOR gates) and a changeover (2 pole) switch. Depending on your application this may be warranted or you may be able to adopt a software solution to detect falling edge, or just use a capacitor to slow down the falling edge.

It is usually better to use a pullup resistor and button to ground, which you can combine with a capacitor to slow the rising edge without affecting the falling edge.

3

The hardware solution suggested by Milliways will work but there is also a software solution.

Within your callback, record the time of the call. Then you can compare current time with the time of the last call. If the current call is within, say, 100ms, of the previous then it must be contact bounce and you ignore it.

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.