Skip to content

Lesson15: Power Management and Light Sleep Wake-Up

1. Lesson Objectives

In this lesson, you will learn how to turn off the screen backlight, LCD power, and peripheral power before entering Light Sleep, and configure wake-up sources such as a timer, touch interrupt, display power status, and USB insertion. After waking up, the program restores power and prints the wake-up cause.

2. Prerequisites

  • The ESP32-S3 environment configuration in Lesson01 has been completed.
  • This lesson uses the built-in Arduino-ESP32 Wire, driver/gpio.h, and esp_sleep.h.
  • No additional third-party libraries are required.

How to add the libraries:

  • No additional third-party libraries are required for this lesson. Use the esp_sleep and GPIO APIs included with the ESP32 3.3.3 board package.

3. Arduino IDE Instructions

  1. Open Lesson15_Power_Sleep.ino in Arduino IDE. lesson_1

  2. Confirm that the board settings match those in Lesson01. In particular, do not change the PSRAM or Flash configuration. lesson_2

  3. Click Verify to compile. lesson_3

  4. Click Upload to flash the program, then open Serial Monitor and select a baud rate of 115200. lesson_4

4. Hardware Instructions

  1. Connect the development board to the computer using a USB data cable, then open Serial Monitor.

lesson_5

  1. After flashing, wait approximately 5 seconds and observe the serial message displayed before the device enters Light Sleep.

  2. Wait for the timer to wake the device automatically, or try GPIO wake-up sources such as touch input or USB insertion.

  3. After the device wakes up, observe the wake cause and GPIO status printed to the serial monitor.

  4. This lesson turns off the backlight, LCD power, and peripheral power. It is normal for the screen to go dark.

5. Key Code Explanation

analogWrite(kBacklightPin, 0);
digitalWrite(kDisplayPowerPin, HIGH);
digitalWrite(kPeripheralPowerPin, LOW);
prepareWakeupSources();
esp_light_sleep_start();

Before entering sleep, the program first turns off the backlight, then the LCD power, and finally the main peripheral power. GPIO40 turns on the LCD when set to LOW, so writing HIGH before sleep turns off the LCD. After the wake-up sources are configured, esp_light_sleep_start() puts the ESP32-S3 into Light Sleep.

esp_sleep_enable_timer_wakeup(kTimerWakeupMicroseconds);
gpioWakeEnabled |= enableOppositeLevelWake(
    kTouchInterruptPin, "TOUCH_INT", touchWakeLevel);
gpioWakeEnabled |= enableUsbInsertionWake();
if (gpioWakeEnabled) {
  esp_sleep_enable_gpio_wakeup();
}

Timer wake-up ensures that the device wakes up automatically even without external activity. For GPIO wake-up, the trigger level must first be configured for each specific pin, and then esp_sleep_enable_gpio_wakeup() must be called to enable it globally. Both the touch interrupt and USB insertion are GPIO wake-up sources.

const int currentLevel = digitalRead(pin);
configuredWakeLevel = currentLevel == HIGH ? LOW : HIGH;
const gpio_int_type_t wakeLevel =
    currentLevel == HIGH ? GPIO_INTR_LOW_LEVEL : GPIO_INTR_HIGH_LEVEL;

This logic reads the current GPIO level and then sets the opposite level as the wake-up condition. The advantage of this approach is that it does not assume a pin will always be HIGH or LOW after power-on. Any change in its level can serve as a wake-up event.

const esp_sleep_wakeup_cause_t cause = esp_sleep_get_wakeup_cause();
restorePower();
Serial.printf("SLEEP: WAKE cause=%s(%d)\n", wakeupCauseName(cause), static_cast<int>(cause));

The first step after waking up is to read the wake-up cause, then restore peripheral power and LCD power. The wake-up type is printed to the serial monitor, allowing students to determine whether the device was awakened by the timer or by GPIO.

Download the Lesson 15 code