There are several things going on in your code. I suspect that what's really causing the problem is that you need to exit the interrupt handler before another interrupt callback can be triggered...but there is also a confusing mix of callback-based handlers and the GPIO.event_detected method.

I think you can simplify things by performing less manipulation of your interrupt configuration. Just have a state variable that starts at 0, increment it to 1 on the first interrupt, so the next time the interrupt method is called you know it's the second interrupt. No need to try setting multiple handlers like that.

Keeping in mind that I don't actually know what you're trying to do...I imagine something like this:

import RPi.GPIO as GPIO
import time

state = 0

GPIO.setmode(GPIO.BCM)
GPIO.setup(26, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(19, GPIO.IN, pull_up_down=GPIO.PUD_UP)


def interrupt_handler(channel):
    global state

    print("interrupt handler")

    if channel == 19:
        if state == 1:
            state = 0
            print("state reset by event on pin 19")
    elif channel == 26:
        if state == 0:
            state = 1
            print("state set by event on pin 26")


GPIO.add_event_detect(26, GPIO.RISING,
                      callback=interrupt_handler,
                      bouncetime=200)

GPIO.add_event_detect(19, GPIO.RISING,
                      callback=interrupt_handler,
                      bouncetime=200)


while (True):
    time.sleep(0)
Answer from larsks on Stack Overflow
🌐
The Robotics Back-End
roboticsbackend.com › home › raspberry pi gpio interrupts tutorial
Raspberry Pi GPIO Interrupts Tutorial - The Robotics Back-End
December 30, 2021 - Here are 3 more code example to show you different ways to use GPIO interrupts on your Raspberry Pi. First, let’s add a LED to our circuit. Connect the shorter leg to the ground, and in between add a resistor (330 Ohm here). Then connect the longer leg of the LED to GPIO 20. Goal: power on the LED when the button is pressed, power off the LED when the button is released (you might have to tweak the bouncetime if you press the button very fast). #!/usr/bin/env python3 import signal import sys import RPi.GPIO as GPIO BUTTON_GPIO = 16 LED_GPIO = 20 def signal_handler(sig, frame): GPIO.cleanup()
🌐
RasPi.TV
raspi.tv › 2013 › how-to-use-interrupts-with-python-on-the-raspberry-pi-and-rpi-gpio-part-3
How to use interrupts with Python on the Raspberry Pi and RPi.GPIO – part 3
June 22, 2015 - Multiple threaded callback interrupts in Python We’ve been learning about interrupts this week because of the brand new interrupt capabilities of RPi.GPIO. We covered a simple “wait for…
Discussions

gpio - How do I implement an interrupt service routine on Raspberry Pi? - Raspberry Pi Stack Exchange
There are several libraries like WiringPi, RPi and pigpio, claiming to implement interrupt handling for GPIO signals. But as far as I can estimate, they all do polling on the pins, therefore implem... More on raspberrypi.stackexchange.com
🌐 raspberrypi.stackexchange.com
January 11, 2021
hardware - How can I write a GPIO interrupt driver? - Raspberry Pi Stack Exchange
It monitors the GPIO pins and allows you to specify callback functions that are called when the pin state changes. ... The first line of that package documentation says "Note that this module is unsuitable for real-time or timing critical applications. This is because you can not predict when Python will be busy garbage collecting." Python is not suitable for my task, as I said. ... My impression was that you didn't want to use Python for the interrupt ... More on raspberrypi.stackexchange.com
🌐 raspberrypi.stackexchange.com
August 7, 2020
python - RPi.GPIO interrupt - Raspberry Pi Stack Exchange
I don't use the Python libraries for programming my Pis, but in the other languages I do use, no, you cannot. You should however be able to set up some global variables and access them from within your ISR though... ... I am not sure about RPi.GPIO but Joan's pigpio library also offers callbacks ... More on raspberrypi.stackexchange.com
🌐 raspberrypi.stackexchange.com
July 22, 2017
Raspberry Pi- GPIO Events in Python - Stack Overflow
The counter and detecting motion multiple times is used to reduce the number of false positives that the sensor picks up. ... The RPi.GPIO Python library now supports Events, which are explained in the Interrupts and Edge detection paragraph. So after updating your Raspberry Pi with sudo rpi-update ... More on stackoverflow.com
🌐 stackoverflow.com
Top answer
1 of 2
2

There are several things going on in your code. I suspect that what's really causing the problem is that you need to exit the interrupt handler before another interrupt callback can be triggered...but there is also a confusing mix of callback-based handlers and the GPIO.event_detected method.

I think you can simplify things by performing less manipulation of your interrupt configuration. Just have a state variable that starts at 0, increment it to 1 on the first interrupt, so the next time the interrupt method is called you know it's the second interrupt. No need to try setting multiple handlers like that.

Keeping in mind that I don't actually know what you're trying to do...I imagine something like this:

import RPi.GPIO as GPIO
import time

state = 0

GPIO.setmode(GPIO.BCM)
GPIO.setup(26, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(19, GPIO.IN, pull_up_down=GPIO.PUD_UP)


def interrupt_handler(channel):
    global state

    print("interrupt handler")

    if channel == 19:
        if state == 1:
            state = 0
            print("state reset by event on pin 19")
    elif channel == 26:
        if state == 0:
            state = 1
            print("state set by event on pin 26")


GPIO.add_event_detect(26, GPIO.RISING,
                      callback=interrupt_handler,
                      bouncetime=200)

GPIO.add_event_detect(19, GPIO.RISING,
                      callback=interrupt_handler,
                      bouncetime=200)


while (True):
    time.sleep(0)
2 of 2
0

That sleep(0) just causes the process to idle there while waiting for switch interrupts. Since nothing in there programmatically ends the process, doing a Ctl-C will stop it.

🌐
Raspberry Pi Forums
forums.raspberrypi.com › board index › programming › python
how do interrupts work on rpi4 - Raspberry Pi Forums
March 14, 2023 - Linux does handle hardware interrupts, whether they are GPIO level changes or a timer etc. It does not poll for changes. Linux then notifies any software which has registered an interest in that interrupt. That might e.g. be a Python script or a C program etc. If the program is not running it will have to be scheduled to run. If the program is running the relevant thread within the program will have to be triggered. That is what introduces the latency. pigpio adds another method, it uses DMA to provide regular snapshots of the GPIO.
🌐
Medium
medium.com › @rxseger › interrupt-driven-i-o-on-raspberry-pi-3-with-leds-and-pushbuttons-rising-falling-edge-detection-36c14e640fef
Interrupt-driven I/O on Raspberry Pi 3 with LEDs and pushbuttons: rising/falling edge-detection using RPi.GPIO | by R. X. Seger | Medium
August 22, 2016 - Sounds complicated, fortunately the RPi.GPIO Python module included in Raspbian supports interrupts nearly as easily as polling. Raspberrywebserver.com’s Using Interrupt Driven GPIO is a good introduction.
🌐
Quorten Blog 1
quorten.github.io › quorten-blog1 › blog › 2020 › 09 › 12 › rpi-gpio-int-uspace
A more elegant way to get Raspberry Pi GPIO interrupts in user-space | Quorten Blog 1
September 12, 2020 - Linux epoll is the name of the game for registering Raspberry Pi GPIO interrupts into your user-space code. 20200912/DuckDuckGo linux epoll 20200912/https://en.wikipedia.org/wiki/Epoll · Well… looking further into the programming interface, it isn’t ideal for what I was thinking. epoll needs to be used in a waiting loop, so if you want an interrupt service routine style interface, then you need to spawn a thread to handle the waiting loop.
🌐
Raspberry Pi
projects-raspberry.com › how-to-use-interrupts-with-python-on-the-raspberry-pi-and-rpi-gpio
How to use interrupts with Python on Raspberry Pi and RPi.GPIO
September 2, 2025 - This tutorial explains how to use interrupts with Python on the Raspberry Pi using the RPi.GPIO library. It covers configuring GPIO pins, setting up event detection, and writing callback functions to respond to input changes immediately.
Find elsewhere
🌐
YouTube
youtube.com › watch
Raspberry Pi - How to Handle GPIO Interrupts with Python 3 - YouTube
Learn how to use GPIO interrupts on your Raspberry Pi, using the RPi.GPIO library. Step by step explanation for both wait_for_edge() and add_event_detect() f...
Published   December 16, 2021
🌐
pytz
pythonhosted.org › RPIO › rpio_py.html
RPIO, the Python module - GPIO & TCP Interrupts
RPIO.py extends RPi.GPIO in various ways, and uses the BCM GPIO numbering scheme by default. ... RPIO can listen for two kinds of interrupts: GPIO and TCP. GPIO interrupts happen when the state on a specific GPIO input changes.
🌐
SourceForge
sourceforge.net › home › browse › raspberry-gpio-python › wiki
raspberry-gpio-python / Wiki / Inputs
# wait for up to 5 seconds for a rising edge (timeout is in milliseconds) channel = GPIO.wait_for_edge(channel, GPIO_RISING, timeout=5000) if channel is None: print('Timeout occurred') else: print('Edge detected on channel', channel)
🌐
AB Electronics UK
abelectronics.co.uk › home › help and support › knowledge base › io pi plus tutorials › io pi plus tutorial 4 - more interrupts
IO Pi Tutorial 4 - More Interrupts IO Pi Interrupts
July 30, 2024 - The IO Pi Plus is supplied with Bus 1 on I2C address 0x20 and Bus 2 on 0x21; if you have changed the I2C addresses, you must update the code below to use the new I2C addresses. The AB Electronics Python library uses another library called python3-smbus; you can install it using apt-get with the following commands. sudo apt-get update sudo apt-get install python3-smbus · The RPi.GPIO library is needed to configure and listen for events on the Raspberry Pi’s GPIO header.
🌐
Raspberrypi-aa
raspberrypi-aa.github.io › session2 › input.html
Polled and Interrupt Driven Input - Introduction to Raspberry Pi
Interrupt driven input means taking an action when an input changes in a desired way. Polled input is the simplest method of input on the Raspberry Pi. For a desired pin, the state of the input pin is checked for the input value. If the input value changes, the program can change its behavior.
🌐
Raspberry Pi Forums
forums.raspberrypi.com › board index › programming › python
GPIO input interrupt: minimizing jitter & delay - Raspberry Pi Forums
#!/usr/bin/env python2.7 # http://raspi.tv/2013/how-to-use-interrupts-with-python-on-the-raspberry-pi-and-rpi-gpio from __future__ import print_function import RPi.GPIO as GPIO import sys, os from subprocess import call from datetime import datetime from time import sleep # set this to highest (real-time) process scheduler priority, and nice -20 myPID=os.getpid() call(["sudo", "chrt", "-p", "99", str(myPID)]) call(["sudo", "renice", "-20", str(myPID)]) GPIO.setmode(GPIO.BCM) # use Broadcom GPIO numbering plan GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP) dMin = 999.0 # minimum observed pul
🌐
Stack Exchange
raspberrypi.stackexchange.com › questions › 23328 › best-way-to-report-all-gpio-interrupts
python - Best way to report all GPIO Interrupts? - Raspberry Pi Stack Exchange
September 16, 2014 - I am new to linux and python, but I am picking things up quickly, I'm currently polling a Pi using the rest service WebPiIO, but would rather have the event driven from the pi itself back to a URL that I can work with from there. Thanks in advance for any assistance. ... It depends on how serious you are and the character of the interrupts. Given that this is tagged with home automation I guess you don't have challenging requirements. Python will never be the best way to monitor interrupts while it is a interpreted language.