School/□ Systems Engineer/Week 33
Polling or interrupts? Latency, energy and shared data
Checking a button a million times a second never misses a press. Why is it usually the wrong design?
About 40 min · 3 goals · Checked by automated tests
1.Why should I care?
A rover's wheel encoder, a greenhouse's door sensor and a lift's safety edge all need fast responses on a limited power budget. How firmware waits for events decides whether the processor can sleep, and therefore whether the battery lasts.
- ◇ Compare polling and interrupts by latency, CPU load and energy.
- ◇ Estimate the worst-case response time of a polling loop and of an interrupt.
- ◇ Protect data shared between an interrupt and main code.
2.Predict
A sensor event happens about once a minute. Polling every 1 ms keeps the CPU awake. Interrupts let it sleep between events. Roughly how much of the time can the interrupt design sleep?
Guess first. Being wrong here is how the surprise works.
3.Discover
In the Robotics Workbench, set up a sensor that reacts to an event (a bump or line crossing). Compare a loop that checks the sensor on a fixed schedule with an event-driven reaction. Note the reaction delay and how often the controller has to run.
Can't use the interactive? Read a description instead
In the Robotics Workbench the learner compares a controller that checks a sensor on a fixed schedule with one that reacts to the event directly. The scheduled check reacts within one period; the event-driven reaction is faster and lets the controller do nothing between events, which is what allows a real processor to sleep.
4.Checkpoint: guided practice
Check what you just discovered before going on. Hints appear after a wrong answer.
Practice 1
A battery-powered door sensor must react within 10 ms to a door opening, which happens a few times a day. Which design?
5.Understand
Polling
The main loop repeatedly reads a status bit. Simple, deterministic and easy to debug. Worst-case latency is the loop period (plus the work done each loop). Energy cost: the CPU wakes every period whether or not anything happened. Good when events are frequent and regular, or when you need a fixed sampling rate anyway (a control loop at 1 kHz).
Interrupts
Hardware detects the event and forces the CPU to run a short handler (an ISR). Latency is microseconds (entry cycles plus any time interrupts are disabled). Between events the CPU can sleep. The price is complexity: handlers interrupt main code at unpredictable moments, which creates races on shared data.
Protecting shared data
Rules of thumb: keep ISRs short (set a flag, copy a value, leave); declare shared variables volatile; read multi-byte values with interrupts briefly disabled or with a consistency check; use a ring buffer with one writer and one reader. Never call slow or blocking functions inside an ISR.
Hybrid designs
Many systems use an interrupt to wake from sleep, then poll briefly while active, or a timer interrupt that runs a fixed-rate control loop. The choice is a budget trade: latency target, energy per event, CPU time left for other work, and how much complexity you can verify.
Explain it another way
Polling is checking your letterbox every five minutes. Interrupts are a doorbell: you can nap until it rings, but it might ring while you're carrying hot soup.
Polling latency ≤ T_poll + T_loop. Interrupt latency = hardware entry (≈ 12–20 cycles on many cores) + longest interrupts-disabled section + preceding higher-priority ISRs. CPU load for polling = T_check/T_poll.
Average power, interrupt design: P = P_sleep + E_event·rate. Polling: P = (T_check/T_poll)·P_active + (1 − T_check/T_poll)·P_sleep. With T_check = 5 µs, T_poll = 1 ms, P_active = 30 mW, P_sleep = 0.1 mW: polling ≈ 0.25 mW versus about 0.1 mW for rare events.
Wearable devices use interrupt pins from accelerometers to wake the main processor only when motion is detected, so they can last days or weeks on a small battery.
Worked example 1
A polling loop runs every 2 ms and takes 0.3 ms per pass. What is the worst-case delay before an event is noticed?
The event may occur just after a check: up to 2 ms until the next check starts, plus up to 0.3 ms to reach the check in the loop. About 2.3 ms worst case.
Common mix-ups
- Interrupts are always better than polling.
✓ Polling is simpler and deterministic; it is often right for fixed-rate control loops or frequent events. - A variable shared with an ISR is safe if it's only one line of code.
✓ One line can compile to several instructions; protect shared data explicitly.
6.Practice on your own
Practice, not a test. Wrong answers get a hint and another try.
Practice 2 · a fresh version every time
A field sensor has a 5 Wh battery. It is active 1% of the time drawing 4.5 W, and asleep the rest of the time drawing 1 mW. Ignoring battery ageing and temperature, how many hours does the battery last? (3 significant figures.)
Practice 3
Which is good practice inside an interrupt handler?
Practice 4
A poll check takes 5 µs and runs every 1 ms. What percentage of CPU time does polling use?
7.Apply it
For the elevator Build's door edge sensor, write a short design note: polling or interrupt, the worst-case latency, the energy impact, and how the shared 'door blocked' flag is protected.
8.Reflect
Concepts: Interrupts and event-driven code · Real-time computing on robots · Electrical power and energy budgets