8

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?

asked Mar 16, 2024 at 8:46
1
  • Its the driver part for this USV. It worked for a long time, but stopped with last update ( apt-get upgrade) Commented Mar 18, 2024 at 11:20

3 Answers 3

12

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.

answered Mar 20, 2024 at 19:19
4
  • This is nonsense. RPi.GPIO never used "the kernel interface" it directly accessed registers. Commented 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. Commented Mar 22, 2024 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 Commented 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. Commented Jul 6, 2024 at 19:07
9

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
answered May 8, 2024 at 9:31
2
0

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

answered Mar 8 at 10:25
3
  • You are the author? Even if it seems obvious, you need to explicitly state that fact. Commented Mar 8 at 12:45
  • Where are your community guidelines? Sorry but I can't find them. Commented 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" Commented Mar 9 at 13:59

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.