bdilloughery
Posts: 5
Joined: Fri Nov 02, 2018 4:55 am

[SOLVED] gpiozero Button - Runtime Error - edge detection

Fri Nov 02, 2018 5:03 am

Read bcroston's post on this github issue for the fix that worked for me:
https://github.com/RPi-Distro/python-gp ... -435620598

==============================================================================

So I'm a teacher, new to circuitry and Raspberry Pi's. Today was literally day one in my class. LED's went well, Python went well ... button's completely failed.

I have 33 new Raspberry Pi 3B+ that came in starter kits from Canakit with Noobs pre-installed on 16GB SD cards. I ran the noobs install, even had it update Raspian via the auto-update over WiFi at the end of the last boot-up process.

Again, Raspberry Pi's work. LED's worked. My students wrote some Python code and have GPIO pin 17 turning LED's on and off. They had multiple separate GPIO pins turning multiple LED's on and off using code (similar to traffic lights).

Then, we tried buttons ... and not a single raspberry pi could do it. The buttons physically work. When we just connect it as part of the LED circuit, pressing the button makes the LED go on, and releasing the button makes the LED go off.

However, running code with gpiozero never once worked on a single one.

Tried to run the simple code of essentially:

from gpiozero import Button

button = Button(2)

button.wait_for_press()
print("Button was pressed")

... but everytime we got the same Runtime Error of something like "failed ... edge detection"

I spent all day on Google, and had my advanced students and TA trying all sorts of sudo commands on a test case Raspberry Pi ... but all the posts were a few years old and none fixed the issue. Tried a lot of different ports, but all had the same error.

Please help!
Last edited by bdilloughery on Mon Nov 05, 2018 11:51 pm, edited 1 time in total.

User avatar
neilgl
Posts: 1573
Joined: Sun Jan 26, 2014 8:36 pm
Location: Near Aston Martin factory

Re: gpiozero Button - Runtime Error - edge detection

Sat Nov 03, 2018 12:41 pm

I tested it on a pi 3B+ with latest raspbian (not via noobs) and updates. It did not work on button 2 but it did work on button 16
(I did not get a runtime error with button 2, just no response)
As I workaround you could try using button 16 and move the "blue" button wire to GPIO 16 (physical pin 36)

Code: Select all

from gpiozero import Button

button = Button(16)

button.wait_for_press()
print("Button was pressed")
I repeated the test on a pi 3B - same result - did not work on button 2 but did on 16
Anyone got any better ideas as the button example on gpiozero.readthedocs.io does not seem to be working out of the box?

Let us know how it goes

User avatar
B.Goode
Posts: 9558
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: gpiozero Button - Runtime Error - edge detection

Sat Nov 03, 2018 1:54 pm

Maybe this recent post from the developer/maintainer of RPi.GPIO - which is the default library used by Gpiozero - is relevant?

viewtopic.php?f=28&t=226002&p=1386867#p1386867

If that pasted link does not jump correctly you want the post from user @croston on 1st November.

User avatar
rpiMike
Posts: 1088
Joined: Fri Aug 10, 2012 12:38 pm
Location: Cumbria, UK

Re: gpiozero Button - Runtime Error - edge detection

Sat Nov 03, 2018 1:57 pm

Make sure your using the latest libraries, I believe gpiozero wraps RPi.GPIO:

Code: Select all

from pkg_resources import require
import RPi.GPIO as GPIO

print('RPi.GPIO:',GPIO.VERSION)
print('gpiozero:',require('gpiozero')[0].version)
RPi.GPIO: 0.6.4
gpiozero: 1.4.1

Are you running the python code within a GUI or from a terminal window ? I have seen the 'failed to add edge detection' when running code within 'Thonny' (maybe threads left running?). Rebooting and running from a terminal window eg 'python3 test.py' worked fine.

User avatar
bensimmo
Posts: 4296
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: gpiozero Button - Runtime Error - edge detection

Sat Nov 03, 2018 2:11 pm

The error is reported on GitHub for gpiozero it seems
https://github.com/RPi-Distro/python-gp ... issues/687
I assume that's you too?

Try switching to pigpio as the pin factory.
https://gpiozero.readthedocs.io/en/stab ... _gpio.html
I've used that in the past and it's also something excellent to learn as you can control other Pi.
A quick intro is here
https://www.raspberrypi.org/magpi/remot ... gpio-zero/

User avatar
croston
Posts: 705
Joined: Sat Nov 26, 2011 12:33 pm
Location: Blackpool
Contact: Website

