One wake-up
A battery holds a fixed amount of charge. A power budget lists each state a device is in, the current it draws there and for how long. Our example: an ESP32 soil sensor that reports every 10 minutes.
Most of the time the chip is in deep sleep. The processors and most memory are off; only a small real-time clock (RTC) keeps counting. The datasheet gives 10 µA. That lasts 600 seconds.
The RTC timer wakes the chip and setup() starts again from the top. With the processor at 80 MHz and the radio off, it draws up to 31 mA. Reading the sensor takes about 1 second.
Now Wi-Fi joins the network and sends the reading. The radio transmits at up to 240 mA. Joining and sending take about 2 seconds, so we budget 240 mA for the whole 2 seconds.
Do the sums
Charge is current times time. In milliamp-seconds (mA·s): sleep 0.01 × 600 = 6, sense 31 × 1 = 31, send 240 × 2 = 480. One whole cycle of 603 seconds uses 517 mA·s.
Spread that charge over the whole cycle: 517 mA·s ÷ 603 s = 0.86 mA. To the battery the device looks like a steady 0.86 mA load, even though it really draws short bursts.
Capacity is in milliamp-hours (mAh). A 3,000 mAh cell can give 0.86 mA for 3,000 ÷ 0.86 ≈ 3,500 hours. That is about 146 days, almost five months on one charge.
Make it last
Of the 517 mA·s, the send uses 480: 93%. Sleep is only about 1%. So shaving microamps off sleep barely helps here. Sending less often, or for less time, is the big lever.
If the chip never slept and stayed awake at 31 mA, the same cell would last 3,000 ÷ 31 ≈ 97 hours: about 4 days. Sleep is what turns days into months.
Set the timer to 30 minutes (1,800 s). Each cycle now uses 529 mA·s over 1,803 s: 0.29 mA on average. The battery lasts about 426 days, almost three times longer.
Real cells give less. A lithium-ion cell loses 1 to 2% of its charge a month on its own, and its protection circuit about 3% more. Cold, regulators and LEDs cost too. Budget with a safety margin.
What happens
;; step 1/11 · budget
power budget every state the device is in:
current (mA) and time (s)
device ESP32 soil sensor, 3,000 mAh cell
cycle sleep -> wake, sense -> send -> sleep
// ESP32 (Arduino core): soil sensor on GPIO34, one report every 10 minutes
const uint64_t T = 600ULL * 1000000ULL; // 600 s, in microseconds
void sendReading(int m) {
// join Wi-Fi and send m: the connectivity lessons show how
}
void setup() {
setCpuFrequencyMhz(80);
int m = analogRead(34);
sendReading(m);
esp_sleep_enable_timer_wakeup(T);
esp_deep_sleep_start();
}
void loop() {} // never reached: waking from deep sleep boots again
Currents are from the Espressif ESP32 datasheet (v5.3): deep sleep with the RTC timer and RTC memory on, 10 µA; processor on at 80 MHz with the radio off, 20 to 31 mA (we take the top); Wi-Fi transmit at +19.5 dBm, 240 mA; receive, 95 to 100 mA. The 1 s and 2 s awake times are this example's assumptions. The 3,000 mAh cell is the teaching cell from the Hardware lesson "How batteries work". The plot drains it in a straight line at the average current and ignores self-discharge, so real lives are shorter.