Skip to content
systemcoding.dev
Embedded

Use ESP32 Deep Sleep With Timer and GPIO Wakeup

Enter ESP32 deep sleep with esp_deep_sleep_start, wake on a timer and an ext0 GPIO, keep a counter in RTC memory, and read the wake-up cause on boot.

6 min read
Summarize in AI
Flat illustration of a microcontroller wired to an LED and a sensor, with the Espressif, Arm and C logos.

In deep sleep an ESP32 powers down almost everything: both CPU cores, most of the RAM and the main digital peripherals. What stays alive is the RTC controller, the RTC fast and slow memories, and the RTC peripherals, which can wake the chip on a timer or on a pin. A deep-sleep wake-up is a reboot, so the program runs from the reset vector every time, and anything that must survive has to be put in RTC memory. This post builds a small program with ESP-IDF v5.3 on an ESP32 (the original dual-core part): it wakes on a 30 second timer or on a button press on GPIO 33, counts wake-ups in RTC memory, prints why it woke, and goes back to sleep. The API names come from the Sleep Modes chapter of the ESP-IDF Programming Guide.

What deep sleep keeps and loses

The ESP-IDF documentation describes the power domains. In deep sleep the CPUs, the main SRAM and flash cache are off. The RTC controller, the ULP coprocessor if used, the RTC slow memory (8 KB) and RTC fast memory (8 KB on the ESP32) stay on, depending on the sleep configuration. Variables are lost unless they are in RTC memory.

ItemAfter deep sleep wake-up
Normal variables in .bss and .dataRe-initialised, as after a reset
Variables marked RTC_DATA_ATTRKept, in RTC slow memory
Variables marked RTC_NOINIT_ATTRKept without re-initialisation on a software wake-up
GPIO configurationLost, unless held with gpio_hold_en
Wi-Fi and Bluetooth stateLost, must be restarted

The attribute RTC_DATA_ATTR is the one you usually want. It places the variable in RTC slow memory and initialises it on the first power-on, but not on a wake-up from deep sleep.

Choose the wake-up sources

A wake-up source is enabled before sleep by a function in esp_sleep.h. This program uses two:

  • esp_sleep_enable_timer_wakeup(uint64_t time_in_us) wakes after the given time, from the RTC timer.

  • esp_sleep_enable_ext0_wakeup(gpio_num_t gpio, int level) wakes when one RTC-capable pin goes to the given level.

On the original ESP32, ext0 works on the RTC GPIOs, which include GPIO 0, 2, 4, 12 to 15, 25 to 27, 32 to 39. GPIO 33 qualifies. The pin must also have its pull resistor configured through the RTC GPIO functions, because during deep sleep the normal digital pull configuration is off.

main.cc
#include <stdio.h>
#include "esp_sleep.h"
#include "esp_log.h"
#include "driver/gpio.h"
#include "driver/rtc_io.h"

#define WAKE_BUTTON   GPIO_NUM_33
#define SLEEP_SECONDS 30

static const char *TAG = "deepsleep";

static RTC_DATA_ATTR uint32_t boot_count;

static const char *cause_name(esp_sleep_wakeup_cause_t c)
{
    switch (c) {
    case ESP_SLEEP_WAKEUP_TIMER: return "timer";
    case ESP_SLEEP_WAKEUP_EXT0:  return "ext0 pin";
    case ESP_SLEEP_WAKEUP_EXT1:  return "ext1 pins";
    case ESP_SLEEP_WAKEUP_UNDEFINED: return "power-on or reset";
    default: return "other";
    }
}

void app_main(void)
{
    boot_count++;
    esp_sleep_wakeup_cause_t cause = esp_sleep_get_wakeup_cause();
    ESP_LOGI(TAG, "boot %lu, woke by %s", (unsigned long)boot_count, cause_name(cause));

    // ... take a measurement, send it, and so on ...

    rtc_gpio_pullup_en(WAKE_BUTTON);
    rtc_gpio_pulldown_dis(WAKE_BUTTON);
    esp_sleep_enable_ext0_wakeup(WAKE_BUTTON, 0);       // wake on low level (button to ground)
    esp_sleep_enable_timer_wakeup((uint64_t)SLEEP_SECONDS * 1000000ULL);

    ESP_LOGI(TAG, "sleeping");
    esp_deep_sleep_start();
}

esp_deep_sleep_start never returns. The chip powers down, and the next thing that runs is the bootloader and then app_main, with boot_count retained. The serial output across a few cycles, with the button pressed once during the second sleep, looks like this. The timestamps and the exact log prefixes vary with your build:

