Page 1 of 1

button control using gpiozero

Posted: Mon Dec 14, 2015 7:45 pm
by satish.gunjal
Hi,

Code: Select all

from gpiozero import LED, Button
from signal import pause

led = LED(2)
button = Button(27)

button.when_pressed = led.on
button.when_released = led.off

pause()
Every time I run above code, for first attempt I get below error and second attempt it works fine!!

Traceback (most recent call last):
File "test.py", line 5, in <module>
button = Button(27)
File "/usr/lib/python2.7/dist-packages/gpiozero/devices.py", line 51, in __call__
result = super(GPIOFixedAttrs, cls).__call__(*args, **kwargs)
File "/usr/lib/python2.7/dist-packages/gpiozero/input_devices.py", line 405, in __init__
super(Button, self).__init__(pin, pull_up, bounce_time)
File "/usr/lib/python2.7/dist-packages/gpiozero/input_devices.py", line 262, in __init__
bouncetime=-666 if bounce_time is None else int(bounce_time * 1000)
RuntimeError: Failed to add edge detection

Re: button control using gpiozero

Posted: Tue Dec 15, 2015 5:34 pm
by -rst-

Re: button control using gpiozero

Posted: Sat Jan 02, 2016 4:43 pm
by ben_nuttall
Unfortunately this is a known bug that resides in RPI.GPIO. See https://github.com/RPi-Distro/python-gpiozero/issues/50

When creating an input device object like Button, it does some setup including edge detection, and we've found that tends to fail on the first attempt (annoyingly), but not the second (luckily).

For example, in the Python shell, try:

>>> from gpiozero import Button
>>> btn = Button(4)
[ you'll probably see the same error you got here - but try again]
>>> btn = Button(4)

You'll probably find it works second time.

Dave Jones (waveform) and Ben Croston are still working on finding the bug in RPI.GPIO and fixing it.

For now, here's some hacky code which will get around the issue:

Code: Select all

from gpiozero import Button

btn = None

while not btn:
    try:
        btn = Button(4)
    except RuntimeError:
        pass
This will basically just keep trying to create the button until it works. As I say, it usually works second time so you shouldn't have to worry about this taking ages to work.

Re: button control using gpiozero

Posted: Sun Jan 03, 2016 5:25 am
by satish.gunjal
thanks :)