All articles

    ESP32 Deep Sleep: A Project That Lasts Months on a Battery

    How to make an ESP32 run on a battery for a long time: the sleep modes and what each draws, deep sleep and waking by timer, by a pin or by several pins, finding the wake-up cause, RTC_DATA_ATTR variables, why the development board draws far more than the chip, the right regulator, shortening the awake time, the calculation, and the problem of uploading to a sleeping board.

    Updated 6 min read

    A temperature sensor in the garden sends a reading every 10 minutes over Wi-Fi. If the ESP32 stayed awake all the time, it would draw about 100 mA or more, and a 2000 mAh battery would be flat in a day. The fix is for it to sleep between readings.

    The chip wakes for moments to read and send, and the rest of the time it sleeps drawing microamps
    The chip wakes for moments to read and send, and the rest of the time it sleeps drawing microamps

    The sleep modes

    ModeWhat's runningApproximate chip consumption
    Awake with Wi-Fi onEverything100–240 mA (higher pulses)
    Modem sleepThe processor, Wi-Fi off20–30 mA
    Light sleepProcessor paused, memory keptabout 0.8 mA
    Deep sleepOnly the RTCabout 10 µA
    HibernationOnly the timerabout 5 µA

    These figures are for the original chip from the datasheet, and they vary a little between models (S3 and C3). The difference between awake and asleep is ten thousand times, which is the secret of projects that last for months.

    The idea

    In deep sleep:

    • The processor and Wi-Fi are off.
    • A small part (the RTC) keeps running to wake the chip.
    • The chip on its own draws about 10 µA (depending on the model and settings).
    • When it wakes, the program starts from the beginning (from setup), not from where it went to sleep.

    Waking by timer

    #define SLEEP_MIN 10
    
    void setup() {
      // 1. read the sensor
      // 2. connect to Wi-Fi and send
      // 3. go to sleep
      esp_sleep_enable_timer_wakeup((uint64_t)SLEEP_MIN * 60 * 1000000ULL);
      esp_deep_sleep_start();
    }
    
    void loop() {
      // never reached
    }

    The time is in microseconds, so watch the number type (ULL) so the calculation doesn't come out wrong. Everything happens in setup: wake, work, sleep.

    The RTC clock isn't very accurate (it can drift by a few per cent). If timing matters, correct the time from the internet now and then.

    Waking by a pin (a button or a sensor)

    esp_sleep_enable_ext0_wakeup(GPIO_NUM_33, 0);  // wakes when the pin goes LOW

    It must be an RTC pin (such as 0, 2, 4, 12–15, 25–27 and 32–39 on the original ESP32). Check the datasheet for your model.

    Note: the pin must be in a stable state while asleep. If the button connects to ground, add an external pull-up (10k or larger to save current), because the ordinary internal pull-up doesn't stay on during sleep.

    More than one pin

    uint64_t mask = (1ULL << 32) | (1ULL << 33);
    esp_sleep_enable_ext1_wakeup(mask, ESP_EXT1_WAKEUP_ANY_HIGH);

    ext1 wakes from several pins together. And you can combine more than one cause: a timer and a button, so whichever comes first wakes it. There's also wake-up by touch on the touch pins.

    Why did it wake?

    esp_sleep_wakeup_cause_t why = esp_sleep_get_wakeup_cause();
    if (why == ESP_SLEEP_WAKEUP_TIMER)  { /* time for the periodic reading */ }
    if (why == ESP_SLEEP_WAKEUP_EXT0)   { /* someone pressed the button */ }

    That way you do something different depending on the cause: an ordinary reading with the timer, and lighting a display with the button, for example.

    Variables that survive sleep

    RTC_DATA_ATTR int bootCount = 0;
    
    void setup() {
      bootCount++;
    }

    Ordinary RAM is wiped, but RTC_DATA_ATTR survives. If the power is cut completely, it's wiped too. For permanent storage, see Preferences.

    A practical use: collect 6 readings in an RTC array, and send them all at once every hour instead of connecting to Wi-Fi every 10 minutes. Connecting is the most expensive thing for the battery.

    The surprise: the board, not the chip

    A development board (DevKit) carries:

    • A voltage regulator that draws current by itself.
    • A USB chip (CP2102 or CH340).
    • A power LED that's always lit.

    So the board in deep sleep may draw milliamps instead of microamps. For serious projects:

    • A board designed for low consumption, or a bare ESP32 module with a regulator whose quiescent current is small.
    • Remove the power LED.
    • Actually measure the consumption with a multimeter in series.

    How much does the regulator matter?

    RegulatorApproximate current it uses by itself
    AMS11175 mA
    MCP1700 / HT73332–4 µA

    The AMS1117 alone draws 500 times more than the sleeping chip! The details are in the voltage regulators article. And note that the regulator must handle the Wi-Fi pulses (about 300–500 mA momentarily), so put a large capacitor next to the ESP32.

    Sensors eat current too

    A sensor left connected draws current all night. Power it from a GPIO pin if its current is small (a few mA), or through a MOSFET you switch off before sleeping. The voltage divider that measures the battery also draws current, so use large resistors (100k or more). See measuring battery voltage.

    The calculation

    A rough example: awake for 5 seconds at 120 mA, every 10 minutes, and sleeping at 50 µA:

    • Average ≈ (5 × 120 + 595 × 0.05) ÷ 600 ≈ 1.05 mA.
    • A 2000 mAh battery → about 80 days in theory (a bit less in reality).

    Work it out with the battery life calculator. Notice that being awake accounts for almost everything: 600 out of 630. So shortening the awake time matters far more than reducing the sleep current.

    Shorten the awake time

    • A static IP instead of waiting for the router to assign one: saves a second or two.
    • Read the sensor before Wi-Fi, and if the reading hasn't changed, you may not need to send at all.
    • Specify the channel and BSSID in WiFi.begin so it doesn't scan for the network.
    • Reduce printing on Serial in the final version.

    If the awake time drops to a second and a half instead of 5, the lifetime in the example roughly triples.

    The upload problem

    A sleeping board can't see the computer, so the upload fails. The fix: hold BOOT (and sometimes RESET) while uploading. A tip: put delay(3000) at the start of setup while you're testing, so there's time to upload before it sleeps, and remove it in the final version.

    Common problems

    ProblemUsual cause
    It keeps waking for no reasonThe wake pin is floating with no pull-up
    It wakes after seconds instead of minutesYou forgot ULL and the number overflowed
    The battery dies in daysThe power LED, regulator and USB chip on the board
    The relay changes state while it sleepsPins release their state in sleep; see gpio_hold_en
    It won't uploadThe board is asleep; hold BOOT

    FAQ

    Can I let it sleep while connected to USB? Yes, but the consumption measured then isn't real, because the USB chip is running. Measure from the battery.

    Which battery should I use? An 18650 lithium cell is common. Read battery types and charging lithium with the TP4056. And if you need it to last forever, a small solar panel can charge it: solar power basics.

    Circuits Nerd
    © 2026 Circuits Nerd. All rights reserved.