Lite / Engineering Complex Systems / 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?
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.
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?
- About half
- Almost all of it
- None
- Exactly 1 ms per minute
Guess first, then reveal
If handling an event takes microseconds, the CPU is awake for a tiny fraction of each minute and can sleep the rest. A polling design must wake (or stay awake) every millisecond just to check.
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.
What the interactive shows (text version)
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.
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
Intuitive
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.
Technical
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.
Mathematical
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.
Applied
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.
Practice
Practice, not a test. Answers are checked on our server; a wrong answer gets a hint and another try. Nothing is saved.
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.
Reflect
Where have you seen a system that clearly polls (checks on a schedule) when it could have waited for an event?
Write your answer on paper or in your own notes. Lite never stores what you write.