- chazwilmot
- Posts: 1
- Joined: Sun Mar 10, 2024 7:10 pm
RuntimeError: Failed to add edge detection
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):
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
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
Re: RuntimeError: Failed to add edge detection
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:
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
Re: RuntimeError: Failed to add edge detection
Sheesh, I'm having the same issues. What should someone at the edge of their understanding do in the meantime?
I tried uninstalling RPi-GPIO and installing RPi-lgpio in my venv, but I'm getting this :shock:
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 detectionCode: 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.
Re: RuntimeError: Failed to add edge detection
I am having this issue as well. Any luck in fixing?
Re: RuntimeError: Failed to add edge detection
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.
Re: RuntimeError: Failed to add edge detection
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
Re: RuntimeError: Failed to add edge detection
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 -
If using Bookworm the virtual environment should be configured and initialised first, and be active when Python code is run.
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
- chanceofflurries
- Posts: 1
- Joined: Mon Sep 09, 2024 1:09 am
Re: RuntimeError: Failed to add edge detection
Thank you for this. I spent hours yesterday trying to troubleshoot this on a RPi Zero W, before I did a web search.
Re: RuntimeError: Failed to add edge detection
Code: Select all
sudo apt remove python3-rpi.gpio
pip3 install rpi-lgpioWhat does this code do? does the code need to be adapted? I know little about GPIO...
Thank you
Re: RuntimeError: Failed to add edge detection
Replies to your questions are in the extensive Raspberry Pi documentation linked from another of your posts today on this same topic.Ysiegel wrote: ↑Sat Sep 28, 2024 8:03 pmDoes 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 consequenceCode: Select all
sudo apt remove python3-rpi.gpio pip3 install rpi-lgpio
What does this code do? does the code need to be adapted? I know little about GPIO...
Thank you
Beware of the Leopard
Re: RuntimeError: Failed to add edge detection
Worked for me.Ysiegel wrote: ↑Sat Sep 28, 2024 8:03 pmDoes this work ?Code: Select all
sudo apt remove python3-rpi.gpio pip3 install rpi-lgpio
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
Re: RuntimeError: Failed to add edge detection
I have tried all of you suggestions and installed rpi-lgpio and I get this error
I'm running it on a Raspberry Pi 3 B+ on 32 bit Bookworm
This is my code:
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
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()
Re: RuntimeError: Failed to add edge detection
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+.IT-smurf wrote: ↑Thu Mar 06, 2025 11:36 amI'm running it on a Raspberry Pi 3 B+ on 32 bit BookwormCode: 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
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.
Re: RuntimeError: Failed to add edge detection
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.hippy wrote: ↑Thu Mar 06, 2025 3:21 pmSeems 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.
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.
Re: RuntimeError: Failed to add edge detection
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:
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:
This is the docker container output, using this dockerfile:
however running the same thing as a systemd service:
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..)
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()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
using this docker-compose.yaml: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"]
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
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
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..)
Jump to
- Community
- General discussion
- Announcements
- Other languages
- Deutsch
- Español
- Français
- Italiano
- Nederlands
- 日本語
- Polski
- Português
- Русский
- Türkçe
- User groups and events
- Raspberry Pi Official Magazine
- Using the Raspberry Pi
- Beginners
- Troubleshooting
- Advanced users
- Assistive technology and accessibility
- Education
- Picademy
- Teaching and learning resources
- Staffroom, classroom and projects
- Astro Pi
- Mathematica
- High Altitude Balloon
- Weather station
- Programming
- C/C++
- Java
- Python
- Scratch
- Other programming languages
- Windows 10 for IoT
- Wolfram Language
- Bare metal, Assembly language
- Graphics programming
- OpenGLES
- OpenVG
- OpenMAX
- General programming discussion
- Projects
- Networking and servers
- Automation, sensing and robotics
- Graphics, sound and multimedia
- Other projects
- Media centres
- Gaming
- AIY Projects
- Hardware and peripherals
- Camera board
- Compute Module
- Official Display
- HATs and other add-ons
- Device Tree
- Interfacing (DSI, CSI, I2C, etc.)
- Keyboard computers (400, 500, 500+)
- Raspberry Pi Pico
- General
- SDK
- MicroPython
- Other RP2040 boards
- Zephyr
- Rust
- AI Accelerator
- AI Camera - IMX500
- Hailo
- Software
- Raspberry Pi OS
- Raspberry Pi Connect
- Raspberry Pi Desktop for PC and Mac
- Beta testing
- Other
- Android
- Debian
- FreeBSD
- Gentoo
- Linux Kernel
- NetBSD
- openSUSE
- Plan 9
- Puppy
- Arch
- Pidora / Fedora
- RISCOS
- Ubuntu
- Ye Olde Pi Shoppe
- For sale
- Wanted
- Off topic
- Off topic discussion