Skip to content
Tutorials Center
My learning

Topics

Lesson 34 of 37 · Debugging, Testing and Low Power

Low-Power Design for Embedded Systems

Learn how to make microcontroller devices last on a battery: sleep modes, wake-up sources, duty cycling, tickless idle, clock gating, and battery life calculations with C examples.

A wearable, a wireless sensor in a field, a smart lock: many embedded devices run on a small battery and must last months or years. In these products, the most important number is not CPU speed but average current. The difference between a careless design and a careful one is often a factor of 100 in battery life, with the same hardware.

This lesson explains where a microcontroller spends power, how sleep modes work, why duty cycling is the main trick, and how to calculate and verify battery life.

Where the power goes

Two components make up a chip's consumption:

  • Dynamic power is spent when transistors switch. It grows with clock frequency and with the square of the supply voltage: roughly P = C * V^2 * f. Halving the clock halves this part; lowering the voltage helps even more.
  • Static (leakage) power flows even when nothing switches. It grows with temperature and gets worse in smaller manufacturing processes. Deep sleep modes exist to cut it, by gating the clocks and removing power from parts of the chip.

Add the peripherals (radio, ADC, sensors, display, LEDs) and the board: a regulator with a high quiescent current, a pull-up resistor that holds a pin at 0 V through 10 kΩ (0.33 mA at 3.3 V, hundreds of times more than a sleeping MCU), a floating input that oscillates. In many designs the MCU core is not the main consumer. A radio transmitting may draw 10 to 100 mA, the core at 48 MHz perhaps 10 mA, and a deep-sleep core 1 µA or less.

Sleep modes

Most microcontrollers offer several levels, trading wake-up time for savings. Names vary by vendor (Sleep, Stop, Standby, Deep Sleep, Hibernate), but the pattern is the same:

Mode (typical) CPU Clocks RAM Wake sources Current Wake time
Run on on kept n/a mA range n/a
Sleep (light) halted peripherals running kept any interrupt hundreds of µA to mA under a µs
Stop / Deep sleep halted most stopped, slow clock (32 kHz) may run kept RTC, pin, a few peripherals a few µA microseconds
Standby / Shutdown off off lost (or a few bytes kept) reset, wake pin, RTC sub-µA to a µA like a reboot (ms)

On ARM Cortex-M the two key instructions are WFI (wait for interrupt) and WFE (wait for event). The CPU halts until a pending, enabled interrupt wakes it; then the ISR runs and execution continues after the WFI. The vendor library selects deeper modes by setting control bits before the WFI.

C
// Read-only Cortex-M sketch: sleep until something happens
while (1) {
    if (work_pending) {
        handle_work();          // do all the work first
    } else {
        __WFI();                // CPU sleeps until the next interrupt
    }
}

There is a classic race here: if the interrupt fires between the check of work_pending and the __WFI(), the CPU sleeps even though work is waiting. Cortex-M handles this elegantly: a pending interrupt makes WFI return at once, even if its ISR ran just before (with interrupts masked via PRIMASK around the check, the pending interrupt will still wake the WFI and then run). Learn the exact rule for your chip, because on some architectures you must enable interrupts and sleep atomically in a single instruction pair.

Duty cycling: the main idea

A sensor node that measures once a minute does useful work for a few milliseconds and sleeps for the rest. Its average current is the time-weighted mix of the two states:

TEXT
I_avg = (I_active * t_active + I_sleep * t_sleep) / (t_active + t_sleep)

Because the active current is often a thousand times the sleep current, the duty cycle (the fraction of time awake) controls everything. The strategy is called race to sleep: finish the work quickly, even at a higher clock speed, and go back to sleep. The energy is power times time; a faster clock draws more power but finishes sooner, and the sleep that follows is nearly free.

Battery life follows from the capacity: hours = capacity_mAh / I_avg_mA. Real batteries do not deliver their nameplate capacity at high drain or low temperature, and self-discharge matters over years, so treat the result as an upper limit and apply a safety factor.

