Projects / ESP32 deep sleep timer

ESP32 deep sleep timer

An ESP32 sleeps in deep sleep for ten seconds, wakes to blink an LED and counts the wake-ups in memory that survives sleep.

Intermediate2 hGuided buildReviewed

Build it step by step

0 of 5 done
  1. ESP3211.8 mA

    Wire the LED

    GPIO26 through 330 ohms, cathode to GND.

  2. ESP32

    Upload the sketch

    Open the serial monitor at 115200.

  3. ESP32

    Watch the output

    A wake-up number prints every ten seconds.

  4. ESP32

    Count the blinks

    One more blink each time, up to ten.

  5. ESP32

    Measure the supply current

    Compare awake and asleep, with care.

In short

How do you build an ESP32 deep sleep timer?

An ESP32 sleeps in deep sleep for ten seconds, wakes to blink an LED and counts the wake-ups in memory that survives sleep. You need ESP32 DevKit V1, LED 5 mm red, Resistor 330 Ω and Breadboard 830 Tie Points. It takes 5 steps and about 120 minutes.

Level
Intermediate
Time
About 120 min
Parts
4

Parts list

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
LED anode330 ohm resistor to ESP32 GPIO26Wake-up flash
LED cathodeESP32 GNDReturn
USB cableComputer or a USB power bankPower

Code

In deep sleep the ESP32 switches off the processor cores, most of the memory and the radios, and keeps only a small timer and a little memory running. When the timer ends the chip restarts from the beginning of the program, so everything in setup runs again and ordinary variables are lost. A variable marked RTC_DATA_ATTR lives in the small always-on memory, so the wake-up counter survives. Here the LED blinks once for each wake-up up to ten so you can see the count rise. A battery project spends nearly all its time asleep and wakes only to take a reading, which is how a sensor can run for months. If you want to see the saving, put a multimeter in series with the USB supply in its milliamp range, with care and the right range set, and compare awake and asleep. A development board still draws a few milliamps asleep because of its USB chip and power LED, so the figure is much higher than the bare chip.

Runs on a virtual ESP32 Dev Module in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).

#include "esp_sleep.h"

const int LED = 26;
const uint64_t SLEEP_SECONDS = 10;
RTC_DATA_ATTR int wakeCount = 0;                  // kept in RTC memory through deep sleep

void setup() {
  Serial.begin(115200);
  delay(300);
  wakeCount++;
  Serial.print("Wake-up number "); Serial.println(wakeCount);

  pinMode(LED, OUTPUT);
  for (int i = 0; i < wakeCount && i < 10; i++) {   // blink once per wake-up, up to ten
    digitalWrite(LED, HIGH); delay(150);
    digitalWrite(LED, LOW);  delay(150);
  }

  esp_sleep_enable_timer_wakeup(SLEEP_SECONDS * 1000000ULL);   // microseconds
  Serial.println("Going to deep sleep");
  Serial.flush();
  esp_deep_sleep_start();                           // the chip restarts from setup() on wake-up
}

void loop() {}                                      // never reached

What the serial monitor should show

Open the serial monitor at the baud rate in the code. This is what our simulator prints for a virtual ESP32 Dev Module with its sensors at their default values. Your numbers will follow your real readings, but the format should match.

Wake-up number 1
Going to deep sleep
[deep sleep for 10 s]

Test checklist

What you should see at each check. If a reading is far off, stop and use the troubleshooting list.

CheckExpected
First startWake-up number 1 and one blink
Ten seconds laterNumber 2 and two blinks
Between wake-upsLED off and low current
Reset by the EN buttonCounter returns to 1
Power cycleCounter returns to 1

If it does not work

Counter never rises

Check RTC_DATA_ATTR is on the variable.

No serial output after sleep

Reopen the serial monitor, the USB port can drop on wake-up.

LED does not blink

Check polarity and GPIO26.

Current barely drops

The board USB chip and power LED still draw, that is normal.

Upload fails

Hold BOOT during the upload.

Still stuck? Find the fault step by step with your multimeter →

Ask Lark AI

Common questions

What parts do I need for the ESP32 deep sleep timer?

ESP32 DevKit V1, LED 5 mm red, Resistor 330 Ω and Breadboard 830 Tie Points.

How long does the ESP32 deep sleep timer take to build?

About 120 minutes for a intermediate maker, in 5 steps.

How do I know the ESP32 deep sleep timer works?

First start: Wake-up number 1 and one blink. Ten seconds later: Number 2 and two blinks. Between wake-ups: LED off and low current.

Counter never rises: what should I check?

Check RTC_DATA_ATTR is on the variable.

No serial output after sleep: what should I check?

Reopen the serial monitor, the USB port can drop on wake-up.

LED does not blink: what should I check?

Check polarity and GPIO26.

Current barely drops: what should I check?

The board USB chip and power LED still draw, that is normal.