We use some essential cookies to make our website work.

We use optional cookies, as detailed in our cookie policy, to remember your settings and understand how you use our website.

16 posts • Page 1 of 1
chazwilmot
Posts: 1
Joined: Sun Mar 10, 2024 7:10 pm

RuntimeError: Failed to add edge detection

Sun Mar 10, 2024 7:23 pm

I tried to follow previous Troubleshooting forms and their solutions did not work for me

I am using the Raspberry Pi Zero 2 W and the RFM69HCW Radio @ 915Mhz

I am following the hookup guide and example code from here: https://rpi-rfm69.readthedocs.io/en/latest/hookup.html

I got the RuntimeError: Failed to add edge detection on interrupt pin 18 and 37 (Physical Pin Number)

I have tried sudo apt-get update, upgrade, install, reboot.

Here is a image of my wiring as well (OR not, the file is too large @ 765 KB):


Code: Select all

from flask import Flask, jsonify
from RFM69 import Radio, FREQ_915MHZ
import RPi.GPIO as GPIO
import threading
import time
app = Flask(__name__)
# pylint: disable=missing-function-docstring,unused-import,redefined-outer-name
node_id = 1
network_id = 100
recipient_id = 2
# We'll run this function in a separate thread
def receiveFunction(radio):
 while True:
 # This call will block until a packet is received
 packet = radio.get_packet()
 print("Got a packet: ", end="")
 # Process packet
 print(packet)
def init_radio():
 with Radio(FREQ_915MHZ, node_id, network_id, isHighPower=False, verbose=False,
 interruptPin=37, resetPin=29, spiDevice=0) as radio:
 print ("Starting loop...")
 # Create a thread to run receiveFunction in the background and start it
 receiveThread = threading.Thread(target = receiveFunction, args=(radio,))
 receiveThread.start()
 while True:
 # After 5 seconds send a message
 time.sleep(5)
 print ("Sending")
 if radio.send(recipient_id, "TEST", attempts=3, waitTime=100):
 print ("Acknowledgement received")
 else:
 print ("No Acknowledgement")
@app.route('/')
def index():
 return "Welcome to the Ground Station!"
@app.route('/data', methods=['GET'])
def get_data():
 global received_packet
 if received_packet:
 return jsonify({"data": received_packet})
 else:
 return jsonify({"data": "No data received yet"})
if __name__ == '__main__':
 threading.Thread(target=init_radio).start()
 app.run(host='0.0.0.0', port=8081, debug=True)
 

embeddedbarsha
Posts: 255
Joined: Mon Jun 26, 2023 8:22 am

Re: RuntimeError: Failed to add edge detection

Mon Mar 11, 2024 8:19 am

Looks like there is a GPIO conflict. The error you're encountering suggests a conflict with GPIO pins 18 and 37. These pins might already be in use by another process or component.
https://playplay.com/video-editor

fabrii4
Posts: 1
Joined: Mon Mar 11, 2024 4:22 pm

Re: RuntimeError: Failed to add edge detection

Mon Mar 11, 2024 5:44 pm

I think there is a conflict between the latest linux image update (linux-image-6.6.20) and RPi.GPIO.

I had the same problem today: before updating the system, the function add_event_detect was working fine, after updating I got the error "RuntimeError: Failed to add edge detection".

For me, the faster/easier solution was to re-flash the sd card and avoid the linux image updates with sudo apt-mark hold:

Code: Select all

sudo apt-mark hold linux-headers-rpi-v6
sudo apt-mark hold linux-headers-rpi-v7
sudo apt-mark hold linux-headers-rpi-v7l
sudo apt-mark hold linux-image-rpi-v6
sudo apt-mark hold linux-image-rpi-v7
sudo apt-mark hold linux-image-rpi-v7l
sudo apt-mark hold linux-image-rpi-v8

fawwal
Posts: 3
Joined: Sun Mar 24, 2024 4:15 am

Re: RuntimeError: Failed to add edge detection

Sun Mar 24, 2024 4:29 am

Sheesh, I'm having the same issues. What should someone at the edge of their understanding do in the meantime?

Code: Select all

 GPIO.cleanup()
Traceback (most recent call last):
 File "/home/ianmontgomery/Documents/testCutter/testcutter.py", line 39, in <module>
 GPIO.add_event_detect(encoder_pin, GPIO.RISING, callback=encoder_callback)
