11:06:39 AM NPT Arduino, Robotics & IoT tutorials for Nepal
Beginner

ESP32 Deep Sleep: Run a Sensor for Months on a Battery

Use ESP32 deep sleep with timer and button wake-up, keep data with RTC_DATA_ATTR, measure sleep current with a multimeter and estimate real battery life.

On this page
ESP32 Deep Sleep: Run a Sensor for Months on a Battery

You build an ESP32 weather node, put it on the roof with a battery, and by the next evening it is dead. The ESP32 is not broken. It was simply awake the whole time, with Wi-Fi on, and Wi-Fi is the hungry part.

Most sensor projects do not need to be awake all the time. A tank level, a soil moisture reading or a temperature value every few minutes is plenty. Between readings, the ESP32 can enter deep sleep, where almost everything is switched off. This guide shows how to use timer wake-up, button wake-up, keep a counter across sleeps, measure the real current, and estimate battery life honestly.

What you need

Scroll sideways to see all columns.

Item Qty Notes
ESP32 DevKit (ESP32-WROOM-32) 1 Any original ESP32 board; S2/S3/C3 differ slightly
Push button 1 For button wake-up
10 kΩ resistor 1 Button pull-up
10 kΩ potentiometer (or any analog sensor) 1 Stand-in for a real sensor
Digital multimeter with a mA range 1 To measure current
Regulated 5 V supply with a USB breakout, or a bench supply 1 For current measurement (not a power bank)
Breadboard and jumper wires 1 set

How deep sleep works

In deep sleep, the ESP32 switches off the main processor cores, Wi-Fi, Bluetooth and most of the chip. A small low-power part called the RTC (real-time clock) domain stays alive. It can wake the chip on a timer or on a pin change.

The key thing to understand: waking from deep sleep is like a reset. Your sketch starts again from setup(). loop() is often never reached. Normal variables are lost, which is why there is a special way to keep data (below).

A typical low-power sensor follows this cycle:

  1. Wake up.
  2. Read the sensor.
  3. Connect Wi-Fi only if you need to send data, send it, and disconnect.
  4. Go back to deep sleep.

The shorter step 2 and step 3 are, the longer the battery lasts.

Timer wake-up in two lines

example-1.cppDownload code
esp_sleep_enable_timer_wakeup(60ULL * 1000000ULL);  // 60 seconds, in microseconds
esp_deep_sleep_start();

The timer value is in microseconds, so multiply seconds by one million. Use ULL so the number does not overflow for long sleeps.

Keep data across sleep: RTC_DATA_ATTR

A normal global variable resets to its starting value every time the ESP32 wakes. A variable marked RTC_DATA_ATTR is stored in RTC memory, which stays powered during deep sleep.

example-2.cppDownload code
RTC_DATA_ATTR int bootCount = 0;

Two limits:

  • RTC memory is lost when power is removed or the battery dies. For permanent data, such as settings or long logs, use flash storage (the Preferences library) and write to it sparingly.
  • RTC memory is small, so keep only counters, last readings and flags there.

Wake on a button (ext0)

You can also wake the ESP32 when a pin changes, for example when someone presses a button. The ext0 wake source uses one RTC-capable GPIO: 0, 2, 4, 12 to 15, 25 to 27 and 32 to 39 on the original ESP32. Avoid the RTC-capable strapping pins (0, 2, 12, 15) for this; GPIO 33 is a good choice.

Wiring

Scroll sideways to see all columns.

Part Connects to
Push button, one side ESP32 GPIO 33
Push button, other side GND
10 kΩ resistor Between GPIO 33 and 3V3 (pull-up)
Potentiometer outer pins 3V3 and GND
Potentiometer wiper ESP32 GPIO 34 (ADC1, works with Wi-Fi)

With the external pull-up, GPIO 33 sits HIGH and goes LOW when you press the button. We wake on LOW. An external resistor is simpler and more predictable for beginners than enabling the internal RTC pull-ups in code.

If you are unsure which pins are safe on an ESP32, read the ESP32 Pinout for Beginners guide on ElectroDai first.

Complete code

sketch-3.inoDownload code
// ESP32 deep sleep: timer + button wake, RTC counter - not compiled yet
#include <Arduino.h>

const int  SENSOR_PIN = 34;                    // ADC1
const gpio_num_t BUTTON_PIN = GPIO_NUM_33;     // RTC GPIO, external pull-up
const uint64_t SLEEP_SECONDS = 60;

RTC_DATA_ATTR int bootCount = 0;               // survives deep sleep
RTC_DATA_ATTR int lastReading = -1;

void printWakeReason() {
  esp_sleep_wakeup_cause_t cause = esp_sleep_get_wakeup_cause();
  switch (cause) {
    case ESP_SLEEP_WAKEUP_TIMER: Serial.println("Woke by timer");  break;
    case ESP_SLEEP_WAKEUP_EXT0:  Serial.println("Woke by button"); break;
    default:                     Serial.println("Power-on or reset"); break;
  }
}

