# Interrupts

While a great amount of logic can be handled within our code, `while` loops are not very conducive to event driven
 behavior. [Enter interrupts](https://randomnerdtutorials.com/micropython-interrupts-esp32-esp8266/). When a change is detected interrupts trigger a handler function. 

When an interrupt occurs the processor halts execution of the main program and executes the interrupt handling routine
then returns control back to the main program.

On the ESP32 you can use all pins as interrupts with the except of pins 6 - 11. 

## Basics

Let's use a button and an LED to demonstrate the basics of interrupts.

Your interrupt handler must accept a single parameter, a `Pin` object, which will be set to the pin triggering the interrupt.

````
def hangle_interrupt(pin):
    pass
````

Set the GPIO that will be used as the input pin. 
```
pin = Pin(14, Pin.IN)
```

Attach an interrupt to pin.
```
pin.irq(trigger=Pin.IRQ_RISING, handler=handle_interrupt)
```

There are 3 different trigger modes you can use:
* IRQ_RISING - Trigger on the rising edge
* IRQ_FALLING - Trigger on the falling edge. For example when a button is released.
* 3 - Trigger the interrupt on both edges.

