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.

Why this happens

The RPi.GPIO library keeps an internal record of every channel you configure. add_event_detect() registers an edge interrupt for one pin and starts a background thread that calls your function. Only one detection can exist per pin, so a second add_event_detect() call on the same pin raises the RuntimeError, even if the first call was many lines earlier or inside a loop.

The “channel already in use” warning comes from setup() finding that the pin was configured before and never released. The usual source is a program that was stopped with Ctrl+C or crashed before cleanup() ran, or a notebook cell that you executed twice. Another running script, or a kernel overlay such as the 1-wire or I2C driver, can also hold a pin, and then the library cannot take it.

How to confirm the cause

  • Run ps aux and look for a second Python process that imports RPi.GPIO. Stop it, then retry; if the error vanishes, two programs were competing.
  • Print GPIO.getmode() after setmode(). A value of GPIO.BCM or GPIO.BOARD tells you which numbering is active; calling setmode() with the other one afterwards is an error, and it also explains why a pin number seems to be wrong.
  • Call GPIO.gpio_function(pin) before setting the pin up. If it returns GPIO.OUT when you expected an input, an earlier run left it in the wrong state.
  • On Raspberry Pi OS, sudo cat /sys/kernel/debug/gpio lists lines that are claimed and by which consumer. A line owned by an overlay or another driver cannot be used by your script.
  • Cut the program down to one pin and one add_event_detect() call. If that works, the fault is a duplicate registration somewhere else in your code.

Worked example

This version removes any old registration first, registers once, and always cleans up. Pin 17 and the pull-up wiring are examples; change them for your own board.

import RPi.GPIO as GPIO
PIN = 17
def pressed(channel):
    print("edge on", channel)
GPIO.setmode(GPIO.BCM)
GPIO.setup(PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.remove_event_detect(PIN)
GPIO.add_event_detect(PIN, GPIO.FALLING, callback=pressed, bouncetime=200)
try:
    input("Press Enter to quit\n")
finally:
    GPIO.cleanup()

Common mistakes

  • Calling add_event_detect() inside a while loop, so the second pass fails.
  • Registering the same pin with add_event_detect() and also using wait_for_edge() on it.
  • Mixing a BCM pin number with BOARD mode, or the reverse, and then blaming the library.
  • Putting cleanup() only at the end of the file, where Ctrl+C never reaches it.
  • Running the script in an interactive session or notebook and re-running a cell without cleanup.

If this did not fix it

  • Check the Pi model. RPi.GPIO does not run on a Pi 5, so a different library or the rpi-lgpio replacement is needed.
  • Check whether the pin is taken by a system function such as a serial port, 1-wire or an I2C overlay set in the Pi configuration.
  • Check the wiring. A floating input without a pull-up produces edges you did not expect, which looks like a library fault.