Low-power firmware design: finding where the battery really goes
How to cut power consumption in battery-powered embedded devices through firmware: measuring average current, sleep states, radio timing and Zephyr RTOS.
Your device works. The sensor reads, the radio talks, the app shows the data. Then the battery dies in weeks instead of months.
The usual reflex is hardware: a bigger cell, a lower-power chip, a different sensor. Sometimes that's right. More often the hardware is fine and the firmware is spending energy nobody asked it to spend.
A device can be built from excellent low-power parts and still drain fast. It talks more often than it needs to. It leaves a peripheral running. It wakes up to check something that hasn't changed. None of that shows up in a datasheet. All of it shows up in the battery.
This post is the way I approach it: understand where the charge goes, fix the biggest consumer first, and measure everything.
Average current is the number that matters#
Peak current gets the attention. Average current decides battery life.
A device moves between states, and each state draws a different current. Here's a made-up but realistic example:
| State | Current | Time per second |
|---|---|---|
| Sleep | 10 µA | 993 ms |
| Processing | 2 mA | 5 ms |
| Radio transmit | 8 mA | 2 ms |
Average current is each state's current multiplied by its time, added up and divided by the total time:
I_avg = (I_sleep × t_sleep + I_proc × t_proc + I_tx × t_tx) / T
= (0.01 mA × 993 ms + 2 mA × 5 ms + 8 mA × 2 ms) / 1000 ms
= (9.93 + 10 + 16) / 1000
≈ 36 µALook at where those 36 µA come from:
- The radio is on for 0.2% of the time and costs 16 µA, the biggest share.
- Processing costs 10 µA.
- Sleep, the state the device is in 99.3% of the time, still costs about 10 µA.
Two lessons fall out of that small table. Time in a high-current state matters as much as the current itself. Halving the transmit time saves 8 µA. And sleep current is never free. Over 99% of the device's life is spent there, so a few microamps of leakage add up to a big slice of the battery.
On a coin cell with roughly 180 mAh you can really use, 36 µA lasts about 7 months. Get it to 18 µA and you have over a year. Same hardware.
Ask the firmware questions first#
Before touching the schematic, ask what the firmware is doing:
- Is the processor waking up more often than the application needs?
- Is any peripheral (UART, ADC, a sensor, an LED) on when nothing is using it?
- Does the radio talk at a rate the application actually needs?
- Does the device ever reach its lowest sleep state, or does something keep it awake?
- Is a background task, timer or log message quietly preventing sleep?
These turn "the battery is bad" into problems you can measure. The goal is not the lowest possible current. It's no wasted current, while the device still does its job on time and reliably.
The radio: talk less, and only when it matters#
On a Bluetooth LE device, the radio is usually the biggest single consumer. Two choices dominate.
Connection or advertising?#
A connection gives you two-way communication, acknowledged delivery and a predictable schedule of connection events. You need it for commands, configuration, firmware updates and anything that must arrive.
Advertising broadcasts data that anyone nearby can pick up. No connection, no acknowledgement. For a sensor that just reports a value every so often, it can be all you need.
Neither is always cheaper. Cost depends on the interval, payload size, transmit power and how hard the receiver has to work. The question to ask is simple: does this device really need a connection, or would a broadcast do?
Make the interval adapt#
A fixed interval is easy to write and wasteful to run. When something is happening, report often. When nothing is, slow right down.
I saw this on a smart sensor mattress I worked on. Its advertising adapts to whether someone is in the bed. The empty bed, which has nothing new to say, saw the largest drop in power. Together with other firmware work, that took estimated battery life from months to years.
The trade-off is latency. A slower interval means older data and a device that takes longer to find. Pick intervals that meet the application's freshness requirement, not intervals that feel safe.
If you want to see how much an interval actually costs, I built a BLE advertising calculator. Move the slider and watch the average current and battery life change.
The processor: wake for a reason, then go back to sleep#
The processor should only run when there is work to do. The usual offenders:
- Polling loops. Checking a flag in a loop keeps the core awake. Use an interrupt or a semaphore and let the thread block.
- Busy waits.
k_busy_wait()burns current.k_sleep()lets the kernel idle. - Timers nobody needs. Every periodic timer is a wake-up. Merge them, slow them down, or remove them.
- Redundant work. Filtering or formatting data that never leaves the device.
In Zephyr, when no thread is ready to run, the idle thread puts the CPU into a low-power state. You don't call "sleep" yourself. You make sure your threads are blocked, sleeping or waiting, so the kernel can do it for you.
Peripherals: off unless in use#
A sensor, an ADC or a UART that stays powered between readings can easily cost more than the processor does. Zephyr's device runtime power management lets a driver suspend a peripheral when nothing holds a reference to it.
Here's the shape of a periodic sensor loop that powers the sensor only while reading it, and slows down when nothing is happening. It's a sketch, not a drop-in driver: the sensor0 alias and the helper functions are placeholders for your board.
#include <zephyr/kernel.h>
#include <zephyr/drivers/sensor.h>
#include <zephyr/pm/device_runtime.h>
static const struct device *sensor = DEVICE_DT_GET(DT_ALIAS(sensor0));
void sensor_thread(void)
{
while (1) {
/* Power the sensor up only for the reading. */
pm_device_runtime_get(sensor);
sensor_sample_fetch(sensor);
struct sensor_value value;
sensor_channel_get(sensor, SENSOR_CHAN_AMBIENT_TEMP, &value);
pm_device_runtime_put(sensor);
bool changed = process_reading(&value); /* your logic */
if (changed) {
send_update(&value); /* radio only when it matters */
}
/* Report often while things change, rarely when they don't. */
k_sleep(changed ? K_SECONDS(1) : K_SECONDS(30));
}
}And the matching configuration:
CONFIG_PM_DEVICE=y
CONFIG_PM_DEVICE_RUNTIME=y
# Production builds: no console or logging unless you need them in the field
CONFIG_SERIAL=n
CONFIG_LOG=nAn RTOS doesn't make a design efficient on its own. Too many threads, frequent wake-ups and drivers left in their default state can drain a Zephyr device just as fast as a bare-metal one. The structure is what saves power.
The quiet battery killers#
These are the ones that don't show up in a code review:
- A UART left enabled. A UART with its receiver on can keep high-frequency clocks running and stop the chip from reaching its deepest sleep. A debug console is often the first thing to switch off in a production build.
- Logging. Log messages wake the processor and keep the UART busy. Fine on the bench, expensive in the field.
- Floating or misconfigured pins. An input left floating, or a pin pulling against an external resistor, leaks current all day.
- The debugger. With a debug probe attached, many chips never enter their deepest sleep. Disconnect it before you measure.
- Pull-ups on idle buses. I²C pull-ups draw current whenever a line is held low. Check what the bus is doing while the device sleeps.
Measure it. Never assume.#
An optimisation that looks right in the code can do nothing on the board, because something else keeps waking the processor. The only proof is a measurement.
A power analyser such as the Nordic Power Profiler Kit II shows current over time, so you can see:
- the real sleep floor, and whether it stays there;
- each wake-up and how long it lasts;
- radio peaks and their spacing;
- wake-ups you didn't expect, which are usually the bug.
Measure under real conditions: idle, normal sensing, transmitting, busiest case, and a long run that covers every periodic task. Write down the setup (supply voltage, firmware build, what's connected) so you can compare one change against the next. Change one thing at a time.
Every saving has a price#
| Change | What you gain | What it costs |
|---|---|---|
| Report less often | Less radio time | Older data |
| Sleep longer | Lower average current | Slower response |
| Event-driven code | No wasted polling | More complex event handling |
| Sample sensors less often | Less activity | Lower time resolution |
| Advertising instead of a connection | Simpler, often cheaper | No delivery acknowledgement |
There is no single best setting. An environmental sensor can live with data a few minutes old. A medical device might not. Tune for the device's real workload and its real requirements, not for the lowest number on the power analyser.
A checklist before you ship#
Hardware
- Unused peripherals disabled?
- Regulator efficient at your sleep current, not only at full load?
- No floating inputs?
Firmware
- No polling loops or busy waits?
- Every periodic timer justified?
- Threads blocked so the idle thread can sleep?
- Console and logging off in the production build?
Radio
- Connection really needed?
- Interval adapts to what's happening?
- Latency and packet loss still acceptable?
Measurement
- Sleep floor measured with the debugger disconnected?
- Every operating state measured?
- Long run done with real workloads?
- Battery estimate checked against a real device?
The short version#
Battery life is a firmware problem more often than a hardware one. Know your average current. Find the biggest consumer. Fix it, measure again, repeat.
A bigger battery hides the waste. Good firmware removes it.
Further reading#
- Zephyr power management
- Zephyr device runtime power management
- Nordic Power Profiler Kit II
- Bluetooth Core Specification
- My BLE advertising calculator for estimating advertising power and battery life
Get the next post by email
Firmware, Bluetooth LE and health-tech research notes. Only new posts. One click to leave.
Your email is used only for this and never shared.