Re: gpiozero Button - Runtime Error - edge detection

Sat Nov 03, 2018 3:58 pm

I've been trying all week to reproduce this problem - no luck so far.

User avatar
rpiMike
Posts: 1088
Joined: Fri Aug 10, 2012 12:38 pm
Location: Cumbria, UK

Re: gpiozero Button - Runtime Error - edge detection

Sat Nov 03, 2018 4:28 pm

The following runs fine on my Pi3B from a terminal window:

Code: Select all

import RPi.GPIO as GPIO
import time

def my_callback():
    print('my_callback')

GPIO.setmode(GPIO.BOARD)
GPIO.setup(40, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) 

GPIO.add_event_detect(40, GPIO.BOTH, callback=my_callback, bouncetime=10)

while True:
    time.sleep(0.01)

and will run on the first attempt within Thonny, however on the second attempt:

>>> %Run test222.py
Traceback (most recent call last):
File "/home/pi/test222.py", line 13, in <module>
time.sleep(0.01)
KeyboardInterrupt: Execution interrupted
>>> %Run test222.py
Traceback (most recent call last):
File "/home/pi/test222.py", line 10, in <module>
GPIO.add_event_detect(40, GPIO.BOTH, callback=my_callback, bouncetime=10)
RuntimeError: Failed to add edge detection
>>>

User avatar
croston
Posts: 705
Joined: Sat Nov 26, 2011 12:33 pm
Location: Blackpool
Contact: Website

Re: gpiozero Button - Runtime Error - edge detection

Sat Nov 03, 2018 5:10 pm

Ive managed to reproduce the problem with Thonny now. How are you stopping your code from running?

User avatar
rpiMike
Posts: 1088
Joined: Fri Aug 10, 2012 12:38 pm
Location: Cumbria, UK

Re: gpiozero Button - Runtime Error - edge detection

Sat Nov 03, 2018 5:16 pm

I'm clicking the stop (interrupt/reset) button in Thonny.

Idahowalker
Posts: 445
Joined: Wed Jan 03, 2018 5:43 pm

Re: gpiozero Button - Runtime Error - edge detection

Sat Nov 03, 2018 5:34 pm

I am able to reproduce the issue of GPIO edge detection with Thonny. I noticed after starting Thonny that if the mouse cursor, when moved to certain positions, displays the arrow and hour glass, and I try to start my program, I will get the edge detection error. After the first edge detection error is created, shutting down Thonny and restarting Thonny will only give me more of the same error. I can find nothing in the task manager of before/after Thonny edge detection error that shows something is still running. The sure way to recover is to shut down the RPi, pull the power plug, reinsert the power plug, and restart Thonny, after the system has rebooted.

I now make sure that, by moving the cursor around to several locations in the Thonny window, that the pointer/hour glass is gone before starting the program.

A reboot is not 100 percent effective in ridding this issue from the system only a power off/on sequence is 100% effective.

This problem only showed up after my last system update.

I use GPIO.BCM.

I have multiprocessing running, thus the use of Thonny, and 8 threads, event triggers, multiple classes, threading and multiprocessing queues. I use I2C, I use a Rx/Tx connection to a Arduino DUE plus direct GPIO RPi:Due connections.

Any other info, you seek to resolving this matter and I have, I'll be glad to help.
Without knowing why you are deleting my postings, I will not know how...

User avatar
croston
Posts: 705
Joined: Sat Nov 26, 2011 12:33 pm
Location: Blackpool
Contact: Website

Re: gpiozero Button - Runtime Error - edge detection

Sat Nov 03, 2018 9:13 pm

I think I've fixed it in the latest development RPi.GPIO. It turns out that the GPIOs weren't getting cleaned up properly under Thonny and a recent bugfix to RPi.GPIO unmasked the problem.

I will release RPi.GPIO 0.6.5 in the next few days. If you can't wait that long then install the latest development RPi.GPIO (in Raspbian Squeeze):

Code: Select all

$ sudo apt-get install python-dev python3-dev
$ sudo apt-get install mercurial
$ sudo apt-get install python-pip python3-pip
$ sudo apt-get remove python-rpi.gpio python3-rpi.gpio
$ sudo pip install hg+http://hg.code.sf.net/p/raspberry-gpio-python/code#egg=RPi.GPIO
$ sudo pip3 install hg+http://hg.code.sf.net/p/raspberry-gpio-python/code#egg=RPi.GPIO
To revert back to the default version in Raspbian:

Code: Select all

