Arduino Interrupt Handling
Watch a microcontroller pause the main loop, jump to an ISR, set a flag, return, and safely handle the event.
Arduino Interrupt Handling
Step through how a microcontroller pauses the main loop, saves context, jumps to an ISR, sets a flag, returns, and safely handles the work later.
Arduino Interrupt Workbench
Goal
See the exact handoff between main code, hardware event, ISR, stack, and safe flag handling.
Try First
Press Trigger INT0, then Step through the six stages. Watch the stack and code highlight.
Watch
Long ISRs, bouncing buttons, and high event rates create missed events or blocked timing.
Why It Matters
IoT devices use interrupts for buttons, encoders, timers, radio pins, sleep wakeups, and fast sensors.
Interrupt Lifecycle
The event line, CPU state, stack context, ISR code, and main-loop flag handling stay synchronized.
Execution Evidence
The selected stage explains what changes inside the CPU and sketch.
Latency Check
Latency is event edge to first useful ISR instruction.
6.0 usISR Fit
Short ISRs keep other timing work responsive.
ShortCode Safety
Checks Arduino-specific interrupt rules.
Safe patternBeginner Ramp
Polling asks the same question again and again: did the button change yet? An interrupt lets hardware raise its hand. The CPU finishes its current instruction, remembers where it was, runs a short ISR, then returns to the main loop.
Lifecycle Sequence
- Main loop runs ordinary sketch code.
- Hardware detects an enabled edge or timer event.
- CPU finishes the current instruction and saves return context.
- CPU jumps to the interrupt vector and runs the ISR.
- ISR sets a volatile flag or increments a small counter.
- RETI restores context and main loop handles the work.
Arduino Rules
- Use `volatile` for variables changed inside an ISR.
- Keep the ISR short; set a flag and leave.
- Avoid `delay()` and `Serial.print()` inside ISRs.
- Protect multi-byte shared reads with `noInterrupts()` and `interrupts()` when needed.
Formula Reference
- Cycle time at 16 MHz = 62.5 ns.
- Teaching latency = finish current instruction + vector/context overhead.
- CPU time used by ISR = ISR duration x event rate.
- Event period = 1000 ms / event rate in hertz.
Interrupt Pins
On Arduino Uno/Nano, external INT0 is D2 and INT1 is D3. Use `digitalPinToInterrupt(pin)` so the sketch remains clearer and more portable. Many ESP32 boards can attach interrupts to most GPIO pins, but electrical constraints still matter.
Safe Pattern Code
volatile bool buttonFlag = false;
void setup() {
pinMode(2, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(2), buttonISR, FALLING);
}
void loop() {
if (buttonFlag) {
buttonFlag = false;
handleButtonPress();
}
}
void buttonISR() {
buttonFlag = true;
}
Practice 1
Use Button INT0. Set debounce to 0 ms. Why does the warning say a single press may create multiple events?
Practice 2
Use Encoder Pulse. Raise event rate above 1000 Hz and ISR work above 100 us. What starts to fail first?
Practice 3
Use Bad Long ISR. Which Arduino rules are violated, and where should the real work move?