# ESP32 deep sleep: how to run a sensor for months on a battery

- Link: https://www.smartdataline.com/esp32-diy-electronics/esp32-deep-sleep-how-to-run-a-sensor-for-months-on-a-battery/
- Published: 2026-11-21T22:01:27+00:00
- Author: Sinoptik

_An ESP32 that stays awake drains a battery in days. The same ESP32 that sleeps 
most of the time and wakes only to take a measurement can run for months. The chip
supports this well, but the result depends on more than one function call. This 
guide covers how deep sleep works, where the power really goes, and how to estimate
battery life before you build anything._

## How deep sleep works

In deep sleep, the CPU and most peripherals are switched off. Only the RTC controller(
and, if you keep it enabled, a small RTC memory area) stays powered. According to
the datasheet, the chip itself then draws only microamps.

The key point is that the program **restarts from the beginning** after every wake-
up. Normal variables are lost, `setup()` runs again, and `loop()` is never reached
if you go back to sleep at the end of `setup()`. Variables marked with `RTC_DATA_ATTR`
are the exception: they survive deep sleep.

    ```wp-block-code
    #include <Arduino.h>

    #define SLEEP_SECONDS 600
    #define US_TO_S 1000000ULL

    RTC_DATA_ATTR int bootCount = 0;   // survives deep sleep

    void setup() {
      Serial.begin(115200);
      bootCount++;

      // 1. read the sensor
      // 2. send the data
      // 3. return to sleep as quickly as possible

      esp_sleep_enable_timer_wakeup(SLEEP_SECONDS * US_TO_S);
      esp_deep_sleep_start();
    }

    void loop() {
      // never reached
    }
    ```

The timer wake-up function takes microseconds, which is why the seconds are multiplied
by one million.

 [ADD: a photo of your ESP32 setup with the sensor connected]

## Where the power really goes

Many people enable deep sleep and then find that the board still draws far more 
than the datasheet suggests. The reason is usually the board, not the chip. A typical
development board also carries a USB-to-serial bridge, a power LED and a voltage
regulator that draws current even when the ESP32 is asleep.

**What to do:**

 * Measure the real sleep current with a multimeter in its microamp range, or with
   a dedicated power profiler. Do not trust the chip’s datasheet figure for your
   whole board.
 * For a battery project, use a board designed for low power, or a bare module with
   an efficient regulator.
 * On a development board, the power LED and the regulator are the usual suspects.
   Check which parts stay powered from the battery.

 [ADD: the sleep current you measured on your own board, and which board it is]

## Stay awake as briefly as possible

Deep sleep saves power only if the time between sleeps is short. Connecting to Wi-
Fi is usually the longest and most expensive part of each cycle. Ways to shorten
it:

 * **Remember the access point.** Store the Wi-Fi channel and BSSID in RTC memory
   and pass them to `WiFi.begin()` on the next wake-up. This skips the full network
   scan.
 * **Use a static IP** so the device does not wait for DHCP.
 * **Send everything in one go.** Read the sensor first, then connect, send one 
   message and disconnect.
 * **Consider ESP-NOW** instead of Wi-Fi if your receiver is another ESP device.
   It avoids the full connection to a router.
 * **Turn off Wi-Fi and Bluetooth** before going to sleep instead of relying on 
   the chip to do it for you.⌊How ESP32 deep sleep really works, where the power
   goes, and how to estimate battery life before you build.⌉

## Switch off the sensor too

A sensor that stays powered during sleep can use more current than the ESP32 itself.
If the sensor draws little enough, power it from a GPIO pin and turn it off before
sleeping. Check the sensor’s current in its datasheet against the maximum current
your GPIO can supply. For anything larger, switch the sensor’s supply with a MOSFET
load switch.

Also look at pins with external pull-up or pull-down resistors. They can leak current
while the chip sleeps. The ESP32 can hold pin states during deep sleep (see `gpio_hold_en()`
and `gpio_deep_sleep_hold_en()` in the ESP-IDF documentation), so outputs do not
float when the chip is off.

## Estimate battery life before you build

The calculation is simple. Average current is the charge used in one full cycle 
divided by the cycle length:

    ```wp-block-code
    I_avg = (I_active × t_active + I_sleep × t_sleep) / (t_active + t_sleep)
    Battery life (hours) = battery capacity (mAh) / I_avg (mA)
    ```

Here is an illustration with **made-up numbers, not measurements**. Replace them
with your own. Say the board draws 120 mA while awake and 0.1 mA asleep, wakes every
10 minutes, and runs from a 2000 mAh battery:

 * **Awake for 5 seconds per cycle:** average current is about 1.1 mA, which gives
   roughly 1800 hours, or about 76 days.
 * **Awake for 1.5 seconds per cycle** (faster reconnect): average current is about
   0.4 mA, which gives roughly 5000 hours, or about 208 days.

Cutting the awake time mattered much more than the sleep current in this example.
Real battery life will be lower, because this ignores battery self-discharge, regulator
losses and voltage drop at the end of the discharge.

## Choose the right wake-up source

 * **Timer:** wakes the chip at fixed intervals. This is the simplest option for
   periodic readings.
 * **External wake-up (ext0, ext1):** wakes the chip from a button, a reed switch
   or a motion sensor. These work only with RTC-capable GPIO pins, so check the 
   pin list for your board.
 * **Touch pins:** wake the chip from a touch event on supported pins.

## Common mistakes

 * Expecting variables to keep their values after wake-up without `RTC_DATA_ATTR`.
 * Leaving long `delay()` calls in the awake part of the program.
 * Forgetting that the board, regulator and sensor draw current of their own.
 * Powering the board from a battery that sags under load. A voltage dip when Wi-
   Fi turns on can reset the chip. See the brownout article on this site for the
   fixes.
 * Waking up too often. Ask whether you really need a reading every minute.

 [ADD: a real battery-life result from one of your devices, if you have one]

## Checklist

 * Measured the sleep current of the whole board, not just the chip.
 * Awake time is as short as possible, with fast Wi-Fi reconnect or ESP-NOW.
 * Sensor is powered off during sleep, or draws negligible current.
 * Persistent data is stored in `RTC_DATA_ATTR` variables.
 * Battery life is estimated with your own measured numbers.
 * Wake-up source matches the use case, and the pins are RTC-capable where required.
