Interrupts, Timers and FreeRTOS on ESP32: the short answer
An interrupt makes the processor stop what it is doing and run a short function, the interrupt service routine (ISR), as soon as an event happens, such as a pin changing. Use attachInterrupt(digitalPinToInterrupt(pin), isr, RISING) for pulses that are too fast or too short to catch by polling, such as an encoder or a flow sensor.
- Level
- Advanced
- Time
- About 6 min
- Topic
- Microcontrollers
Capture fast events with interrupts, run code on precise timers and split work into FreeRTOS tasks.
Before this: Firmware Build, Logs & Fault Isolation, PWM Timing & Actuator Interfaces
An interrupt makes the processor stop what it is doing and run a short function, the interrupt service routine (ISR), as soon as an event happens, such as a pin changing. Use attachInterrupt(digitalPinToInterrupt(pin), isr, RISING) for pulses that are too fast or too short to catch by polling, such as an encoder or a flow sensor. Keep ISRs tiny: increment a counter or set a flag, then return. Never use Serial, delay() or Wi-Fi inside one. Variables shared with an ISR must be declared volatile, and on the ESP32 the ISR should be marked IRAM_ATTR so it runs from fast internal memory.
Hardware timers call an ISR at an exact interval, independent of what loop() is doing. They are the right tool for sampling a sensor at a steady rate, for example 1,000 times a second for vibration, where loop() timing would jitter.
The ESP32's Arduino core already runs FreeRTOS, a small real-time operating system. You can create tasks with xTaskCreatePinnedToCore, each with its own loop, priority and stack, and pass data between them safely with queues. A common split: a high-priority task reads sensors on a timer and pushes readings into a queue, while a lower-priority task sends them over Wi-Fi. If Wi-Fi stalls for a second, sampling carries on. Watch out for shared data: protect it with a mutex or a queue, never with ordinary global variables.
Keep interrupt routines short
An interrupt service routine runs at once and blocks other work, so it should do the minimum: record the event, set a flag or put a value in a queue, and return. Do not print, wait or run long calculations inside it. On the ESP32 the routine is marked IRAM_ATTR so it is in fast memory, and FreeRTOS provides special versions of its calls for use inside an interrupt, such as xQueueSendFromISR.
A button interrupt will trigger several times from contact bounce, so ignore further triggers for a few milliseconds.
Tasks and shared data
FreeRTOS lets you run several tasks, each with its own priority and stack. A task that waits on a queue or a delay uses no processor time, so other tasks run. Give each task enough stack, since an overflow causes random crashes, and check the remaining stack with the high-water mark call.
When two tasks share data, protect it. A mutex lets one task use a resource at a time, and a queue passes values safely between tasks. A priority that is too high for a task that never waits starves the others and can trigger the watchdog.
Timers
Hardware timers give regular, accurate timing without blocking, for example sampling a sensor at exactly 1 kHz. Use them to trigger a task rather than doing the work in the timer routine.
- Printing or waiting inside an interrupt routine.
- Giving a task too little stack and getting random crashes.
- Sharing data between tasks without a queue or a mutex.
Practise this lesson with Pro
The explanation above is free for everyone. The practice for Advanced lessons in every domain are part of the Pro plan at ₹999 a month.
With Pro you also get: a worked example, a hands-on task, a 3-question check.
