smitthhyy
Posts: 4
Joined: Sun Apr 16, 2017 8:43 am

SOLVED: RuntimeError: Failed to add edge detection

Tue Feb 13, 2018 6:49 am

GPIO.add_event_detect(PIN, GPIO.BOTH, handle) is generating error: RuntimeError: Failed to add edge detection

Solution: This issue is because an unknown conflict using Pin7 (GPIO4) which appears to have a secondary function of GCLK. Moving it to Pin37 (GPIO26) which has no secondary functions allows it to work, after fixing an unrelated code issue (which is fixed in this code snippet, thanks Mark):

Code: Select all

#!/usr/bin/python3
# -*- coding: utf-8 -*-

import RPi.GPIO as GPIO
import time
import threading

PIN = 7
GPIO.setmode(GPIO.BOARD)
GPIO.setup(PIN, GPIO.IN)

def handle(channel):
    movement = GPIO.input(PIN)
    if movement:
        print("Movement")
    else:
        print(“No movement")

print ("Setting up event detect")
worked = False
while not worked:
    # keep trying to set up event detect based on suggestion in 
    # https://www.raspberrypi.org/forums/viewtopic.php?f=32&t=129015&p=874227#p874227
    worked = True
    try:
        GPIO.add_event_detect(PIN, GPIO.BOTH, handle)
    except RuntimeError:
        worked = False

print("We are running!")  # This never prints, never gets out of above while loop

while True:
        time.sleep(1e6)
Have tried with both python2 and python3.
Have run as root (sudo)
Have ensured gpio package is latest with “pip install RPi.GPIO –upgrade” and “pip3 install RPi.GPIO --upgrade”
Have tried re-install of raspberrypi-sys-mods

Device: RPI 2B v1.2
Image: 2017-11-29-raspbian-stretch-lite
GPIO library: rpi.gpio v0.6.3
Last edited by smitthhyy on Tue Feb 13, 2018 10:01 pm, edited 2 times in total.

MarkDH102
Posts: 265
Joined: Fri Feb 13, 2015 3:18 pm

Re: RuntimeError: Failed to add edge detection

Tue Feb 13, 2018 7:26 am

I think that you may need to run with "sudo"
i.e.
sudo python3 application.py

smitthhyy
Posts: 4
Joined: Sun Apr 16, 2017 8:43 am

Re: RuntimeError: Failed to add edge detection

Tue Feb 13, 2018 9:39 am

Thanks Mark, I've tried that too. There are a few threads out there that I worked through.

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

Re: RuntimeError: Failed to add edge detection

Tue Feb 13, 2018 10:13 am

Maybe there is a conflict with some other use of Pin 7 (gpio4)?

Are you physically constrained to using that pin?

Do you get the same result if you try a nearby alternative such as 11, 13 or 15?

(Invoking the script with sudo should not be necessary if using a recent version of the Raspbian Operating System.)

Also: just noticed that your call to add_event_detect() is inside a loop. Perhaps you can only set a given event up once?

MarkDH102
Posts: 265
Joined: Fri Feb 13, 2015 3:18 pm

Re: RuntimeError: Failed to add edge detection

Tue Feb 13, 2018 11:22 am

I've just taken your code and tried it on my Pi0W with the latest Stretch (NOT LITE) and the same version of GPIO and it worked...
On the first run though I got Type Errors regarding a missing parameter in the handler() function.
In my older code, I declared it as def handler(channel). I don't know if it's still needed.
But on subsequent runs, the error didn't occur.
And I didn't need SUDO. Must have been a Jessie thing...

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

Re: RuntimeError: Failed to add edge detection

Tue Feb 13, 2018 11:34 am

And I didn't need SUDO. Must have been a Jessie thing...

It was a Raspbian Wheezy 'thing'.

Sudo-less access to gpio was introduced with the release of Raspbian Jessie in September 2015.

Reference: http://downloads.raspberrypi.org/raspbi ... _notes.txt

MarkDH102
Posts: 265
Joined: Fri Feb 13, 2015 3:18 pm

Re: RuntimeError: Failed to add edge detection

Tue Feb 13, 2018 11:37 am

Check out this thread for a possible fix with Stretch Lite. This is just a guess mind you.
I remember having this problem with Jessie at some stage which is why my code got launched with SUDO.

viewtopic.php?t=183481

Fix:

Following https://github.com/raspberrypi/linux/is ... -292901658, my non-working system did not have raspberrypi-sys-mods installed, while my working systems did. Installing this, and confirming to overwrite '/etc/udev/rules.d/99-com.rules' if prompted, resolved the issue. Be sure to remove any existing GPIO exports, or simply reboot - or existing exports will still have the broken permissions.

smitthhyy
Posts: 4
Joined: Sun Apr 16, 2017 8:43 am

Re: RuntimeError: Failed to add edge detection

Tue Feb 13, 2018 9:49 pm

B.Goode wrote:
Tue Feb 13, 2018 10:13 am
Maybe there is a conflict with some other use of Pin 7 (gpio4)?

Are you physically constrained to using that pin?

Do you get the same result if you try a nearby alternative such as 11, 13 or 15?

(Invoking the script with sudo should not be necessary if using a recent version of the Raspbian Operating System.)

Also: just noticed that your call to add_event_detect() is inside a loop. Perhaps you can only set a given event up once?
I moved it to Pin37 and it worked. Thanks for the suggestion. Strange that it worked for Mark, I'm assuming he used Pin7.

Is there an easy way to find out what is conflcting? This time it's ok because have plenty spare pins, but if others come across this, they may be constrained and need to find out how to solve.

FYI: I did also try re-install the raspberry pi sys module with sudo apt update; sudo apt install --reinstall raspberrypi-sys-mods;sudo reboot but that didn't solve using Pin7.

Thanks for your help. :)

pcmanbob
Posts: 4128
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: SOLVED: RuntimeError: Failed to add edge detection

Tue Feb 13, 2018 11:07 pm

Physical pin 7 ( gpio4) is also used as the default pin for the 1-wire interface that may have been what caused your problem, especially if 1-wire interface was enabled.
Remember we want information.......................no information no help
The use of crystal balls & mind reading is not supported

Return to “Troubleshooting”

Who is online

Users browsing this forum: No registered users and 40 guests