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.
Build it step by step
0 of 5 doneWire the LED
GPIO26 through 330 ohms, cathode to GND.
Upload the sketch
Open the serial monitor at 115200.
Watch the output
A wake-up number prints every ten seconds.
Count the blinks
One more blink each time, up to ten.
Measure the supply current
Compare awake and asleep, with care.
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
- Development BoardsBuyESP32 DevKit V1 × 1
- SemiconductorsBuyLED 5 mm red × 1
- PassivesBuyResistor 330 Ω × 1
- PrototypingBuy
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| LED anode | 330 ohm resistor to ESP32 GPIO26 | Wake-up flash |
| LED cathode | ESP32 GND | Return |
| USB cable | Computer or a USB power bank | Power |
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 reachedWhat 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.
| Check | Expected |
|---|---|
| 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 |
| Reset by the EN button | Counter returns to 1 |
| Power cycle | Counter 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 →
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.