C Battery life from the duty cycle

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Look at how the life stretches: waking every second gives about seven weeks, every ten seconds more than a year, once a minute about four years, and at ten minutes the sleep current dominates (self-discharge of the cell would limit it in practice). The lesson is that both terms matter: cut the active time, cut the wake frequency, and make sure the sleep current really is as low as the datasheet says.

What wakes the chip, and what it costs

Every wake-up costs energy: the oscillator must start (a crystal can take milliseconds to stabilize), the clock tree is reconfigured, and the ISR runs. A device that wakes 1,000 times per second for a 1 ms tick pays that overhead 1,000 times, even when there is nothing to do. This leads to the tickless idle technique that some RTOS kernels offer (FreeRTOS configUSE_TICKLESS_IDLE): when all tasks are blocked, the kernel works out when the next timeout is due, programs a low-power timer for exactly that moment, and stops the periodic tick until then.

The simulation compares three designs over 10 seconds for a device that must do 2 ms of work every 100 ms: a polling loop that never sleeps, a design that sleeps but wakes on a 1 kHz tick, and a tickless design that wakes only for real events. Every wake costs 30 µs at the active current (oscillator start and context restore).

C Polling vs periodic tick vs tickless idle

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

The numbers show the order of magnitude each technique buys: the first design drains the cell in about a day, the second lasts weeks, and tickless idle more than doubles that again. Real products measure it: put a current meter or a power analyzer (Nordic PPK2, Joulescope, or a shunt resistor with a scope) in series with the supply and look at the waveform, not just the average. The first measurement of a "sleeping" device often reveals a forgotten peripheral or a pin that keeps something on.

A practical checklist

  1. Sleep whenever idle. Put __WFI() (or the vendor's low-power call) in the idle task or main loop, and prefer deeper modes when the wake latency allows.
  2. Run at the lowest clock and voltage that meets the deadline, or use race to sleep if the work is bursty. Measure both; the winner depends on the chip.
  3. Switch off what you do not use: gate peripheral clocks, power down the ADC between samples, disable the radio between packets, turn off the sensor's supply with a GPIO or load switch.
  4. Handle the pins. Configure unused pins as analog or output low; avoid floating inputs, and avoid pull-ups with a path to ground. Check external devices' sleep currents too.
  5. Use event-driven design. Interrupts, DMA and hardware timers let the CPU stay asleep while a peripheral does the work (for example, an ADC with DMA that fills a buffer and wakes the CPU only when it is full).
  6. Batch the work. Collect ten readings and transmit once; the radio's startup cost is shared across them.
  7. Use a low-power clock. A 32.768 kHz crystal and RTC can keep time at under 1 µA while everything else sleeps.
  8. Check what the regulator wastes. A linear regulator drops the excess voltage as heat; a buck converter is more efficient at high load, but its quiescent current might be too high for sleep. Choose by the profile of your load.
  9. Verify after every feature. A new driver may keep a clock enabled. Add a power regression test to your hardware-in-the-loop rig (see testing embedded code).

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. A device draws 10 mA when active and 5 µA asleep, and is active 1% of the time. Roughly what is its average current?

    0.01 * 10 mA = 100 µA, plus 0.99 * 5 µA ≈ 5 µA, so about 105 µA.
  2. 2. What does 'race to sleep' mean?

    Energy is power times time; a short burst followed by deep sleep usually uses less than a long, slow one.
  3. 3. Why does tickless idle save power?

    The kernel programs a timer for the next real deadline and sleeps until then, avoiding thousands of useless wake-ups.
  4. 4. A pull-up resistor of 10 kΩ holds a pin at 0 V through a switch while the device sleeps at 3.3 V. What is the problem?

    3.3 V / 10 kΩ = 0.33 mA, which can be a hundred times the whole chip's sleep current.

Summary

  • Battery life depends on average current; dynamic power scales with C * V^2 * f and leakage dominates deep sleep.
  • Use sleep modes (WFI/WFE and vendor deep sleep), choose the depth by wake time and what must be kept alive.
  • Duty cycling and race to sleep dominate: cut both the active time and the wake frequency, and use tickless idle.
  • Switch off unused peripherals, handle pins, batch work and always measure the real current waveform.

Next: Project: A Traffic Light State Machine