External wakeup from deep sleep on XIAO ESP32C3

Hi there,

So I have this running in the video, Sleeps and wakes up BSP 3.1.1
good to go using the GPIO…

The ESP32C3 uses ESP_SLEEP APIs, but the function esp_sleep_enable_ext0_wakeup() is not always available in some ESP32 variants (especially ESP32C3). The correct way for GPIO wake-up on the ESP32C3 is to use esp_deep_sleep_enable_gpio_wakeup(), which you originally had in your code.

#include <Adafruit_NeoPixel.h>
#include <esp_sleep.h>

// Define pin connections
#define LED_PIN    2   // WS2812 connected to D0 (GPIO2)
#define BUTTON_PIN 3   // Button connected to D1 (GPIO3)
#define NUM_LEDS   8   // Total WS2812 LEDs

Adafruit_NeoPixel strip(NUM_LEDS, LED_PIN, NEO_GRB + NEO_KHZ800);

void enterDeepSleep() {
    Serial.println("Entering deep sleep... Press the button to wake up.");
    Serial.flush();
    delay(100);

    // Configure D1 (GPIO3) as the wake-up source
    esp_sleep_enable_ext0_wakeup((gpio_num_t)BUTTON_PIN, HIGH);  // Wake on HIGH

    // Enter deep sleep
    esp_deep_sleep_start();
}

void setup() {
    Serial.begin(115200);
    strip.begin();
    strip.show(); // Initialize all LEDs to OFF

    pinMode(BUTTON_PIN, INPUT_PULLDOWN);
}

void loop() {
    static int ledIndex = 0;

    if (ledIndex < 5) {  // Light up 5 LEDs, one at a time
        strip.setPixelColor(ledIndex, strip.Color(0, 255, 0)); // Green color
        strip.show();
        Serial.print("Lighting LED: ");
        Serial.println(ledIndex);
        delay(1000); // Wait 1 second
        ledIndex++;
    } else {
        enterDeepSleep();  // After 5 LEDs are lit, enter deep sleep
    }
}

Serial output…

Lighting LED: 0
Lighting LED: 1
Lighting LED: 2
Lighting LED: 3
Lighting LED: 4
Entering deep sleep... Press the button to wake up.
Waking up from Deep Sleep...
Lighting LED: 0
Lighting LED: 1
Lighting LED: 2
Lighting LED: 3
Lighting LED: 4
Entering deep sleep... Press the button to wake up.

Video…

Powers on
:heavy_check_mark: Each time the loop runs, one WS2812 LED lights up (Green).
:heavy_check_mark: After the 5th LED lights up, the ESP32C3 goes into deep sleep.
:heavy_check_mark: Pressing the button on D1 (GPIO3) wakes up the device.
:heavy_check_mark: After wake-up, the sequence starts over.
Setup inits the GPIO as the Wakeup call.

HTH
GL :slight_smile: PJ :v:

  • Replaced esp_sleep_enable_ext0_wakeup() with esp_deep_sleep_enable_gpio_wakeup()
  • This is the correct method for ESP32C3 GPIO wakeup.
  • It will wake up when D1 (GPIO3) is pulled HIGH.
  • Added esp_sleep_get_wakeup_cause()
  • Now, after waking up, the Serial Monitor will print “Waking up from Deep Sleep…”.
  • Added INPUT_PULLDOWN to BUTTON_PIN
  • Ensures that GPIO3 stays LOW when not pressed, preventing false wake-ups.
    :+1: