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 - Learn why, when, and how to use interrupts with GPIOs on your Raspberry Pi programs. Complete tutorial with code examples in Python, using RPi.GPIO module.
🌐
Raspberry Pi Forums
forums.raspberrypi.com › board index › hardware and peripherals › raspberry pi pico › general
Are the GPIO pins in RPi Pico Hardware Interrupts? - Raspberry Pi Forums
GPIO interrupts work differently these are interrupts which have different trigger conditions which can generate an interrupt. Generally there is a multiple conditions routed to a single interrupt. However in this case there is a single GPIO interrupt for all pins and all conditions.
Discussions

Raspberry Pi Interrupts Python (GPIO Library) - Stack Overflow
I have a little problem with my little raspberry project, I have hooked up an LCD screen and a bunch of buttons to it, from my university course about micro-controllers I've learned that interrupts More on stackoverflow.com
🌐 stackoverflow.com
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 implement a busy wait in a parallel thread. More on raspberrypi.stackexchange.com
🌐 raspberrypi.stackexchange.com
January 11, 2021
Can GPIO pins generate interrupts? - element14 Community
Join Raspberry Pi to participate - click to join for free! ... I looked at the example code for doing I/O using the GPIO pins, but — I need to be able to count impulses (between 0.01 and 250Hz), which isn't really doable with GPIO polling on a multitasking OS. Are the GPIO pins capable of generating interrupts... More on community.element14.com
🌐 community.element14.com
Raspberry Pi 2 B GPIO pwm and interrupt pins - Raspberry Pi Stack Exchange
Good day! I would like to use pwm and interrupt on raspberry GPIO pins but i don't know which GPIO pins are actually dedicated for pwm output and which is for hardware interrupts, is there a way fo... More on raspberrypi.stackexchange.com
🌐 raspberrypi.stackexchange.com
🌐
Raspberry Pi Forums
forums.raspberrypi.com › board index › programming › bare metal, assembly language
GPIO Interrupt - Raspberry Pi Forums
*/ interrupts = <2 17>, <2 18>, <2 19>, <2 20>; when the config says <2 17>, it means the 2nd bank of 32, so 32+17, aka 49 it looks like internally, it was designed as a 96(3*32) pin gpio controller, but only ~53 pins are actually setup on any pi SoC and the 4th interrupt is to let you interrupt ...
🌐
Raspberry Pi Forums
forums.raspberrypi.com › board index › programming › python
how do interrupts work on rpi4 - Raspberry Pi Forums
March 14, 2023 - 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.
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.

Find elsewhere
🌐
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 - Raspberrywebserver.com’s Using Interrupt Driven GPIO is a good introduction. The basic idea is instead of reading the current input state using GPIO.input(), add a callback function using GPIO.add_event_detect(). You can call GPIO.add_event_detect() to say what condition you are looking for, then GPIO.add_event_callback() to set the callback for said condition: GPIO.add_event_detect(BTN_B, GPIO.RISING) GPIO.add_event_callback(BTN_B, lambda pin: GPIO.output(LED_B, False))
🌐
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 - 20200902/DuckDuckGo raspberry pi gpio interrupt 20200902/http://wiringpi.com/reference/priority-interrupts-and-threads/ I guess, after all, gpio-keys is the most elegant way to get GPIO interrupts into user-space. On the other hand… yes, for gpio-keys, you must also open the file /dev/input/event0. Well, if it’s a bummer either way, I’m guessing Linuxepoll may be easier if you only have one GPIO pin to monitor, otherwise gpio-keys is better.
🌐
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 - I modified this code to use a mcp2008 (same chip family but 8 i/o instead of 16) for the push-button inputs in my project, then made a simple wired connection (via a safety series resistor just in case the GPIO pin inadvertently became an output) between the INT output pin of the mcp2008 chip to a Pi GPIO pin. Then you CAN use the RPi.GPIO code explained on this web site to register the interrupt and respond accordingly – very many thanks for that Alex, I could not have solved my problem as to how to do this without your excellent tutorials (I am a Raspbian novice!).
🌐
element14 Community
community.element14.com › products › raspberry-pi › f › forum › 18713 › can-gpio-pins-generate-interrupts
Can GPIO pins generate interrupts? - element14 Community
If you are just measuring the interval to be about 700 microseconds, and then doing 1/interval and getting 1500+/- 15 each time, you're not missing interrupts. If you count interrupts for a long enough period, maybe 1 day of wallclock or ntp-synced time, then you should be able to calibrate the crystal of the raspberry pi.
🌐
Raspberrypi-aa
raspberrypi-aa.github.io › session2 › input.html
Polled and Interrupt Driven Input - Introduction to Raspberry Pi
GPIO.setup(pin, GPIO.IN, pull_up_down=GPIO.PUD_UP); # or GPIO.setup(pin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN); Input on the Raspberry Pi can be done in two modes, polled and interrupt. Polling for input means checking for input periodically on an as needed basis.
🌐
Raspberry Pi Projects
raspberry-projects.com › pi › programming-in-c › io-pins › gpio-interrupts
GPIO interrupts – Raspberry Pi Projects
Raspberry Pi Projects · /Programming in C/C++ / IO Pins / GPIO interrupts · http://www.raspberrypi.org/phpBB3/viewtopic.php?f=44&t=7509 · http://www.raspberrypi.org/phpBB3/viewtopic.php?f=44&t=9207 ·
🌐
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 diagram below shows how the components will be connected, with the buttons on Bus 1 and the IA pin on Bus 1 connected to GPIO 23 (pin 16) on the Raspberry Pi via a voltage divider.
🌐
Raspberry Pi
raspberrypi.org › board index › hardware and peripherals › interfacing (dsi, csi, i2c, etc.)
Interrupt handling - Raspberry Pi Forums
August 14, 2017 - I dont know exactly what's going on behind the scenes - I can just guess - the kernel gets the interrupt, then has to scan a list of stuff to do, or handlers to call - one will recognise the GPIO pin and somehow signal the scheduller to allow the userland program to un-block and run - so the ...
🌐
Phil's Blog
phil.lavin.me.uk › 2013 › 08 › how-to-use-kernel-gpio-interrupts-on-the-raspberry-pi
How to use Kernel GPIO interrupts on the Raspberry Pi
August 1, 2013 - Pins will output 3.3v when connected to ground and will be ‘raised’ as ‘on’ when 3.3v is supplied to them. You change, in software, whether a pin is input or output. Simplez. The Raspbian distribution has, since around mid 2012, had a kernel which includes support for Kernel GPIO interrupts.