Polling vs Interrupt-Driven Execution in Embedded C
📖 Scenario: You are working on a simple embedded system that reads a button press to toggle an LED. You want to understand the difference between polling and interrupt-driven execution.
🎯 Goal: Build two small programs: one using polling to check the button state repeatedly, and another using an interrupt to react to the button press immediately.
📋 What You'll Learn
Create a variable to represent the button state
Create a variable to represent the LED state
Write a polling loop to check the button state and toggle the LED
Write an interrupt service routine (ISR) to toggle the LED on button press
Print the LED state changes to simulate output
💡 Why This Matters
🌍 Real World
Embedded systems often need to read buttons or sensors. Polling checks repeatedly, which can waste time. Interrupts react immediately, saving power and improving responsiveness.
💼 Career
Understanding polling vs interrupts is key for embedded software engineers working on microcontrollers, IoT devices, and real-time systems.
Progress0 / 4 steps
1
Setup button and LED state variables
Create two integer variables called button_state and led_state. Initialize button_state to 0 (not pressed) and led_state to 0 (LED off).
Embedded C
Hint
Use int type and set both variables to 0.
2
Add a polling loop to check button state
Add a while(1) infinite loop. Inside it, use an if statement to check if button_state equals 1 (pressed). If pressed, toggle led_state between 0 and 1 using led_state = 1 - led_state;. Then reset button_state to 0.
Embedded C
Hint
Use while(1) for an infinite loop and toggle led_state inside the if.
3
Write an interrupt service routine (ISR) to toggle LED
Write a function called button_ISR with no parameters and void return type. Inside it, toggle led_state between 0 and 1 using led_state = 1 - led_state;. This simulates an interrupt toggling the LED immediately.
Embedded C
Hint
Define button_ISR as a void function with no parameters and toggle led_state inside.
4
Print LED state changes to simulate output
Add printf statements inside the polling loop and inside button_ISR to print "LED is ON\n" when led_state is 1 and "LED is OFF\n" when led_state is 0. This shows the LED state changes.
Embedded C
Hint
Use a helper function to print LED state and call it after toggling led_state.
Practice
(1/5)
1. Which statement best describes polling in embedded C programming?
easy
A. Polling repeatedly checks for an event in a loop.
B. Polling uses special functions to respond automatically to events.
C. Polling is more efficient than interrupt-driven execution.
D. Polling disables all hardware interrupts.
Solution
Step 1: Understand polling concept
Polling means the program continuously checks if an event has happened by running a loop.
Step 2: Compare options
Polling repeatedly checks for an event in a loop. correctly states polling as repeated checking. Options A, B, and C describe interrupts or incorrect facts.
Final Answer:
Polling repeatedly checks for an event in a loop. -> Option A
Quick Check:
Polling = repeated checking [OK]
Hint: Polling means checking repeatedly in a loop [OK]
Common Mistakes:
Confusing polling with interrupts
Thinking polling is automatic
Assuming polling disables interrupts
2. Which of the following is the correct way to declare an interrupt service routine (ISR) in embedded C?
easy
A. void ISR() { /* code */ }
B. __interrupt void ISR() { /* code */ }
C. void interrupt ISR() { /* code */ }
D. interrupt void ISR() { /* code */ }
Solution
Step 1: Recall ISR declaration syntax
In many embedded C compilers, the keyword __interrupt before the function declares it as an ISR.
Step 2: Check options
__interrupt void ISR() { /* code */ } uses __interrupt correctly. Options A, B, and D use incorrect or incomplete syntax.
Final Answer:
__interrupt void ISR() { /* code */ } -> Option B
Quick Check:
ISR declaration = __interrupt keyword [OK]
Hint: ISR functions use __interrupt keyword before return type [OK]
Common Mistakes:
Omitting __interrupt keyword
Placing interrupt keyword after void
Using plain void without interrupt
3. Consider this embedded C code snippet using polling:
while(1) {
if (button_pressed()) {
toggle_led();
}
}
What happens when the button is pressed?
medium
A. The LED never toggles because interrupts are disabled.
B. The LED toggles only after the loop finishes.
C. The LED toggles immediately when the button is pressed.
D. The LED toggles only if an interrupt triggers.
Solution
Step 1: Analyze the polling loop
The loop continuously checks if button_pressed() returns true, then calls toggle_led() immediately.
Step 2: Understand behavior on button press
When the button is pressed, button_pressed() becomes true, so toggle_led() runs right away.
Final Answer:
The LED toggles immediately when the button is pressed. -> Option C
Quick Check:
Polling triggers action immediately on event [OK]
Hint: Polling checks and acts immediately inside the loop [OK]
Common Mistakes:
Thinking toggle waits for loop end
Confusing polling with interrupts
Assuming interrupts are required
4. This code tries to use interrupts but has a bug:
A. enable_interrupts() is called inside main loop.
B. The main loop should not be infinite.
C. toggle_led() cannot be called inside ISR.
D. ISR is missing the __interrupt keyword.
Solution
Step 1: Check ISR declaration
The ISR function lacks the __interrupt keyword, so it won't be recognized as an interrupt handler.
Step 2: Review other code parts
enable_interrupts() is correctly called once; toggle_led() can be called in ISR; infinite loop is normal in embedded main.
Final Answer:
ISR is missing the __interrupt keyword. -> Option D
Quick Check:
ISR needs __interrupt keyword [OK]
Hint: ISR must have __interrupt keyword to work [OK]
Common Mistakes:
Forgetting __interrupt keyword
Thinking infinite loop is wrong
Assuming toggle_led() is invalid in ISR
5. You want to design a program that reads a sensor value only when it changes, without wasting CPU time checking constantly. Which method is best?
hard
A. Use an interrupt triggered by the sensor change.
B. Use polling to check sensor value in a tight loop.
C. Use a delay loop to check sensor every second.
D. Use a timer to poll sensor every millisecond.
Solution
Step 1: Understand the requirement
The program should react only when the sensor changes and avoid wasting CPU time on constant checking.
Step 2: Compare methods
Polling (options B, C, D) wastes CPU by checking repeatedly. Interrupt (Use an interrupt triggered by the sensor change.) triggers only on change, saving CPU.
Final Answer:
Use an interrupt triggered by the sensor change. -> Option A
Quick Check:
Interrupts save CPU by reacting only on events [OK]
Hint: Use interrupts to react only when events happen [OK]
Common Mistakes:
Choosing polling for efficiency
Using delays wastes CPU
Confusing timer polling with interrupts
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.