Power supplies and batteries
LDOs and switching regulators, battery chemistries, battery-life budgets and brown-outs.
Where the volts come from
Every board turns some input (USB 5 V, a battery, a 12 or 24 V supply) into the rails its chips need: 3.3 V, 1.8 V, 5 V. Firmware engineers rarely design the power stage, but they constantly live with its consequences: battery life, brown-outs, noise on the ADC, and the board resetting when a motor starts.
Linear regulators (LDOs)
A linear regulator drops the extra voltage across a transistor and turns it into heat. It is simple, cheap, small and quiet (no switching noise), which makes it ideal for analog and RF rails.
Its weakness is efficiency. The current in equals the current out, so efficiency ≈ Vout / Vin, and the rest is heat:
P_heat = (Vin − Vout) × I
From 12 V to 3.3 V at 200 mA: (12 − 3.3) × 0.2 = 1.74 W of heat, which needs a large package or a heatsink, and 73 % of the battery thrown away. From 3.7 V (a Li-ion cell) to 3.3 V the same LDO is 89 % efficient: fine.
An LDO ("low dropout") still works when the input is only slightly above the output. The dropout voltage is that minimum difference, typically 100 to 300 mV. Below it, the output follows the input down.
Switching regulators (buck, boost)
A switching regulator chops the input with a transistor and smooths it with an inductor and capacitors. Energy is transferred, not burned, so efficiency is typically 85 to 95 %, almost regardless of the voltage difference.
- Buck steps down (12 V to 3.3 V).
- Boost steps up (one AA cell at 1.2 V to 3.3 V).
- Buck-boost does either, for a battery that starts above the output and ends below it.
The costs: switching noise (ripple and radiated interference at the switching frequency), more parts, careful board layout, and quiescent current. A regulator's own current draw with no load matters enormously for battery devices that sleep: a regulator taking 100 µA at idle can drain a battery faster than the sleeping microcontroller, which may need only 2 µA.
Batteries
| Chemistry | Nominal voltage | Notes |
|---|---|---|
| Alkaline AA | 1.5 V, falling to ~1.0 V | Cheap, high capacity at low current, poor at high current |
| Li-ion / Li-Po | 3.7 V (4.2 V full, ~3.0 V empty) | Rechargeable; needs a charger IC and protection against over-discharge |
| LiFePO4 | 3.2 V, very flat | Safer, long cycle life, lower energy density |
| Lithium coin cell (CR2032) | 3.0 V | ~220 mAh, but only a few mA: high current pulses collapse its voltage |
| Li-SOCl2 (primary) | 3.6 V | Very long shelf life for 10-year meters; needs a capacitor for radio bursts |
Capacity in mAh is measured at a gentle current; at high current, or in the cold, you get less. A Li-ion cell's voltage falls as it discharges, so its voltage tells the state of charge only roughly, and only at rest: a fuel-gauge IC (coulomb counting) does better.
Battery life budget
Battery-powered devices spend most of their time asleep, so average current is what matters:
#include <stdio.h>
/* One duty cycle of a sensor node: sleep, wake, measure, transmit. */
typedef struct { const char *what; double ma; double ms; } phase_t;
int main(void) {
const phase_t cycle[] = {
{ "sleep", 0.003, 59000 }, /* 3 uA for 59 s */
{ "measure", 2.0, 900 }, /* sensor powered, MCU awake */
{ "transmit", 45.0, 100 }, /* radio on */
};
double charge = 0, period = 0; /* mA*ms and ms */
for (int i = 0; i < 3; i++) {
charge += cycle[i].ma * cycle[i].ms;
period += cycle[i].ms;
printf("%-9s %8.3f mA for %6.0f ms\n", cycle[i].what, cycle[i].ma, cycle[i].ms);
}
double average_ma = charge / period;
double battery_mah = 2000 * 0.8; /* derate 20 % for temperature, ageing and self-discharge */
double hours = battery_mah / average_ma;
printf("average %.3f mA -> %.0f days on a 2000 mAh cell\n", average_ma, hours / 24);
return 0;
}
Notice which phase dominates: here the 100 ms of radio uses more charge than the 59 seconds of sleep. Shortening the transmission, or sending less often, does more for battery life than shaving microamps off sleep.
Brown-outs
When the supply sags (a weak battery, a motor or radio drawing a burst of current), the microcontroller can misbehave before it stops: flash writes corrupt, logic runs at a voltage below its rated speed. A brown-out detector (BOD) holds the chip in reset below a threshold instead. Always enable it on products, set it above the chip's minimum voltage at its clock speed, and:
- give radios and motors their own bulk capacitance near the load,
- finish or abandon flash writes cleanly when the supply is failing (see flash and file systems),
- count brown-out resets: many of them point at the supply, not the code.
Pitfall: Measuring average current with a multimeter on a device that sleeps and wakes gives a meaningless number. Use a power profiler or a scope across a shunt (see power profiling).
Interview question: How do you estimate battery life for a device that sleeps most of the time?
Answer: Split one cycle into phases (sleep, wake, measure, transmit), measure each phase's current and duration with a power profiler, and compute the average current as the sum of current times time divided by the period. Battery life is the usable capacity divided by that average, where usable capacity is derated for temperature, high-current pulses, self-discharge and ageing, often by 20 to 30 %. Then I check which phase dominates, which is usually the radio, and optimise that first.
Build it: warn about the battery before it fails
This is step 3, the last of the module project. The door sensor's coin cell sags under the radio's load as it ages, and an alarm system must report "battery low" weeks before the sensor goes silent, without crying wolf every time a cold night pulls the voltage down. The sensor has no voltage reference of its own: it measures its supply by reading the chip's internal reference with the ADC, right after a transmission, when the cell is at its weakest.
Lab: Supply voltage from VREFINT, and a battery state with hysteresis
vdd_mv(raw, cal)returns the supply voltage in millivolts, rounded to the nearest. The chip's internal reference readscalcounts when the supply is exactly 3.0 V (the factory measured it and stored it inVREFINT_CAL); with a lower supply the same reference reads more counts, soVDD = 3000 × cal / raw.batt_update(b, loaded_mv)is called after each heartbeat with the voltage measured under load and returns the battery state:- under
CRITICAL_MV:BATT_CRITICALat once (no filtering: below this, a transmission can brown out the chip, so the sensor sends only its last "battery dead" message); - under
LOW_MVon 3 heartbeats in a row:BATT_LOW; - once low or critical, it returns to
BATT_OKonly aboveNEW_MV(a new battery), so a warm afternoon does not cancel the warning. A reading at or aboveLOW_MVresets the run of low readings. - A reading between
CRITICAL_MVandLOW_MVwhile critical stays critical.
Going deeper
Interview question: How would you design a power bank's firmware to protect the battery and the user?
Answer: Monitor cell voltage, current and temperature, charge with constant current then constant voltage within the cell's limits, stop on over-temperature, and negotiate output voltage (USB PD) only within what the cable and device support. Detect short circuits in hardware, and handle brown-outs so the MCU never runs out of spec.
Interview question: When would you choose an LDO and when a switching regulator?
Answer: An LDO when the input is close to the output, or the current is small, so little power is wasted; and for noise-sensitive rails such as ADC references, sensors and RF, often after a switcher. A switching regulator when the voltage difference and current are large, so efficiency and heat matter, or when the output must be above the input (boost). For battery devices I also compare quiescent currents, since a regulator's idle draw can exceed the sleeping MCU's.
Interview question: What is a brown-out and how should firmware deal with it?
Answer: A brown-out is a dip of the supply below what the chip needs to run correctly, without going to zero, for example when a battery is weak or a motor starts. The chip can then execute wrongly or corrupt flash writes. The brown-out detector holds or resets the chip below a set threshold, and it should be enabled and set correctly. Firmware should avoid starting flash writes when the supply is low, make stored data robust against interrupted writes, and record brown-out resets so supply problems become visible.
Explain it: In your own words, explain why a linear regulator wastes energy and a switching regulator does not.
Answer: A linear regulator works like a variable resistor in series with the load: the same current flows in and out, and the voltage it drops from input to output times that current is turned into heat, so its efficiency is roughly the output voltage over the input voltage. A switching regulator instead switches the input on and off quickly and stores energy in an inductor, delivering it at the lower voltage but higher current, so input power roughly equals output power minus small switching and resistive losses.