ESP32 Interrupt Tutorial

Published  June 17, 2022   0
ESP32 Interrupt Tutorial

Interrupts are used to handle events that do not happen during the normal execution of a program but when a specific trigger occurs. For example, if we write a program to blink an LED the microcontroller will execute each command one by one. But if we want to monitor a switch to start or stop the blinking, it will be only possible after all other tasks before the checking is completed i.e., it won’t be real-time. That’s where interrupts come to play. With interrupt, we do not need to continuously check the state of the digital input pin. When an interrupt occurs, the controller stops the execution of the main program, and a function is called upon known as ISR or the Interrupt Service Routine. The controller then executes the tasks inside the ISR and then gets back to the main program after the ISR execution is finished.

The ESP32 has a total of 32 interrupts for it’s each core. Each interrupt has a certain priority level, most (but not all) interrupts are connected to the interrupt mux. Because there are more interrupt sources than interrupts, some interrupts are shared with multiple interrupt sources.

Types of Interrupts in ESP32

The major classification of interrupts in ESP32 is based on the interrupt source. And they are Hardware Interrupts and Software interrupts.

External or Hardware Interrupts

Hardware interrupts occur in response to an external hardware event. For example, there is a Touch Interrupt which happens when touch is detected and a GPIO interrupt when the state of a GPIO pin is changed. GPIO interrupts and touch interrupts comes under this category.

Software Interrupts

This type of interrupts occurs when a software event is triggered such as timer overflow. Timer interrupts are an example of software interrupts. We will discuss more about Timers in the upcoming dedicated tutorial for ESP32 Timers.

Commonly Asked Questions about ESP32 Interrupts

Q. How many interrupts can ESP32 handle?

The ESP32 offers up to 32 interrupt slots for each core.

Q. How to use external interrupt in ESP32?

You can attach the desired pin to an interrupt and can assign an ISR for the same with the help of attachInterrupt function.

Q. In ESP32 which pins support External interrupt?

All of them support GPIO interrupt and 10 of them support touch interrupt.

Q. What are the GPIO interrupt modes ESP32 have?

ESP32 has five types of interrupt events and are LOW, HIGH, CHANGE, FALLING, and RISING.

ESP32 GPIO Interrupts

With ESP32, we can configure all the GPIO pins as hardware interrupt sources. We can enable interrupt on any of these GPIO pins by attaching them to a corresponding ISR. To attach an interrupt, we will use the attchInterrupt() macro. The usage of attachInterrupt() macro is as follows-

attachInterrupt(GPIOpin, ISR, Event);

The attachInterrupt() function takes in three arguments:

GPIOpin: This parameter indicates the GPIO pin number to which the interrupt is to be attached.

ISR: The second parameter is the name of the function that will be called every time the interrupt is triggered.

Event: The third parameter indicates at which event the interrupt should be triggered. The five possible events are the following.

  • LOW: Triggers interrupt whenever the pin is LOW

Interrupts in ESP32

  • HIGH: Triggers interrupt whenever the pin is HIGH

ESP32 Interrupt Event

  • CHANGE: Triggers interrupt whenever the pin changes value, from HIGH to LOW or LOW to HIGH

ESP32 Interrupt Change Event

  • FALLING: Triggers interrupt when the pin goes from HIGH to LOW

ESP32 Interrupt Falling Event

  • RISING: Triggers interrupt when the pin goes from LOW to HIGH

ESP32 Interrupt Rising Event

So for example, to trigger an interrupt whenever the state of GPIO4 changes, we can use the attachInterrupt function as follows:

attachInterrupt(4, ISR, CHANGE);

How to Disable an Interrupt?

In some situations, we will have to disable the interrupt temporarily. For such cases, we can use the detachInterrupt function. Once this function is called for a specific pin, the interrupt that is attached to that pin will be disabled until the attachInterrupt function is called again or the system is rebooted. The syntax for using it is as follows-


Interrupt Service Routine or ISR

This will be the function that will be called upon when the specific interrupt is triggered. The syntax is as follows:

void IRAM_ATTR ISR() {

Where ISR is the function name, and the statements are the tasks that should be completed when this function is called. Since the ISR is blocking the main program, it is recommended to use it to do tasks that are as small as possible. The parameter IRAM_ATTR will ensure that the ISR function is placed in the IRAM area instead of the flash area. This will ensure that the ISR function will load faster.

Hardware Example – Turning on or off an LED

In this example, we will turn on and off an LED with a push-button switch. But instead of using polling, we will be using the GPIO interrupt. To do that make the connections on a breadboard as shown in the below circuit diagram.

ESP32 Interrupt LED Blinking Example Circuit

Here is the actual circuit connected on a breadboard as per the circuit diagram.

ESP32 LED Blinking Example

#define pushButton_pin   33
#define LED_pin   32
void IRAM_ATTR toggleLED()
  digitalWrite(LED_pin, !digitalRead(LED_pin));
void setup()
  pinMode(LED_pin, OUTPUT);
  pinMode(pushButton_pin, INPUT_PULLUP);
  attachInterrupt(pushButton_pin, toggleLED, RISING);
void loop()

As soon as the pushbutton is pressed, the voltage at GPIO33 will fall to 0V. And when the button is released, the voltage will rise to VCC. When the voltage is rising, the interrupt will be triggered and the corresponding ISR toggleLED will be called. Because the ISR will be only triggered at the rising edge, the debouncing will be eliminated.

ESP32 Touch Interrupt

Just like the GPIO interrupt, the ESP32 supports touch Interrupt on its 10 touch inputs. Using the touch interrupt is also similar. To attach a touch interrupt, we will use the touchAttachInterrupt function. Its syntax is as follows:

touchAttachInterrupt(GPIOPin, ISR, Threshold)

Here the GPIOPin is the pin with touch input support and the ISR is the ISR function, and the Threshold is the touch value at which the interrupt should be triggered. Everything else is the same as the GPIO interrupt example.

Projects Using ESP32 and Interrupts

There are some interesting projects done with the ESP32 and its Interrupt feature. If you want to know more about those topics, links are given below-

ESP32 Active Mode and Deep Sleep Mode Power Consumption
ESP32 Active Mode and Deep Sleep Mode Power Consumption

In this project, we will check the current consumption of widely popular Wi-Fi and Bluetooth-enabled microcontroller unit ESP32 in normal working mode and deep sleep mode.

Biometric Attendance System with Google Sheet Integration
Biometric Attendance System with Google Sheet Integration

We build a Biometric Attendance System using ESP32 and OLED Display Module which can store the attendance records in google sheets.

Supporting Files

Have any question realated to this Article?

Ask Our Community Members