void setup() {
  Serial.begin(115200);
  delay(200);                    // give Serial time to start

  pinMode(BUTTON_PIN, INPUT);   // external 10 kΩ pull-up
  bootCount++;
  Serial.printf("Boot number: %d\n", bootCount);
  printWakeReason();

  int reading = analogRead(SENSOR_PIN);
  Serial.printf("Sensor: %d (previous: %d)\n", reading, lastReading);
  lastReading = reading;

  // Here you would connect Wi-Fi, send the reading, then disconnect.

  esp_sleep_enable_timer_wakeup(SLEEP_SECONDS * 1000000ULL);
  esp_sleep_enable_ext0_wakeup(BUTTON_PIN, 0);   // wake when GPIO 33 goes LOW

  while (digitalRead(BUTTON_PIN) == LOW) delay(20); // wait for release
  Serial.println("Going to sleep");
  Serial.flush();
  esp_deep_sleep_start();
}

void loop() {
  // Never reached: the ESP32 restarts from setup() after each wake
}

Open the Serial Monitor at 115200 baud. Every minute you should see a new boot number, "Woke by timer", and a reading. Press the button and it wakes immediately with "Woke by button".

Debounce note: a button can bounce, and if it is still held when the ESP32 goes back to sleep, it may wake again at once. The sketch waits until GPIO 33 reads HIGH before sleeping.

Why your dev board still drains power in sleep

Here is the part many tutorials skip. Even when the ESP32 chip is in deep sleep, a DevKit board has other parts that stay powered:

  • The USB-to-serial chip (CP2102, CH340 or similar).
  • The power LED, which glows all the time.
  • The voltage regulator, which has its own standby current.

On many DevKit boards, these parts draw far more than the sleeping ESP32 chip. So a DevKit is great for learning deep sleep, but for real months-on-a-battery projects, people use a bare ESP32 module on their own board, or a dev board designed for low power. The only way to know is to measure your own board.

Measure current safely with a multimeter

Current is measured in series, which means the current must flow through the meter.

  1. Unplug the USB cable. Current measured through USB includes the USB chip and is hard to break into.
  2. Set the meter to its highest current range first. Move the red probe to the mA (or A) jack.
  3. Connect the regulated 5 V supply's positive wire to the meter's red probe, and the meter's black probe to the board's 5V/VIN pin. Connect supply GND directly to board GND.
  4. Watch the reading while awake, then while asleep. Switch to a lower range only if the reading is small.
  5. Move the red probe back to the V/Ω jack when you finish. Leaving it in the current jack and then measuring voltage can short your circuit and blow the meter's fuse.

Do not use a power bank for this test. Many power banks switch off when the current becomes very small, which looks like a dead board.

Two notes: the awake current jumps around quickly (Wi-Fi bursts), so a normal multimeter shows a rough average, not peaks. And on its smallest current ranges, a meter can drop enough voltage to make the ESP32 reset when it wakes. If that happens, use a higher range. Our multimeter guide covers the meter basics.

Estimate battery life (the method)

Once you have your own measured values, the estimate is simple:

  1. Average current = (awake current × awake time + sleep current × sleep time) ÷ total cycle time.
  2. Battery life in hours = usable battery capacity in mAh ÷ average current in mA.
  3. Use only part of the label capacity (for example, 70 percent) to allow for temperature, ageing, regulator losses and the battery's cut-off voltage.

Illustration only, not measurements: if a board were awake for 5 seconds at 80 mA, and asleep for 595 seconds at 0.1 mA, the average would be (80 × 5 + 0.1 × 595) ÷ 600, about 0.77 mA. With a 2,000 mAh cell at 70 percent usable, that is about 1,400 mAh ÷ 0.77 mA, roughly 1,800 hours or around 75 days. Change any number and the answer changes a lot, which is exactly why you must measure.

The formula also shows what matters most: awake time. Cutting the Wi-Fi connection time often helps more than anything else.

Troubleshooting

Scroll sideways to see all columns.

Symptom Likely cause Fix
Counter resets to 1 every wake Variable not marked RTC_DATA_ATTR, or power was cut Add RTC_DATA_ATTR; check the supply
Never wakes up Sleep time too long or overflowed; wrong units Use microseconds and ULL
Wakes immediately again and again Button pin floating or held LOW Add the pull-up; wait for release before sleeping
Serial Monitor shows nothing after sleep USB serial reconnects after the print Add a short delay after Serial.begin()
Sleep current much higher than expected Dev board LED, USB chip, regulator Expected on DevKits; use a low-power board for deployment
Board resets when measuring current Meter's burden voltage on a low range Use a higher current range
Battery dies far sooner than the estimate Long Wi-Fi connect time Shorten awake time; send less often

FAQ

Is light sleep the same as deep sleep?

No. Light sleep pauses the CPU but keeps more of the chip powered, and the program continues where it stopped. Deep sleep saves more power but restarts from setup().

Can I use any GPIO to wake the ESP32?

Only RTC-capable GPIOs can wake it from deep sleep. On the original ESP32 those are 0, 2, 4, 12 to 15, 25 to 27 and 32 to 39.

Can I run this from an 18650 cell directly?

A single lithium cell is up to about 4.2 V, which is too high for the ESP32's 3.3 V pin and too low for some DevKit regulators to give a clean 3.3 V. Use a suitable low-dropout regulator or board design, and follow 18650 safety rules.

What project should I make with deep sleep?

A rooftop tank monitor, a soil moisture node or a remote weather node. See our ESP32 IoT project ideas, and use the ESP32 Blynk dashboard to view the data.

Related tutorials

See all IoT →