I once deployed a batch of soil moisture sensors in a field. They were supposed to run for a year on a small battery. Three weeks later, the batteries were dead.
I had not thought about power consumption at all. The microcontroller was running at full speed, the WiFi radio was always on, and the sensor was powered continuously. That is a recipe for a dead battery in days.
Power optimization is not optional for battery-powered devices. Here is what I learned.
Measure first, optimize second
Before you optimize anything, measure your actual current consumption. Use a current probe or a dedicated power analyzer like the Nordic PPK2. You cannot optimize what you have not measured.
You will often be surprised. The WiFi radio on an ESP32 uses about 200mA when transmitting. The microcontroller itself uses maybe 10mA. The radio is the problem, not the CPU.
Sleep modes: the biggest win
Modern microcontrollers have sleep modes that reduce power consumption dramatically. In deep sleep, an STM32 can use as little as 1 microamp. That is 10,000 times less than when it is running at full speed.
// STM32 example: enter stop mode, wake up on RTC alarm
HAL_RTCEx_SetWakeUpTimer_IT(&hrtc, 60, RTC_WAKEUPCLOCK_CK_SPRE_16BITS); // wake in 60 seconds
HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);
// ... execution resumes here after wakeupThe key insight: if your device only needs to do something once per minute, it should be asleep for 59 of those 60 seconds.
Duty cycling: wake, work, sleep
This is the most powerful technique. Instead of running continuously, your device wakes up, does its job quickly, and goes back to sleep.
while (1) {
PowerOnSensor();
WaitForSensorWarmup(10); // 10ms
float reading = ReadSensor();
PowerOffSensor();
TransmitData(reading); // send over LoRa or WiFi
EnterDeepSleep(60000); // sleep for 60 seconds
}A device that is active for 50ms every 60 seconds uses about 1/1200th of the power of a continuously active device. That turns a 3-day battery life into a year.
Power down peripherals you are not using
Every peripheral draws power even when idle. Turn them off when you do not need them:
// Power down the sensor between readings
HAL_GPIO_WritePin(SENSOR_PWR_GPIO, SENSOR_PWR_PIN, GPIO_PIN_RESET);
// Disable the ADC clock when not in use
__HAL_RCC_ADC_CLK_DISABLE();Choose low-power components
The microcontroller is often not the biggest power consumer. Choose your components carefully:
- Use LoRa instead of WiFi for long-range IoT. LoRa uses about 40mA transmitting vs WiFi's 200mA.
- Use e-ink displays instead of LCD. E-ink only uses power when the image changes.
- Choose sensors with sleep modes and fast startup times.
- Use a low-power MCU family (STM32L series, nRF52, SAMD21).
Run at lower clock speeds
Power consumption scales roughly linearly with clock frequency. If your task does not need 80 MHz, run at 8 MHz. You use 10 times less power.
Calculate your battery life
Once you know your average current consumption, calculating battery life is simple:
Battery capacity: 2000 mAh
Average current: 100 µA = 0.1 mA
Battery life = 2000 / 0.1 = 20,000 hours = about 2.3 yearsMeasure your actual average current with a power analyzer. Calculate. If it is not enough, find the biggest consumer and reduce it.