RuntimeError: Failed to add edge detection
I tried uninstalling RPi-GPIO and installing RPi-lgpio in my venv, but I'm getting this :shock:

Code: Select all

(testCutter) ianmontgomery@testCutter:~/Documents/testCutter $ python testcutter.py
Traceback (most recent call last):
 File "/home/ianmontgomery/Documents/testCutter/testcutter.py", line 1, in <module>
 from RPi import GPIO as GPIO
 File "/home/ianmontgomery/Documents/testCutter/testCutter/lib/python3.11/site-packages/RPi/GPIO/__init__.py", line 13, in <module>
 import lgpio
ModuleNotFoundError: No module named 'lgpio'
Last edited by fawwal on Sun Mar 24, 2024 4:48 am, edited 1 time in total.

prwiley
Posts: 33
Joined: Tue Jun 19, 2018 2:08 pm

Re: RuntimeError: Failed to add edge detection

Thu May 09, 2024 1:56 pm

I am having this issue as well. Any luck in fixing?

b0b89
Posts: 1
Joined: Thu May 16, 2024 2:41 am

Re: RuntimeError: Failed to add edge detection

Thu May 16, 2024 2:44 am

I'm having this problem too after updating, all my packages and I ran rpi-update cause some post said it could help with my wifi not reconnecting when it drops. My wifi does seem a little more stable but my cat speedometer doesn't work now.

abishur
Posts: 4505
Joined: Thu Jul 28, 2011 4:10 am

Re: RuntimeError: Failed to add edge detection

Tue Jun 18, 2024 5:36 pm

Any update on this? I'm hitting this issue where no matter what pin I select I'm getting the runtimeError: Failed to add edge detection message

hippy
Posts: 19831
Joined: Fri Sep 09, 2011 10:34 pm

Re: RuntimeError: Failed to add edge detection

Tue Jun 18, 2024 6:15 pm

It would help if people say what they have, what they mean by 'updated' when that applies.

The problem usually seems to be using the Raspberry Pi provided RPi.GPIO under Bookworm or on a Pi 5. The usual solution is to uninstall that and install Dave Jones' replacement -

Code: Select all

sudo apt remove python3-rpi.gpio
pip3 install rpi-lgpio
If using Bookworm the virtual environment should be configured and initialised first, and be active when Python code is run.

chanceofflurries
Posts: 1
Joined: Mon Sep 09, 2024 1:09 am

Re: RuntimeError: Failed to add edge detection

Mon Sep 09, 2024 1:34 pm

Thank you for this. I spent hours yesterday trying to troubleshoot this on a RPi Zero W, before I did a web search.

Ysiegel
Posts: 6
Joined: Sat Mar 06, 2021 9:56 pm

Re: RuntimeError: Failed to add edge detection

Sat Sep 28, 2024 8:03 pm

Code: Select all

sudo apt remove python3-rpi.gpio
pip3 install rpi-lgpio
Does this work ? I have the same problem for edge detection only on a pi zero since update and upgrade... And with a fresh bookworm image I did as consequence

What does this code do? does the code need to be adapted? I know little about GPIO...
Thank you

B.Goode
Posts: 18780
Joined: Mon Sep 01, 2014 4:03 pm

Re: RuntimeError: Failed to add edge detection

Sat Sep 28, 2024 9:27 pm

Ysiegel wrote:
Sat Sep 28, 2024 8:03 pm

Code: Select all

sudo apt remove python3-rpi.gpio
pip3 install rpi-lgpio
Does this work ? I have the same problem for edge detection only on a pi zero since update and upgrade... And with a fresh bookworm image I did as consequence

What does this code do? does the code need to be adapted? I know little about GPIO...
Thank you
Replies to your questions are in the extensive Raspberry Pi documentation linked from another of your posts today on this same topic.
Beware of the Leopard

hippy
Posts: 19831
Joined: Fri Sep 09, 2011 10:34 pm

Re: RuntimeError: Failed to add edge detection

Sun Sep 29, 2024 8:51 pm

Ysiegel wrote:
Sat Sep 28, 2024 8:03 pm

Code: Select all

sudo apt remove python3-rpi.gpio
pip3 install rpi-lgpio
Does this work ?
Worked for me.

However, as 'rpi-lgpio' now appears to be available as an 'apt package', I presume the Raspberry Pi recommendation to use 'apt' rather than 'pip3' will apply -

Code: Select all

sudo apt remove python3-rpi.gpio
sudo apt install python3-rpi-lgpio