$ sudo pip uninstall RPi.GPIO
$ sudo pip3 uninstall RPi.GPIO
$ sudo apt-get install python-rpi.gpio python3-rpi.gpio

User avatar
neilgl
Posts: 1573
Joined: Sun Jan 26, 2014 8:36 pm
Location: Near Aston Martin factory

Re: gpiozero Button - Runtime Error - edge detection

Sat Nov 03, 2018 10:12 pm

Thanks for the fix - I will give it a try later, though I never use Thonny.

User avatar
neilgl
Posts: 1573
Joined: Sun Jan 26, 2014 8:36 pm
Location: Near Aston Martin factory

Re: gpiozero Button - Runtime Error - edge detection

Sun Nov 04, 2018 12:56 pm

Installed the updated development RPi.GPIO as per instructions.
Using a simple pushbutton (no external resistors) on Button 2, it works OK.

The GPIO.VERSION now shown is 0.6.4 - should it be that?

User avatar
croston
Posts: 705
Joined: Sat Nov 26, 2011 12:33 pm
Location: Blackpool
Contact: Website

Re: gpiozero Button - Runtime Error - edge detection

Sun Nov 04, 2018 1:15 pm

neilgl wrote:
Sun Nov 04, 2018 12:56 pm
The GPIO.VERSION now shown is 0.6.4 - should it be that?
That's what it will report on the development version because I haven't changed it to report anything else yet.

bdilloughery
Posts: 5
Joined: Fri Nov 02, 2018 4:55 am

Re: gpiozero Button - Runtime Error - edge detection

Mon Nov 05, 2018 11:55 pm

Thank you all for the support.

The github fix posted by bcroston fixed the issue for me.
https://github.com/RPi-Distro/python-gp ... -435620598

For the record though, I was using Python3 IDLE, not Thonny.

It would work the first time after a system reboot, but then not again after that. I was often using Ctrl-C to stop the code if it had something still going (like LED.blink(2)). Otherwise I was just compiling a new program when the last one ended.

I'm not going to pretend I understand all of the backend stuff, but my classroom volunteer was tinkering a lot and seems like the issue was that the pins were getting cleared/cleaned up after the first usage.

Thank you for the fix!!!!

User avatar
bensimmo
Posts: 4296
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: [SOLVED] gpiozero Button - Runtime Error - edge detection

Tue Nov 06, 2018 8:01 am

bcroston (croston in this thread) created the problem in the first place ;-)
Only because he's the programmer of RPi.gpio python module
My understanding is some update to fix or improve some code then means another bug surfaced.
croston, is that because you tinkering and improving RPi.gpio python module?

bennuttal is one of the main developers of gpiozero.

:-)
Just to out some context (hopefully correct) to this thread of why github and bug reporting works well.
Though croston used sourceforge https://sourceforge.net/projects/raspberry-gpio-python/

mwsnelso
Posts: 2
Joined: Thu Nov 08, 2018 2:11 pm

Re: [SOLVED] gpiozero Button - Runtime Error - edge detection

Thu Nov 08, 2018 2:18 pm

Has this problem been resolved such that the work-around (using Development gpiozero) is no longer necessary? If not, can we get an estimated date as to when it will be?

mwsnelso
Posts: 2
Joined: Thu Nov 08, 2018 2:11 pm

Re: [SOLVED] gpiozero Button - Runtime Error - edge detection

Thu Nov 08, 2018 7:02 pm

I should have said: When will the fix for the RPi.GPIO (not gpiozero - sorry!) problem be in the official release? We use apt-get (not pip) for the Raspberry Pi 3B+'s in our coding club for kids. I just tested it, and as expected, the Button problem still exists in the official release.

francog
Posts: 1
Joined: Tue Nov 13, 2018 2:54 am

Re: [SOLVED] gpiozero Button - Runtime Error - edge detection

Fri Nov 16, 2018 11:19 pm

The issue has been resolved on my end. Many thanks to croston for fixing problem. :)

User avatar
neilgl
Posts: 1573
Joined: Sun Jan 26, 2014 8:36 pm
Location: Near Aston Martin factory

Re: [SOLVED] gpiozero Button - Runtime Error - edge detection

Tue Nov 27, 2018 9:51 am

Maybe some other issue but I am getting the "Failed to add edge detecion" error with a fully updated pizeroW using pythton 2.7 and RPi.GPIO (0.6.5) when I run the script on reboot from crontab -e
However, if I add a short sleep into the script it works OK, so it looks like some timing issue at startup?

Return to “Troubleshooting”