C12 · Lesson 42 · Power budgets and sleep
Making a battery last months instead of days
A sensor that reports every few seconds spends almost all its life waiting. If the processor waits by running a loop, it burns its full current the whole time, and a coin cell is flat in days. If it sleeps between readings, the same battery lasts months; in deep sleep, years. Battery-powered firmware is designed around that fact: do the work in short bursts, then sleep until a timer or a pin wakes you. The arithmetic is simple, an average weighted by time, and it shows at once which part of the design is eating the battery.
- The program reads a sensor every 100 ms. Between readings it either waits busily (
MODE 0), sleeps with__wfi()(MODE 1), or deep-sleeps (MODE 2). The power panel shows the average current and how long a 220 mAh coin cell would last. - Run all three: about 2.05 mA (4.5 days), 0.55 mA (17 days) and 0.052 mA (175 days). The work itself, a 10 µs conversion ten times a second, barely registers.
- In deep sleep the timer's own 50 µA is now most of the total: the next saving would come from a lower-power timer, not from the code.
Predict
A device draws 2 mA while awake and 0.002 mA asleep, and it's awake 1% of the time. What's its average current?
About 0.022 mA: . The 1% awake still dominates, ten times the sleep current, so shortening the awake time, or waking less often, is where the next saving lies.
Name it
Average current
Each state contributes its current times the fraction of time spent in it:
counting the processor's mode, every peripheral that's running, and the loads: an LED lit at 3.9 mA can outweigh the whole chip. A battery of capacity Q (in mAh) lasts
in hours (in practice somewhat less, because capacity falls at low temperatures and high currents).
This board
| State | Current |
|---|---|
| CPU running | 2 mA |
CPU asleep (wfi) | 0.5 mA |
deep sleep (SYS_SLEEP = 1, then wfi) | 0.002 mA |
| timer running | +0.05 mA |
| ADC converting | +0.6 mA for its 10 µs |
| UART, I²C or SPI transferring | +0.1 to 0.15 mA while busy |
| an LED lit | +3.9 mA |
Designing for it
- Sleep whenever there's nothing to do; let interrupts (a timer, a pin, a byte) wake the processor.
- Wake as seldom as the job allows, and finish quickly.
- Turn off peripherals when not in use; light LEDs briefly, or not at all.
- Batch work: one wake-up that does three things beats three wake-ups.
- In MicroPython,
time.sleep_ms()andmachine.lightsleep()let the processor sleep; a loop that spins checking the clock never does.
Worked example
How long does a 220 mAh coin cell last at 0.55 mA, and at 0.052 mA?
Show the solution
- At 0.55 mA.
- At 0.052 mA.
Answer about 17 days at 0.55 mA, and about 176 days at 0.052 mA.
Worked example
The device flashes a 3.9 mA LED for 20 ms every 2 s as a heartbeat. What does that add to the average?
Show the solution
- Average the flash over its period.
- Compare. That's almost as much as the whole deep-sleep design.
- A lighter heartbeat. A 5 ms flash every 10 s would add 0.002 mA.
Answer 0.039 mA.
Worked example
Each measurement wakes the board for 5 ms at 2.05 mA from 3.3 V. How much energy does it cost?
Show the solution
- Energy per measurement.
- What a coin cell holds. A 220 mAh coin cell holds about 2.6 kJ at 3.3 V, enough for some 77 million such measurements, if sleeping cost nothing.
- In practice. The sleep current between measurements usually dominates.
Answer 33.8 µJ per measurement.
Check
A device draws 2 mA awake and 0.05 mA asleep, and it's awake 10% of the time. What's its average current?
Write units the way you normally would: 1.5 mA, 1500 µA and 0.0015 A are the same answer.
Check
A 1000 mAh battery supplies an average of 0.05 mA. How many days does it last?
Check
An LED drawing 3.9 mA flashes for 5 ms every 10 s. What does it add to the average current?
Write units the way you normally would: 1.5 mA, 1500 µA and 0.0015 A are the same answer.
Check
The ADC draws an extra 0.6 mA for each 10 µs conversion, and the program converts 100 times a second. What does that add to the average current?
Write units the way you normally would: 1.5 mA, 1500 µA and 0.0015 A are the same answer.
Check
A device wakes 10 times a second and is awake 0.5 ms each time. What fraction of the time is it awake?
Check
A battery sensor in deep sleep draws 0.052 mA, of which 0.05 mA is its wake-up timer. What would most extend the battery's life?
Check
This logger reads the sensor every 100 ms but waits busily in between, drawing over 2 mA. Make it sleep between readings, keeping the readings the same.
C on the course's RISC-V board (10 MHz, no floating point). #include "board.h" for the registers; print(x) shows a number. Tab indents; press Esc and then Tab to move on.
Check
This blinker waits by spinning on the clock, keeping the processor awake. Make it sleep instead, keeping the blink the same.
MicroPython on the course's board: machine (Pin, PWM, ADC, DAC, UART, I2C, SPI, Timer, WDT) and time. Each call into the hardware takes about 40 µs. Tab indents; press Esc and then Tab to move on.
Check
A 1000 mAh battery powers a load averaging 0.052 mA.
(a)
How long does it last?
(b)
In days?
Check
A device sleeps at 0.052 mA and wakes to 2.05 mA for 50 ms every 10 s.
(a)
What is its average current?
Write units the way you normally would: 1.5 mA, 1500 µA and 0.0015 A are the same answer.
(b)
What share of the average comes from waking?
Explain why
Why does a battery-powered device's lifetime depend far more on how it waits than on how fast its code runs?
A typical sensor device is idle almost all the time: it does a few milliseconds of work every second or every minute. Its average current is each state's current weighted by the time spent in it, so the waiting state's current, multiplied by nearly all the time, dominates. Waiting in a loop keeps the processor at full current; sleeping cuts that by four times here, and deep sleep by a thousand. Making the code twice as fast only halves a term that was already tiny. Only once the sleep current is minimized do the awake time, the wake-up rate and the peripherals' standby currents become worth optimizing.
Practice
Keep practicing
10 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.