IT-smurf
Posts: 13
Joined: Sat Feb 04, 2023 11:06 am

Re: RuntimeError: Failed to add edge detection

Thu Mar 06, 2025 11:36 am

I have tried all of you suggestions and installed rpi-lgpio and I get this error

Code: Select all

Traceback (most recent call last):
	File "/root/speedometer/main.py", line 4, in <module>
		import RPi.GPIO as GPIO
	File "/root/.venv/lib/python3.11/site-packages/RPi/GPIO/__init__.py", line 927, in <module>
		RPI_INFO = _get_rpi_info()
	File "/root/.venv/lib/python3.11/site-packages/RPi/GPIO/__init__.py", line 428, in _get_rpi_info
		raise NotImplementedError(
NotImplementedError: This module does not understand old-style revision codes
I'm running it on a Raspberry Pi 3 B+ on 32 bit Bookworm
This is my code:

Code: Select all

from RPLCD.i2c import CharLCD
import RPi.GPIO as GPIO
import time
lcd = CharLCD(i2c_expander='PCF8574', address=0x3f, port=1, cols=16, rows=2, dotsize=8)
lcd.clear()
GPIO.setwarnings(False) # Ignore warning for now
GPIO.setmode(GPIO.BOARD) # Use physical pin numbering
GPIO.setup(12, GPIO.OUT)
def button_callback(channel):
 lcd.clear()
 GPIO.output(12, GPIO.HIGH)
 lcd.write_string("lys tændt")
 time.sleep(1)
 lcd.clear()
 GPIO.output(12, GPIO.LOW)
 lcd.write_string("lys slukket")
GPIO.setup(29, GPIO.IN, pull_up_down=GPIO.PUD_OFF)
GPIO.add_event_detect(29, GPIO.PUD_DOWN, callback=button_callback) 
GPIO.cleanup()
lcd.clear()

hippy
Posts: 19831
Joined: Fri Sep 09, 2011 10:34 pm

Re: RuntimeError: Failed to add edge detection

Thu Mar 06, 2025 3:21 pm

IT-smurf wrote:
Thu Mar 06, 2025 11:36 am

Code: Select all

	File "/root/.venv/lib/python3.11/site-packages/RPi/GPIO/__init__.py", line 428, in _get_rpi_info
		raise NotImplementedError(
NotImplementedError: This module does not understand old-style revision codes
I'm running it on a Raspberry Pi 3 B+ on 32 bit Bookworm
Seems to be, whichever 'RPi.GPIO' this is, it has determined the revision code of the Pi hardware to be in the old-style format which shouldn't be the case for a Pi 3B+.

Only a few Pi should be using old-style revision codes; A, B, A+, B+, plus a couple of older CM variants.

Are you sure you have a "Pi 3 B+" ?

If so I would guess there's an issue with the 'RPi.GPIO' module or how it's determining the revision code.

vzzbcks
Posts: 1
Joined: Wed Feb 11, 2015 9:42 pm

Re: RuntimeError: Failed to add edge detection

Mon Mar 24, 2025 6:41 pm

hippy wrote:
Thu Mar 06, 2025 3:21 pm
Seems to be, whichever 'RPi.GPIO' this is, it has determined the revision code of the Pi hardware to be in the old-style format which shouldn't be the case for a Pi 3B+.

Only a few Pi should be using old-style revision codes; A, B, A+, B+, plus a couple of older CM variants.
I've run in to this problem with a Pi 1B. I get "RuntimeError: Failed to add edge detection" with python3-rpi.gpio and "NotImplementedError: This module does not understand old-style revision codes" with python3-rpi-lgpio.

Solution is to specify the 'new-style' RPi board revision code as an environment variable for RPi1 and some RPi2 models (page 4 here). E.g., for the RPi1 B+ v1.2 made by Sony in the UK, the code is 900032.
Last edited by vzzbcks on Mon Mar 24, 2025 7:01 pm, edited 1 time in total.

akamming
Posts: 13
Joined: Mon Oct 01, 2018 10:06 am

Re: RuntimeError: Failed to add edge detection

Sat Jul 05, 2025 8:53 am

Hi,

I know this is an old thread. I have exactly the same issue (Runtime error, failed to add edge detection), and tried a lot:

I've narrowed my issue down to:

Most simple version of script to recreate:

Code: Select all

#!/usr/bin/env python3
from gpiozero import Button
from signal import pause
# Eenvoudige callback
def on_press():
 print("Knop ingedrukt (puls gedetecteerd)")
# Maak Button op GPIO18 (BCM)
button = Button(18)
# Koppel callback
button.when_pressed = on_press
print("Wacht op GPIO18 input (druk Ctrl+C om te stoppen)")
pause()
When i run as a normal command in my user shell (either as pi user or root user), i have no issues, The script just logs everytime the button is pushed.

However when i run the script as a systemd service or in a docker container (which is the goal). I alway get the same issue:

Code: Select all

buttontest-1 | /usr/local/lib/python3.11/site-packages/gpiozero/devices.py:300: PinFactoryFallback: Falling back from lgpio: No module named 'lgpio'
buttontest-1 | warnings.warn(
buttontest-1 | Traceback (most recent call last):
buttontest-1 | File "/app/buttontest.py", line 11, in <module>
buttontest-1 | button = Button(18)
buttontest-1 | ^^^^^^^^^^
buttontest-1 | File "/usr/local/lib/python3.11/site-packages/gpiozero/devices.py", line 108, in __call__
buttontest-1 | self = super().__call__(*args, **kwargs)
buttontest-1 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
buttontest-1 | File "/usr/local/lib/python3.11/site-packages/gpiozero/input_devices.py", line 412, in __init__
buttontest-1 | super().__init__(
buttontest-1 | File "/usr/local/lib/python3.11/site-packages/gpiozero/mixins.py", line 417, in __init__
buttontest-1 | super().__init__(*args, **kwargs)
buttontest-1 | File "/usr/local/lib/python3.11/site-packages/gpiozero/input_devices.py", line 168, in __init__
buttontest-1 | self.pin.when_changed = self._pin_changed
buttontest-1 | ^^^^^^^^^^^^^^^^^^^^^
buttontest-1 | File "/usr/local/lib/python3.11/site-packages/gpiozero/pins/__init__.py", line 471, in <lambda>
buttontest-1 | lambda self, value: self._set_when_changed(value),
buttontest-1 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
buttontest-1 | File "/usr/local/lib/python3.11/site-packages/gpiozero/pins/pi.py", line 639, in _set_when_changed
buttontest-1 | self._enable_event_detect()
buttontest-1 | File "/usr/local/lib/python3.11/site-packages/gpiozero/pins/rpigpio.py", line 220, in _enable_event_detect
buttontest-1 | GPIO.add_event_detect(
buttontest-1 | RuntimeError: Failed to add edge detection
This is the docker container output, using this dockerfile:
FROM python:3.11-slim

# Vereiste buildtools installeren
RUN apt-get update && apt-get install -y \
gcc \
python3-dev \
&& rm -rf /var/lib/apt/lists/*

# Vereiste Python libs installeren
RUN pip install gpiozero RPi.GPIO

# App toevoegen
WORKDIR /app
COPY buttontest.py .

CMD ["python", "buttontest.py"]
using this docker-compose.yaml:

Code: Select all

services:
 buttontest:
 build: .
 privileged: true # noodzakelijk voor GPIO toegang
 devices:
 - "/dev/gpiomem:/dev/gpiomem"
 volumes:
 - /sys/class/gpio:/sys/class/gpio
 restart: unless-stopped
however running the same thing as a systemd service:

Code: Select all

[Unit]
Description=gpio2mqtt
After=network.target
[Service]
PermissionsStartOnly=false
ExecStart=/home/pi/CarportGPIO2MQTT/bin/python /home/pi/CarportGPIO2MQTT/gpiomqtt.py
WorkingDirectory = /home/pi/CarportGPIO2MQTT
Type=simple
Restart=always
RestartSec=1
[Install]
WantedBy=multi-user.target
also gives the same error.
Again: Running it from the command prompt runs fine (both as pi or root user)

I've tried forcing the pin factory (nor result) or installing the other lgpio libs, resulted in the same behaviour: Runs fine when running from the prompt, but fails under docker of systemd service

Is there anyone who can help me?

(BTW: I am running on a pi4 bookworm arm 64 bit, and just to rule out anything: Updated everything with apt update/upgrade)

EDIT: Found it: it was in a venv environment, when i update the correct libs and that venv environment it worked as a service (still need to fix for the docker, but step further now..)

16 posts • Page 1 of 1

Return to "Python"

AltStyle によって変換されたページ (->オリジナル) /