I'm working on a Raspberry Pi project using Python and the rpi-lgpio library to monitor GPIO events. My setup involves connecting a RadiationD-v1.1 (CAJOE) module to GPIO pin 11 (physical pin 23) on my Raspberry Pi. However, when I run my script, I encounter the following error:
sudo python3 GPIO4.py
Traceback (most recent call last):
File "/home/raspberry/RADROBOT/GPIO4.py", line 24, in <module>
GPIO.add_event_detect(11, GPIO.FALLING, callback=countme)
RuntimeError: Failed to add edge detection
Code snippet:
Here’s the relevant portion of my code:
import lgpio as GPIO
GPIO.setmode(GPIO.BOARD) # Using physical pin numbering
GPIO.setup(11, GPIO.IN, pull_up_down=GPIO.PUD_UP)
def countme(channel):
print("Event detected on GPIO pin!")
GPIO.add_event_detect(11, GPIO.FALLING, callback=countme)
What I’ve tried so far:
- Verified hardware connections:
- The module is connected to GPIO 11 (physical pin 23) as per the Raspberry Pi pinout.
- The wiring is solid and confirmed with a multimeter.
- Software and environment setup:
- Switched from RPi.GPIO to rpi-lgpio to avoid compatibility issues.
- Installed rpi-lgpio in a virtual environment using pip install rpi-lgpio.
- Checked for GPIO conflicts:
- Killed any processes that might be using the GPIO pins (ps aux | grep python).
- Ensured no pins are being initialized twice in my script.
- Changed pins:
- Attempted to use GPIO 17 (physical pin 11) instead, but the same error occurs.
- Experimented with pull-up/down configuration:
- Adjusted the pull-up/down settings (PUD_UP and PUD_DOWN) to see if this resolved the issue.
Additional Context:
- Raspberry Pi Model: Raspberry Pi 4 Model B
- OS: Raspberry Pi OS (Bookworm)
- Module: RadiationD-v1.1 (CAJOE)
- Python Library: rpi-lgpio
My Questions:
- Why does GPIO.add_event_detect fail with this error? Could it be a hardware limitation or a software/library issue?
- Are there specific configurations required for rpi-lgpio that differ from RPi.GPIO when setting up event detection?
- How can I debug this issue further to identify the root cause?
Any advice, insights, or troubleshooting tips would be greatly appreciated. Thank you in advance for your help! 😊
-
Use gpiozero gpiozero.readthedocs.io/en/stable/recipes.html#buttonCoderMike– CoderMike2024年12月12日 17:23:07 +00:00Commented Dec 12, 2024 at 17:23
1 Answer 1
I normally avoid answering Questions which only have a code fragment; if you want help post a Minimal, Reproducible Example.
What you have posted is nonsense; you have tagged rpi-gpio
which is a "Compatibility shim for lgpio emulating the RPi.GPIO API" BUT you are NOT using it and have import lgpio as GPIO
.
The fragment is nonsense for lgpio
.
The shim may make sense if you have established code using RPi.GPIO
although RPi.GPIO
is included in Bookworm and works on a Pi4.
NOTE there are other errors; you use sudo python3
so python will NOT search in your venv but only show system libraries.
Explore related questions
See similar questions with these tags.