Trouble with external wake-up in ESP32

Hello, I want the ESP32 to enter deep sleep until a button is pressed and it wakes up, but I'm not being able to do so. I tried the following:

#include <esp_sleep.h>

#define BUTTON_PIN 21

void setup() {
  Serial.begin(9600);
}

void loop() {
  Serial.println("Going to sleep in 2 seconds");
  delay(2000);
  
  esp_sleep_enable_ext0_wakeup((gpio_num_t)BUTTON_PIN, LOW);
  esp_deep_sleep_start();

  Serial.println("Just woke up!");
}

I don't know if the Just Woke Up message isn't being printed because the board is not waking up or if it has something to do with the serial monitor connection upon coming back from sleep. How should I modify my code? Thanks

The line

Serial.println("Just woke up!");

will never be executed because when ESP32 is awakened it does not resume starting from where you enter in deep sleep mode, but restarts from setup().

You need to consider wake up as a reset.

Makes sense except @mribeira didn't say that the message "Going to sleep in 2 seconds" reprints after pressing the button.

Yup, it doesn't. Nothing gets printed even if written in the setup function, so I'm not sure it's going to sleep at all.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.