Understanding Interrupts in Microcontrollers (With Real Examples)

Introduction
Imagine you are doing your homework. Suddenly, the doorbell rings. You stop writing, open the door, handle the visitor, and then continue your homework exactly where you left off.
That doorbell is an interrupt.
A microcontroller works in the same way. Instead of constantly checking everything again and again, it waits until something important happens-and then it reacts immediately. That reaction mechanism is called an interrupt.
What Is an Interrupt?
An interrupt is a signal that tells the microcontroller:
“Stop what you are doing right now. Something important needs attention.”
Once the important task is done, the microcontroller returns back to what it was doing before.
Why Do We Need Interrupts?
Without interrupts, a microcontroller would have to keep checking everything repeatedly, like this:
- Is the button pressed?
- Is the timer finished?
- Did data arrive?
- Is the sensor ready?
This checking is called polling, and it wastes time and power.
Interrupts allow the microcontroller to:
- React instantly
- Save power
- Run faster
- Handle real-time events
Interrupt vs Polling
| Polling | Interrupt |
|---|---|
| Keep checking again and again | Wait until event happens |
| Wastes CPU time | CPU works efficiently |
| Slow response | Immediate response |
| Simple but inefficient | Slightly complex but powerful |
Real-Life Example
Polling Example
You keep asking your friend every 5 seconds:
“Are you ready? Are you ready? Are you ready?”
Interrupt Example
Your friend says:
“I’ll call you when I’m ready.”
Interrupts are the phone call.
How Interrupts Work Inside a Microcontroller
Here is the simple flow:
- Main program is running
- An event occurs (button press, timer overflow, data received)
- Microcontroller pauses main program
- Interrupt Service Routine (ISR) runs
- ISR finishes
- Main program resumes exactly where it stopped
What Is an Interrupt Service Routine (ISR)?
An ISR is a special function that runs when an interrupt occurs.
Rules of ISR (very important):
- Keep it short
- Do only urgent work
- Never use long delays
- Return quickly
Think of ISR as:
“Handle the doorbell fast, don’t start cooking dinner there.”
Types of Interrupts in Microcontrollers
1. External Interrupt
Triggered by something outside the microcontroller.
Example:
- Button press
- Sensor signal
- Emergency stop switch
Real use cases:
- User input
- Safety shutdown
- Wake-up from sleep
2. Timer Interrupt
Triggered when a timer reaches a certain value.
Example:
- Every 1 second
- Every 10 milliseconds
Real use cases:
- Blinking LEDs
- Scheduling tasks
- RTOS system tick
3. Communication Interrupt
Triggered when data is received or transmitted.
Example:
Real use cases:
- Serial communication
- Automotive networking
- IoT data transfer
4. Internal Interrupts
Triggered by internal events.
Examples:
- ADC conversion complete
- DMA transfer complete
- Watchdog timeout
Simple Real Embedded Example – Button Interrupt
Without Interrupt
- MCU keeps checking button state
- Wastes CPU cycles
With Interrupt
- MCU sleeps
- Button press wakes MCU
- ISR toggles LED
This is how low-power devices work.
Interrupt Priority
Imagine:
- Fire alarm
- Doorbell
Which one should be handled first?
Fire alarm.
Microcontrollers also have interrupt priorities:
- High-priority interrupts interrupt low-priority ones
- Critical events always win
What Happens If Two Interrupts Occur Together?
The microcontroller:
- Handles higher priority interrupt first
- Stores the other one
- Executes it later
This ensures no important event is missed.
Common Beginner Mistakes With Interrupts
- Writing long code inside ISR
- Using delay functions inside ISR
- Calling blocking functions
- Forgetting to clear interrupt flags
These mistakes cause:
- System freeze
- Missed interrupts
- Unstable behavior
Best Practices for Using Interrupts
- Keep ISR very short
- Use flags to communicate with main code
- Process heavy work in main loop or RTOS task
- Assign priorities carefully
Interrupts in RTOS Systems
In RTOS:
- ISR signals tasks
- Tasks do the heavy processing
- RTOS manages priorities and timing
Interrupts + RTOS = powerful real-time systems.
Where Interrupts Are Used in Real Products
- Automotive ECUs (CAN, sensors, safety)
- Washing machines
- Medical devices
- IoT sensors
- Drones and robotics
Interrupts are everywhere.
Why Interviewers Love Interrupt Questions
Interrupts test:
- Real-time thinking
- Hardware understanding
- Embedded fundamentals
If you understand interrupts, you truly understand embedded systems.
Conclusion
An interrupt is simply the microcontroller’s way of saying:
“I’ll stop my work only when something important happens.”
By using interrupts, embedded systems become faster, smarter, and more efficient. For beginners, mastering interrupts is one of the biggest milestones in embedded learning-and a foundation for RTOS, automotive systems, and real-world product development.
