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


def isr(channel):

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)

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?

  • What are these pins connected to? How are you using them?
    – Milliways
    Commented Mar 17 at 10:38
  • Its the driver part for this USV. It worked for a long time, but stopped with last update ( apt-get upgrade)
    – uenz
    Commented Mar 18 at 11:20

2 Answers 2


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
    Commented Mar 21 at 11:30
  • 3
    @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
    Commented Mar 22 at 14:23
  • 1
    Switching to lgpio solved the "Failed to add edge detection" problem, but was not a drop-in replacement for me. Edges are detected later than with RPi.GPIO and are not debounced the same. See also: rpi-lgpio.readthedocs.io/en/release-0.4/differences.html
    – Perette
    Commented May 20 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
    Commented Jul 6 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.