Text
I (301) deepsleep: boot 1, woke by power-on or reset
I (311) deepsleep: sleeping
I (301) deepsleep: boot 2, woke by timer
I (311) deepsleep: sleeping
I (298) deepsleep: boot 3, woke by ext0 pin
I (308) deepsleep: sleeping

The first line shows ESP_SLEEP_WAKEUP_UNDEFINED, which is what esp_sleep_get_wakeup_cause returns after a power-on reset or a reset that is not a deep-sleep wake-up. The counter goes from 1 to 2 to 3 because it sits in RTC memory.

Pull-ups and the level

During normal operation, the GPIO pull-up is part of the digital IO mux. In deep sleep the digital mux is off, and the RTC IO takes over the pad. If you only configure the pull with gpio_set_pull_mode, the pad floats in sleep and the pin may wake the chip at random, or never. The calls rtc_gpio_pullup_en and rtc_gpio_pulldown_dis configure the RTC IO's own resistors, which stay powered in deep sleep.

For a button wired from the pin to ground, the idle level is high through the pull-up and the pressed level is low, so the wake level is 0. For a sensor with an active-high output, use a pull-down and level 1. The function esp_sleep_enable_ext0_wakeup(gpio, level) takes 0 or 1.

If the pin is already at the wake level when you enter deep sleep, the chip wakes at once. A button that is held down during sleep entry produces an immediate wake-up. The remedy is to wait for the release before sleeping:

wait_release.cc
while (gpio_get_level(WAKE_BUTTON) == 0) {
    vTaskDelay(pdMS_TO_TICKS(10));
}

Several pins: ext1

ext0 handles one pin. For several, esp_sleep_enable_ext1_wakeup_io(uint64_t mask, esp_sleep_ext1_wakeup_mode_t mode) takes a bitmask of RTC GPIOs and a mode. On the original ESP32 the modes are ESP_EXT1_WAKEUP_ALL_LOW and ESP_EXT1_WAKEUP_ANY_HIGH. ESP-IDF v5.3 provides this function, and the older esp_sleep_enable_ext1_wakeup is deprecated. After the wake-up, esp_sleep_get_ext1_wakeup_status() returns the mask of the pins that triggered it:

ext1.cc
#define BUTTON_A GPIO_NUM_32
#define BUTTON_B GPIO_NUM_33

uint64_t mask = (1ULL << BUTTON_A) | (1ULL << BUTTON_B);
esp_sleep_enable_ext1_wakeup_io(mask, ESP_EXT1_WAKEUP_ANY_HIGH);   // buttons wired to 3.3 V, pull-downs on
esp_deep_sleep_start();

// after the wake-up:
uint64_t which = esp_sleep_get_ext1_wakeup_status();
if (which & (1ULL << BUTTON_A)) { /* button A */ }

The mode "any low" exists only on later chips such as the ESP32-S3, so on this part use active-high buttons with RTC pull-downs for "any" semantics. Check the chapter for your exact chip before relying on a mode.

Keeping state and pins across the sleep

Data in RTC_DATA_ATTR survives, but there is a cost: the RTC slow memory stays powered, which adds a little current. For larger state, the options are storing in flash through NVS, which has write limits, or in the RTC memory in a compact struct. A struct with a magic number and a checksum guards against garbage after a brown-out reset, when RTC memory may be corrupt:

rtc_state.cc
// crc32_le comes from esp_rom_crc.h
typedef struct {
    uint32_t magic;
    uint32_t boot_count;
    int32_t  last_reading;
    uint32_t crc;
} rtc_state_t;

static RTC_DATA_ATTR rtc_state_t state;

static bool state_valid(void)
{
    return state.magic == 0x5E5E1234u && state.crc == crc32_le(0, (uint8_t *)&state, offsetof(rtc_state_t, crc));
}

To keep an output pin at its level during deep sleep, a GPIO holding function exists: gpio_hold_en(gpio_num_t) latches the pad's level, and gpio_deep_sleep_hold_en() enables holding for all held pads during deep sleep. Remember to call gpio_hold_dis after the wake-up before you change the pin.

Mistakes to avoid

  • Expecting code to resume. After deep sleep there is no "after esp_deep_sleep_start()". Put all the work at the start of app_main.

  • Using a pin that is not RTC-capable. esp_sleep_enable_ext0_wakeup returns ESP_ERR_INVALID_ARG for such a pin. Check the return value with ESP_ERROR_CHECK.

  • Holding the button down. The chip wakes immediately and appears to never sleep.

  • Forgetting Wi-Fi shutdown. Call esp_wifi_stop before sleeping so the radio is off cleanly.

  • Spending too long awake. The point of deep sleep is a low duty cycle, so keep the awake path short.

Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.