In the previous project, we discussed how to wake ESP32 using EXT0. ESP32 supports five power modes: active, modem sleep, light sleep, deep sleep, and hibernation. For battery-powered or Internet-of-Things (IoT) applications, deep sleep mode is particularly useful.
In this mode, the ESP32 powers down most components, including the CPU, flash memory, Wi-Fi, Bluetooth, and peripherals. It “wakes up” in response to specific triggers such as a timer, touch, EXT0, EXT1, UART, or RTC. Deep sleep mode significantly conserves battery life and operates the controller only if needed.
There are two ways to wake ESP32 from deep sleep using external signals: EXT0 and EXT1. We already demonstrated how to use EXT0, which allows the board to wake up via an external signal on a specific RTC GPIO. However, EXT0 is limited to a single “wake-up” source.
In contrast, the EXT1 enables ESP32 to wake from multiple external sources and can even detect which GPIO triggered this. The board can then be programmed to execute different tasks based on the specific GPIO or external trigger source, such as a button press, sensor input, or any signal generating a defined falling or rising edge.
In this project, we’ll demonstrate how to wake the ESP32 from deep sleep using EXT1 and detect which GPIO triggered it. Based on the trigger source, the board will execute different tasks. Specifically, we will configure two external wake-up sources: GPIO36 and GPIO39.
- If the GPIO36 triggers ESP32 to wake, the LED will blink twice.
- If the GPIO39 triggers ESP32 to wake, the LED will blink five times.
While this example uses LED blinking for demonstration purposes, any task can be executed based on the external trigger. Additionally, specific console messages will be printed to the serial port for each trigger event.
Using EXT1
EXT1 lets ESP32 to wake up using multiple RTC GPIOs. While EXT0 uses the RTC input/output to trigger waking, EXT1 uses the RTC controller instead. As a result, RTC peripherals and RTC memories need not be powered during the deep sleep.
EXT1 also allows for multiple “wake-up” sources with different logic. Similar to EXT0, EXT1 only uses the RTC GPIOs. The available RTC GPIOs vary slightly between ESP32 chip revisions as follows:
- ESP32-S3: GPIOs 0-21
- ESP32: GPIOs 0, 2, 4, 12-15, 25-27, 32-39
- ESP32-S2: GPIOs 0-21
To configure EXT1 on ESP32, the esp_sleep_enable_ext1_wakeup_io() function must be called. It has this prototype:
esp_sleep_enable_ext1_wakeup_io(bitmask, mode);
Where…
bitmask: this 64-bit integer represents the GPIO pins used to wake ESP32. Each bit within the bitmask corresponds to an RTC GPIO pin. If a bit is set (1), that pin is enabled for wake-up. If a bit is clear (0), that pin is disabled for the wake-up. Typically, bitwise operations like OR (|), AND (&), and bit shifts (<<) are used to create the bitmask.
mode: specifies the logic that triggers the wake-up. There are two possible modes:
- ESP_SLEEP_WAKEUP_ANY_HIGH: ESP32 will wake if any of the enabled GPIOs (those with a ‘1’ in the bitmask) are HIGH. This is an “OR” condition.
- ESP_SLEEP_WAKEUP_ALL_LOW: ESP32 will wake only if all of the enabled GPIOs (those with a ‘1’ in the bitmask) are LOW simultaneously. This is an “AND” condition.
For ESP32-S2, ESP32-S3, ESP32-C6, or ESP32-H2, the available modes are following.
- ESP_EXT1_WAKEUP_ANY_LOW: ESP32 will wake if any of the enabled GPIOs (those with a ‘1’ in the bitmask) are LOW.
- ESP_EXT1_WAKEUP_ANY_HIGH: ESP32 will wake if any of the enabled GPIOs (those with a ‘1’ in the bitmask) are HIGH.
Like with EXT0, EXT1 can also use the RTC fast memory for data storage during deep sleep. This allows for storing data that must be preserved across sleep cycles and is extremely useful for keeping track of how many times ESP32 has woken up, storing sensor readings, and more.
To store a variable in RTC memory, use the RTC_DATA_ATTR attribute. For example:
RTC_DATA_ATTR int bootCount = 0;
This declaration tells the compiler to place the bootCount variable into RTC memory. The value of the bootCount is retained even when ESP32 is in deep sleep. However, it’s important to note that the RTC memory is typically erased when ESP32 is powered down or reset.
Using EXT1Â
Now, let’s implement EXT1 to wake the ESP32. In this project, we’ll configure ESP32 so it detects a rising wake-up signal from two push buttons connected to the GPIO36 and GPIO39. Upon waking, the ESP32 will perform the following tasks before re-entering deep sleep:
- Update and display the boot count.
- Identify and print the wake-up source (EXT1).
- If GPIO36 triggered the wake-up, blink the LED twice and print specific messages to the serial port.
- If GPIO39 triggered the wake-up, blink the LED five times and print specific messages to the serial port.
- After completing the designated tasks, re-enter deep sleep mode.
Components
- ESP32 x1
- Push button x2
- LED x1
- 10K resistors x3
- Breadboard x1
- Jumper wires
- MicroUSB cable to connect ESP32 with your computer
Circuit connections
Begin by interfacing an LED with ESP32’s GPIO23. Connect an LED’s cathode to ESP32’s GPIO and its anode to the ground. You’ll note, it’s impossible to interface the LED to GPIO34, GPIO35, GPIO36, or GPIO39 as these pins cannot be configured as digital output on ESP32.
So, we’ve interfaced an LED with ESP32’s GPIO23. Next, interface two pushbuttons, one at the GPIO36 and the other at the GPIO39 with an external pull-up. The circuit diagram is below.
The sketch
After making circuit connections, upload the following sketch to ESP32.
You may also like:
Filed Under: Electronic Projects, Video
Questions related to this article?
👉Ask and discuss on Electro-Tech-Online.com and EDAboard.com forums.
Tell Us What You Think!!
You must be logged in to post a comment.