1

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?

2
  • What are these pins connected to? How are you using them?
    – Milliways
    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
    Mar 18 at 11:20

2 Answers 2

4

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.

4
  • This is nonsense. RPi.GPIO never used "the kernel interface" it directly accessed registers.
    – Milliways
    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
    Mar 22 at 14:23
  • Thank you. Thank you. Thank you.
    – zimdanen
    May 3 at 15:47
  • 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
    2 days ago
0

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
2

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.