Search code examples
interruptmicropythonraspberry-pi-pico

How to find which pin caused an interrupt?


How to find which pin caused the interrupt?

def handle_interrupt(Pin):
    print(Pin)

int1 = Pin(2, Pin.IN,Pin.PULL_UP)
int1.irq(trigger=Pin.IRQ_FALLING, handler=handle_interrupt)

int2 = Pin(10, Pin.IN,Pin.PULL_UP)
int2.irq(trigger=Pin.IRQ_FALLING, handler=handle_interrupt)

Output:

Pin(2, mode=IN, pull=PULL_UP)

or:

Pin(10, mode=IN, pull=PULL_UP)

I can use different handlers, or convert the class to a string and split, but is there an easier way?


Solution

  • This seems to work:

    def handle_interrupt(irq):
        print(irq)
    
    int1 = Pin(2, Pin.IN,Pin.PULL_UP)
    int1.irq(trigger=Pin.IRQ_FALLING, handler=lambda a:handle_interrupt(2))
    
    int2 = Pin(10, Pin.IN,Pin.PULL_UP)
    int2.irq(trigger=Pin.IRQ_FALLING, handler=lambda a:handle_interrupt(10))