I ran a python script on a Raspberrypi 4 on Raspbian Bookworm for a ups (uninterruptible power supply) quite a while. Since the latest update of bookworm the script fails. Since the script of the ups is somewhat complex i narrowed down the issue to the following code fragment:
import RPi.GPIO as GPIO
import time
CLOCK_PIN = 27
PULSE_PIN = 22
BOUNCE_TIME = 30
def isr(channel):
return
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(CLOCK_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(PULSE_PIN, GPIO.OUT, initial=1)
GPIO.add_event_detect(CLOCK_PIN, GPIO.FALLING, callback=isr, bouncetime=BOUNCE_TIME)
time.sleep(30)
GPIO.cleanup()
with this error:
Traceback (most recent call last): File "picofssd.py", line 15, in <module> GPIO.add_event_detect(CLOCK_PIN, GPIO.FALLING, callback=isr, bouncetime=BOUNCE_TIME) RuntimeError: Failed to add edge detection
The code is run in a service as root. Unfortunately i have no idea what can cause the error. Pin 27 should not have another function than gpio and user root should have all access rights.
I found hints that this can be caused when script is run as normal user, but is run as root. Pin has no dual function, so it should also not be a configuration issue.
Any hints what can cause this error?
-
Its the driver part for this USV. It worked for a long time, but stopped with last update ( apt-get upgrade)uenz– uenz2024年03月18日 11:20:46 +00:00Commented Mar 18, 2024 at 11:20
3 Answers 3
After doing a lot of research i found a solution myself: It seems that the kernel interface for gpio access changed from library gpio to lgpio. Switching the python package rpi-gpio to rpi-lgpio as provider for the RPi.GPIO include solved the issue without any changes in code.
-
This is nonsense. RPi.GPIO never used "the kernel interface" it directly accessed registers.Milliways– Milliways2024年03月21日 11:30:41 +00:00Commented Mar 21, 2024 at 11:30
-
4@Milliways Maybe the wording is nonsense, but not the message. Python cannot access the CPU Registers by itself. It relies on a library. The library provided with rpi-gpio stopped working on by bookworm installation with the above mentioned error. Replacing the rpi-gpio library by the rpi-lgpio library did the trick for my installation. May explanation was taken from the description of rpi-lgpio.uenz– uenz2024年03月22日 14:23:48 +00:00Commented Mar 22, 2024 at 14:23
-
1Switching to
lgpio
solved the "Failed to add edge detection" problem, but was not a drop-in replacement for me. Edges are detected later than withRPi.GPIO
and are not debounced the same. See also: rpi-lgpio.readthedocs.io/en/release-0.4/differences.htmlPerette– Perette2024年05月20日 21:02:19 +00:00Commented May 20, 2024 at 21:02 -
I have similar issue and strace see problem at
faccessat(AT_FDCWD, "/sys/class/gpio/gpio3", F_OK) = -1 ENOENT (No such file or directory)
where/sys/class/gpio/gpio3
does not exist in filesystem in Bookworm.TMa– TMa2024年07月06日 19:07:33 +00:00Commented Jul 6, 2024 at 19:07
I don't have the reputation to comment, so here a short addon to uenz's post.
If apt
does not find the package out of the box (e.g. freshly dumped raspi image), you just need a short package update. So the three commands to switch from classic gpio to lgpio are:
sudo apt remove python3-rpi.gpio
sudo apt update
sudo apt install python3-rpi-lgpio
-
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From ReviewMilliways– Milliways2024年05月08日 22:17:01 +00:00Commented May 8, 2024 at 22:17
-
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.2024年05月18日 18:23:42 +00:00Commented May 18, 2024 at 18:23
The python3-rpi-lgpio package is definitely the way to go. However, there is an alternative to python3-rpi-lgpio called GPIOconverter if you find any issues with python3-rpi-lgpio.
You can find it at https://bobrathbone.com/raspberrypi/gpio_converter.html
-
You are the author? Even if it seems obvious, you need to explicitly state that fact.2025年03月08日 12:45:18 +00:00Commented Mar 8 at 12:45
-
Where are your community guidelines? Sorry but I can't find them.Bob Rathbone– Bob Rathbone2025年03月09日 13:23:50 +00:00Commented Mar 9 at 13:23
-
Maybe look on Meta, I can't find the post I am looking for. Nevertheless, if you don't explicitly state that the site/code is your own, you might be accused of self-promotion (or spam). It is often a good idea to just include something along the lines of "Note: I am the author of the code/owner of the website"2025年03月09日 13:59:05 +00:00Commented Mar 9 at 13:59