ESP32 Sleep Modes and Wake-up Sources: A Perfect Power Saving guide

Since many IoT devices run on batteries, understanding and using ESP32 sleep modes is critical for extending device lifetime. One of the reasons the ESP32 is so popular in IoT applications is its ability to balance performance and power efficiency.

In this article, we’ll explore different sleep modes, their impact on power consumption, and the various wake-up sources that bring the ESP32 back to life when needed.


Why Sleep Modes Matter

Most IoT devices don’t need to stay active all the time. For example:

  • A temperature sensor only needs to send readings every few minutes.
  • A motion detector only wakes up when movement is detected.

By putting the ESP32 into a sleep mode, you save a huge amount of energy, making your device last months instead of days on a single charge.


ESP32 Sleep Modes

  1. Modem-sleep
    • Turns off Wi-Fi and Bluetooth while keeping the CPU active.
    • Suitable for applications that only need occasional connectivity.
  2. Light-sleep
    • Suspends CPU and peripherals but keeps memory and RTC peripherals powered.
    • Can wake up quickly from events such as timers, GPIO, or touch.
  3. Deep-sleep
    • CPU, most peripherals, and RAM are powered off.
    • Only RTC memory and peripherals remain active.
    • Perfect for ultra-low-power applications like battery-powered sensors.

Wake-up Sources and Examples

Basic Instructions to upload the code to esp32

  1. Connect the ESP32 to your PC via USB.
  2. Install the Arduino IDE if you don’t have. Open the Arduino IDE.
  3. Install ESP32 boards in arduino IDE if not installed.
  4. Select your ESP32 board under Tools → Board and correct Port.
  5. Copy-paste the provided code.
  6. Upload the sketch to your ESP32.

1. Timer Wake-up

Useful when the device needs to wake up at fixed intervals.

// ESP32 sleep modes 
#include "esp_sleep.h"

void setup() {
  Serial.begin(115200);
  delay(1000);

  Serial.println("ESP32 is going to sleep for 5 seconds...");
  esp_sleep_enable_timer_wakeup(5000000); // 5 seconds
  esp_deep_sleep_start();
}

void loop() {
  // Not used in deep sleep
}

2. Touch Wake-up

You can use the capacitive touch pins of ESP32 to wake the device.

Number of Touch Pins: The ESP32 has 10 capacitive touch GPIOs: T0 to T9 (mapped to GPIOs 4, 0, 2, 15, 13, 12, 14, 27, 33, and 32 respectively).

// ESP32 sleep modes 
#include "esp_sleep.h"
#include "driver/touch_pad.h"

void setup() {
  Serial.begin(115200);
  delay(1000);

  touchAttachInterrupt(T3, NULL, 40); // Example: GPIO 15 (Touch 3)
  esp_sleep_enable_touchpad_wakeup();

  Serial.println("Touch to wake ESP32...");
  esp_deep_sleep_start();
}

void loop() {}


3. GPIO Wake-up

The ESP32 can wake up from a specific pin going HIGH or LOW.

// ESP32 sleep modes 
#include "esp_sleep.h"

#define WAKEUP_PIN GPIO_NUM_33

void setup() {
  Serial.begin(115200);
  delay(1000);

  pinMode(WAKEUP_PIN, INPUT_PULLUP);
  esp_sleep_enable_ext0_wakeup(WAKEUP_PIN, 0); // Wake when pin goes LOW

  Serial.println("Press button on GPIO 33 to wake up...");
  esp_deep_sleep_start();
}

void loop() {}

4. ULP Co-Processor Wake-up

The ULP (Ultra-Low Power) co-processor can run simple tasks while ESP32 is in deep sleep (like reading sensors).

Example use case: Monitor an analog value (like battery voltage) and wake ESP32 if it crosses a threshold. (This one is more advanced and requires ULP assembly programming — good for a follow-up article).


Combining Wake-up Sources

ESP32 allows multiple wake-up sources together. For example, you can combine timer and GPIO so that the ESP32 wakes either on schedule or by a button press:

// ESP32 sleep modes 
#include "esp_sleep.h"

#define WAKEUP_PIN GPIO_NUM_33

void setup() {
  Serial.begin(115200);
  delay(1000);

  pinMode(WAKEUP_PIN, INPUT_PULLUP);

  esp_sleep_enable_timer_wakeup(10000000); // 10 seconds
  esp_sleep_enable_ext0_wakeup(WAKEUP_PIN, 0); // Button wake

  Serial.println("Sleeping... will wake in 10s or by button");
  esp_deep_sleep_start();
}

void loop() {}

Serial Monitor Output:

 ESP32 sleep modes-Timer Wakeup result

Use Cases

  • Timer + Sensor Reading: Periodic wake-ups to read temperature, then sleep again.
  • Touch Wake-up: A smart lamp activated by touch.
  • GPIO Wake-up: Battery-powered device that wakes on a button press.
  • ULP Wake-up: Monitoring battery voltage without fully waking the CPU.

Conclusion

Using ESP32 sleep modes effectively is the key to building low-power IoT devices. Whether you choose light sleep for quick responses or deep sleep for ultra-low power, you can fine-tune your project to maximize battery life.

Pairing these modes with the right wake-up sources (timer, touch, GPIO, or ULP) gives you full control over when and how your ESP32 runs.

If you’re serious about making portable, battery-efficient ESP32 projects, mastering sleep modes is just as important as coding the main logic.


Task for Readers:
Try modifying the timer example so the ESP32 wakes every 10 seconds, reads the internal hall sensor, prints the value to Serial, and goes back to sleep. This will help you practice combining sleep modes with sensor tasks.

Leave a Comment