Writing a Safe ISR
Keep interrupt handlers short and use volatile.
Writing a Safe ISR is a free Arduino & IoT Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Arduino & IoT Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
A Special Function
An ISR, or interrupt service routine, is the function that runs when an interrupt fires. It has rules that ordinary functions do not.
Keep It Short
The golden rule: make your ISR as short as possible. The main program is paused while it runs, so finish fast and get out.
Do the Heavy Work Later
Inside the ISR, just set a flag or bump a counter. Let the loop do the slow work like printing or math once it sees the flag.
void myISR() {
pressed = true;
}Shared Variables
An ISR and your loop both touch the same variable. The compiler may not expect it to change behind the loop's back, which causes subtle bugs. 🐞
The volatile Keyword
Mark any variable shared with an ISR as volatile. It tells the compiler the value can change at any moment, so always reread it.
volatile bool pressed = false;No delay Inside
Never call delay in an ISR. The timing that delay needs is itself paused during an interrupt, so it simply will not work.
Serial Is Risky Too
Avoid Serial.print inside an ISR. It relies on interrupts that are blocked while your handler runs, so output can stall or garble.
millis Freezes
Inside an ISR, millis stops advancing because its background counting is paused. Read elapsed time in the loop, not the handler.
Handle the Flag in Loop
Back in the loop, check the flag, act on it, then clear it. This is where the real response safely takes place.
if (pressed) {
pressed = false;
handlePress();
}Reading Multi-Byte Values
When the loop reads a multi-byte volatile value an ISR updates, briefly disable interrupts so you get a clean, unsplit number.
noInterrupts();
long c = count;
interrupts();Why These Rules Matter
Follow these rules and your ISR stays fast and predictable. Break them and you get freezes, lost data, or values that never seem to update.
Quick Check
Why must a variable shared between an ISR and the loop be marked volatile?
Recap
You learned to keep an ISR short, mark shared data volatile, skip delay and Serial, and do the real work back in the loop. 🎉
Frequently asked questions
Is the “Writing a Safe ISR” lesson free?
Yes — the full text of “Writing a Safe ISR” is free to read here on the web, and the Arduino & IoT Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Arduino & IoT Academy course, upgrade to CoddyKit PRO.
What will I learn in “Writing a Safe ISR”?
Keep interrupt handlers short and use volatile. You practise Arduino & IoT Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Arduino & IoT Academy?
No prior experience is required. Arduino & IoT Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Writing a Safe ISR” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Arduino & IoT Academy lesson?
Yes. Every Arduino & IoT Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.