I'm using RPi.GPIO to handle events from a simple pushbutton. Pushing the button connects the associated GPIO pin (4) to ground. I would like to trigger an event when the button is released (e.g., when the value read from the GPIO pin goes from 0 to 1).
With the following code:
>>> import RPi.GPIO as GPIO >>> GPIO.setmode(GPIO.BCM) >>> GPIO.setup(4, GPIO.IN, pull_up_down=GPIO.PUD_UP) >>> def cb(*args): ... print('callback was called') ... >>> GPIO.add_event_detect(4, edge=GPIO.RISING, callback=cb, bouncetime=200)
A single button push and release results in:
callback was called callback was called
I can avoid getting multiple events by cranking up the value of
bouncetime, but I'm still getting them when the button is pressed rather than when the button is released.
I guess this is expected behavior; I assume that the culprit is noise when the button is pushed so that a spurious logic change triggers the rising edge detection. I've tried to resolve this by adding logic to the callback to explicitly check the pin value...
def cb(pin): val = GPIO.input(pin) if val == 1: print('callback was called')
...but even with this logic in place I am still, albeit less frequently, triggering on button push instead of button release.
Is there anything else I can do to ensure that my code only reacts to the button release?