Table of Contents
This example uses GPIO12 as the external wake-up source.
#include <esp_sleep.h>
#define WAKEUP_PIN GPIO_NUM_12 // Define the wake-up GPIO pin
void setup() {
Serial.begin(115200);
delay(1000);
Serial.println("ESP32 going to deep sleep...");
// Configure the GPIO pin as an input with a pull-up resistor (optional)
pinMode(WAKEUP_PIN, INPUT_PULLUP);
// Enable external wake-up using GPIO pin (triggered when the pin goes LOW)
esp_sleep_enable_ext0_wakeup(WAKEUP_PIN, 0); // Wake up when GPIO is LOW
// Enter deep sleep mode
esp_deep_sleep_start();
}
void loop() {
// This will not execute after wake-up because the ESP32 restarts
}
How It Works
- GPIO Configuration: The pinMode() function configures the GPIO pin (GPIO12 in this case) as an input with a pull-up resistor.
- Wake-up Source: The esp_sleep_enable_ext0_wakeup() function enables wake-up from GPIO12 when it goes LOW (you can change this to trigger on HIGH by setting the second parameter to 1).
- Entering Deep Sleep: The ESP32 enters deep sleep using esp_deep_sleep_start(). It stays in deep sleep until GPIO12 is triggered (goes LOW).
- Once the GPIO is triggered, the ESP32 wakes up, restarts, and begins executing from setup().
Additional Notes
- If you want to wake up using a GPIO HIGH signal, change the second parameter in esp_sleep_enable_ext0_wakeup() to 1.
- The GPIO pin must be connected to an external signal (e.g., a button, a sensor, or another ESP32) that can pull it LOW or HIGH.
Since RTC memory is not used and an external GPIO is used to wake up the ESP32, it will enter hibernation mode.