satish.gunjal
Posts: 42
Joined: Sat May 23, 2015 11:50 am

button control using gpiozero

Mon Dec 14, 2015 7:45 pm

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

-rst-
Posts: 1316
Joined: Thu Nov 01, 2012 12:12 pm
Location: Dublin, Ireland

Re: button control using gpiozero

Tue Dec 15, 2015 5:34 pm

http://raspberrycompote.blogspot.com/ - Low-level graphics and 'Coding Gold Dust'

User avatar
ben_nuttall
Raspberry Pi Foundation Employee & Forum Moderator
Raspberry Pi Foundation Employee & Forum Moderator
Posts: 231
Joined: Sun Aug 19, 2012 11:19 am
Location: Cambridge, UK
Contact: Website

Re: button control using gpiozero

Sat Jan 02, 2016 4:43 pm

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.
Community Manager - Raspberry Pi Foundation
Author of GPIO Zero and creator of piwheels

satish.gunjal
Posts: 42
Joined: Sat May 23, 2015 11:50 am

Re: button control using gpiozero

Sun Jan 03, 2016 5:25 am

thanks :)

Return to “Python”