Table of Contents

The RTC GPIOs are part of the RTC (Real-Time Clock) controller and remain active even when the main CPU is in deep sleep. This allows you to keep track of certain GPIO states during deep sleep and even wake up the ESP32 using these pins.

However, regular GPIOs (non-RTC) lose their state when the ESP32 enters deep sleep because the power to these pins is turned off in deep sleep.

How to Keep GPIO State in Deep Sleep:

  1. Use RTC GPIOs:

The ESP32 has RTC GPIOs that maintain their state during deep sleep. These pins can retain their input or output values when the ESP32 is in deep sleep.

  1. Configure RTC GPIOs to retain state:

You can configure a RTC GPIO as an input or output before entering deep sleep. The state of the pin will be retained, and you can use it to wake up the ESP32 or perform actions after waking up.

Steps to Keep GPIO State:

  1. Select RTC GPIOs:

The ESP32 has certain GPIO pins that are connected to the RTC controller. These are the RTC GPIOs that can retain their state during deep sleep. Examples of these GPIOs include:

  • GPIO 0
  • GPIO 2
  • GPIO 12
  • GPIO 13
  • GPIO 14
  • GPIO 15
  • GPIO 34
  • GPIO 35
  • GPIO 36
  • GPIO 39
  1. Configure the GPIO Pin:
  • You can configure the RTC GPIO pin as an input or output before entering deep sleep.
  • If you are using the pin to wake up the ESP32, you can set it as an input and use the esp_sleep_enable_ext0_wakeup() function.
  1. Entering Deep Sleep:
  • The ESP32 can be put into deep sleep, where the state of the RTC GPIOs is maintained.
  • Other peripherals are powered down, but the RTC GPIOs will retain their input/output state.
  1. Wake Up Using GPIO:
  • You can use the GPIOs to wake up the ESP32 when an event occurs (such as a voltage change on the GPIO pin).
  • If the pin is configured as an input, you can use the esp_sleep_enable_ext0_wakeup() to trigger a wake-up event.

Example Code to Keep GPIO State in Deep Sleep:

This example demonstrates how to set up a GPIO as an RTC input and use it to wake up the ESP32 from deep sleep.

#include <esp_sleep.h>

#define WAKEUP_PIN GPIO_NUM_12  // Use GPIO12 as RTC GPIO to wake up

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

    // Set GPIO12 as an input pin (this will retain state in deep sleep)
    pinMode(WAKEUP_PIN, INPUT);

    // Enable wake-up from GPIO12 (falling edge)
    esp_sleep_enable_ext0_wakeup(WAKEUP_PIN, 0);  // Trigger on falling edge (GPIO12 goes LOW)

    Serial.println("Entering deep sleep...");

    // Enter deep sleep mode
    esp_deep_sleep_start();
}

void loop() {
    // This code won't execute after deep sleep; the ESP32 will restart upon wake-up
}

How It Works:

  1. GPIO Configuration: The GPIO pin GPIO12 is set as an input pin before entering deep sleep. This pin retains its state during deep sleep.
  2. Wake-Up Condition: The ESP32 is set to wake up from deep sleep if GPIO12 goes LOW (falling edge). This is specified using esp_sleep_enable_ext0_wakeup().
  3. Entering Deep Sleep: The ESP32 enters deep sleep using esp_deep_sleep_start(), during which the state of GPIO12 is maintained by the RTC controller.
  4. Wake-Up: Once the event occurs (e.g., GPIO12 goes LOW), the ESP32 will wake up and resume execution from the start.

Key Considerations:

  • RTC GPIOs Only: Only RTC GPIOs can retain state during deep sleep. Non-RTC GPIOs lose their state when the ESP32 enters deep sleep.
  • State of Output Pins: If you configure a GPIO as an output and set it HIGH or LOW before entering deep sleep, the pin will retain that state as long as it is connected to the RTC power domain (which is the case for RTC GPIOs).
  • Power Consumption: Keeping the ESP32 in deep sleep with RTC GPIOs active will still result in very low power consumption (~10 µA).

Categorized in:

Deep Sleep,

Tagged in: