I was writing a Python script on my Raspberry Pi Zero 2W to scan a matrix keypad I built for a DIY synthesizer project, and I kept getting the following error: RuntimeError: Failed to add edge detection
whenever I used the GPIO.add_event_detect
function. It turns out that there is currently a bug with the new kernel/Raspberry Pi OS (e.g. GPIO.add_event_detect no longer works with the new kernel 6.60 · Issue #6037 · raspberrypi/linux, RuntimeError: Failed to add edge detection – Raspberry Pi Forums). This article will outline my debugging journey and how you may fix this error yourself.
First, I ran sudo raspi-config
and disabled all of the interfaces that may have been interfering with my pin (assuming GPIO 2, then I2C1). I rebooted, but the issue still persisted.
Then, I ran sudo nano /boot/firmware/config.txt
(may be sudo nano /boot/config.txt
on your machine) and looked for anything like enable_uart=1
or similar that may be already taking control of the pin, but I found nothing.
Then, I ran ls -l /sys/class/gpio
to see if any pin has already been exported, which may be interfering with the pins, but I didn’t find anything notable.
Then, I ran a small test script:
import RPi.GPIO as GPIO
import time
TEST_PIN = 2
def test_callback(channel):
print(f"SUCCESS: Event detected on pin {channel}!")
try:
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(True) # See if any warnings appear before the error
print(f"Attempting to set up pin {TEST_PIN} as INPUT PULL_UP.")
GPIO.setup(TEST_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP)
print(f"Pin {TEST_PIN} setup complete.")
time.sleep(0.1) # wait
print(f"Attempting to add event detection to pin {TEST_PIN}.")
GPIO.add_event_detect(TEST_PIN, GPIO.FALLING, callback=test_callback, bouncetime=200)
print(f"Event detection added successfully to pin {TEST_PIN}.")
while True:
time.sleep(1)
except RuntimeError as e:
print(f"ERROR during GPIO operation on pin {TEST_PIN}: {e}")
except Exception as e:
print(f"An unexpected error occurred: {e}")
finally:
print("Cleaning up GPIO...")
GPIO.cleanup()
The test script gave me the following error:
--- Testing GPIO Pin 2 (BCM) ---
GPIO mode set to BCM and warnings enabled.
Setting up pin 2 as an input with internal pull-up resistor.
/home/stefan/test_gpio2.py:20: RuntimeWarning: A physical pull up resistor is fitted on this channel!
GPIO.setup(TEST_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP)
Pin 2 setup complete. Current state: 1
Attempting to add falling edge event detection to pin 2.
!!! RUNTIME ERROR on pin 2: Failed to add edge detection
This told me that it wasn’t a script issue, and it wasn’t some other app hijacking the pins. I ran the script for a bunch more pins, including ones not connected to any interface (like 26), and got the same error. Next, I ran dmesg | tail -n 10
immediately after the script and saw the following output:
[ 147.733623] export_store: invalid GPIO 2
[ 171.889673] export_store: invalid GPIO 4
[ 179.997388] export_store: invalid GPIO 7
[ 200.617476] export_store: invalid GPIO 26
Then, I knew that it was a library issue, since this was a fresh install of Raspberry Pi OS. I ran sudo pip3 uninstall RPi.GPIO
, sudo apt purge python3-rpi-lgpio
, and sudo apt autoremove
. Afterwards, I ran sudo apt update
and only ran sudo apt install python3-rpi-lgpio
. Not installing RPi.GPIO through pip seemed to have fixed the issue. If you have the same issue, try following the steps above to diagnose it. I had the same issue on my Raspberry Pi Zero 2W too, and the same fix fixed it.