RPi.GPIO errors: “Failed to add edge detection” and “channel already in use”

Call GPIO.cleanup() when the program ends, and set each pin up once.

What the message means

The library keeps pin state while your program runs. A pin left set by an earlier run or another process triggers these errors.

Fix it in this order

  1. Wrap the main code in try and finally, and call cleanup in finally.
  2. Stop any other script that uses the same pins.
  3. Set each pin up once at the start.
  4. Add a bounce time to edge detection.
  5. Use only one numbering mode, BCM or BOARD.
  6. Try the gpiozero library if you want simpler code.

Why it happens

RPi.GPIO raises “Failed to add edge detection” when the pin is not set up as an input before add_event_detect is called, when edge detection was already added to that pin, or when another process holds the pin. Because the library remembers pin state until cleanup runs, a script that crashed earlier can leave the next run in a bad state.

On a Raspberry Pi 5 the old RPi.GPIO library does not work, because the GPIO hardware changed. The rpi-lgpio package provides a compatible interface, and gpiozero works on both. Check your Pi model before spending time on wiring.

import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP)
try:
    GPIO.add_event_detect(17, GPIO.FALLING, bouncetime=200)
    # main loop here
finally:
    GPIO.cleanup()

The example above has not been run on hardware here; test it on your own board.

If you see this, do this

You seeDo this
Edge detection fails on startPin still held by an old run.
Works once then errorsMissing cleanup.

Pi power, bounce.

Last reviewed: November 2026. These are the usual causes and the order that finds them fastest. Test on your own board; clones and cables differ.