Skip to content

Commit 206c0c7

Browse files
Deep Sleep Example Fix (#9904)
* fix(example): EXT0 and EXT1 wakeup Fixes the Deep Sleep wakup example to run with IDF5.1. The API has changed and a adjustment was necessary. * feat(wakeup): Use Macro for GPIO_NUM Changed the example to use a #define for the RTC IO Pin (GPIO) used in the example. * fix(typo): typo and commentaries Fixes the commentary to the correct IDF terms. * ci(pre-commit): Apply automatic fixes --------- Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
1 parent b77b38e commit 206c0c7

File tree

1 file changed

+53
-36
lines changed

1 file changed

+53
-36
lines changed

Diff for: libraries/ESP32/examples/DeepSleep/ExternalWakeUp/ExternalWakeUp.ino

+53-36
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,35 @@
11
/*
2-
Deep Sleep with External Wake Up
3-
=====================================
4-
This code displays how to use deep sleep with
5-
an external trigger as a wake up source and how
6-
to store data in RTC memory to use it over reboots
7-
8-
This code is under Public Domain License.
9-
10-
Hardware Connections
11-
======================
12-
Push Button to GPIO 33 pulled down with a 10K Ohm
13-
resistor
14-
15-
NOTE:
16-
======
17-
Only RTC IO can be used as a source for external wake
18-
source. They are pins: 0,2,4,12-15,25-27,32-39.
19-
20-
Author:
21-
Pranav Cherukupalli <[email protected]>
2+
Deep Sleep with External Wake Up
3+
=====================================
4+
This code displays how to use deep sleep with
5+
an external trigger as a wake up source and how
6+
to store data in RTC memory to use it over reboots
7+
8+
This code is under Public Domain License.
9+
10+
Hardware Connections
11+
======================
12+
Push Button to GPIO 33 pulled down with a 10K Ohm
13+
resistor
14+
15+
NOTE:
16+
======
17+
Only RTC IO can be used as a source for external wake
18+
source. They are pins: 0,2,4,12-15,25-27,32-39.
19+
20+
Author:
21+
Pranav Cherukupalli <[email protected]>
2222
*/
23+
#include "driver/rtc_io.h"
2324

24-
#define BUTTON_PIN_BITMASK 0x200000000 // 2^33 in hex
25-
25+
#define BUTTON_PIN_BITMASK(GPIO) (1ULL << GPIO) // 2 ^ GPIO_NUMBER in hex
26+
#define USE_EXT0_WAKEUP 1 // 1 = EXT0 wakeup, 0 = EXT1 wakeup
27+
#define WAKEUP_GPIO GPIO_NUM_33 // Only RTC IO are allowed - ESP32 Pin example
2628
RTC_DATA_ATTR int bootCount = 0;
2729

2830
/*
29-
Method to print the reason by which ESP32
30-
has been awaken from sleep
31+
Method to print the reason by which ESP32
32+
has been awaken from sleep
3133
*/
3234
void print_wakeup_reason() {
3335
esp_sleep_wakeup_cause_t wakeup_reason;
@@ -56,20 +58,35 @@ void setup() {
5658
print_wakeup_reason();
5759

5860
/*
59-
First we configure the wake up source
60-
We set our ESP32 to wake up for an external trigger.
61-
There are two types for ESP32, ext0 and ext1 .
62-
ext0 uses RTC_IO to wakeup thus requires RTC peripherals
63-
to be on while ext1 uses RTC Controller so does not need
64-
peripherals to be powered on.
65-
Note that using internal pullups/pulldowns also requires
66-
RTC peripherals to be turned on.
61+
First we configure the wake up source
62+
We set our ESP32 to wake up for an external trigger.
63+
There are two types for ESP32, ext0 and ext1 .
64+
ext0 uses RTC_IO to wakeup thus requires RTC peripherals
65+
to be on while ext1 uses RTC Controller so does not need
66+
peripherals to be powered on.
67+
Note that using internal pullups/pulldowns also requires
68+
RTC peripherals to be turned on.
6769
*/
68-
esp_sleep_enable_ext0_wakeup(GPIO_NUM_33, 1); //1 = High, 0 = Low
69-
70+
#if USE_EXT0_WAKEUP
71+
esp_sleep_enable_ext0_wakeup(WAKEUP_GPIO, 1); //1 = High, 0 = Low
72+
// Configure pullup/downs via RTCIO to tie wakeup pins to inactive level during deepsleep.
73+
// EXT0 resides in the same power domain (RTC_PERIPH) as the RTC IO pullup/downs.
74+
// No need to keep that power domain explicitly, unlike EXT1.
75+
rtc_gpio_pullup_dis(WAKEUP_GPIO);
76+
rtc_gpio_pulldown_en(WAKEUP_GPIO);
77+
78+
#else // EXT1 WAKEUP
7079
//If you were to use ext1, you would use it like
71-
//esp_sleep_enable_ext1_wakeup(BUTTON_PIN_BITMASK,ESP_EXT1_WAKEUP_ANY_HIGH);
72-
80+
esp_sleep_enable_ext1_wakeup_io(BUTTON_PIN_BITMASK(WAKEUP_GPIO), ESP_EXT1_WAKEUP_ANY_HIGH);
81+
/*
82+
If there are no external pull-up/downs, tie wakeup pins to inactive level with internal pull-up/downs via RTC IO
83+
during deepsleep. However, RTC IO relies on the RTC_PERIPH power domain. Keeping this power domain on will
84+
increase some power comsumption. However, if we turn off the RTC_PERIPH domain or if certain chips lack the RTC_PERIPH
85+
domain, we will use the HOLD feature to maintain the pull-up and pull-down on the pins during sleep.
86+
*/
87+
rtc_gpio_pulldown_en(WAKEUP_GPIO); // GPIO33 is tie to GND in order to wake up in HIGH
88+
rtc_gpio_pullup_dis(WAKEUP_GPIO); // Disable PULL_UP in order to allow it to wakeup on HIGH
89+
#endif
7390
//Go to sleep now
7491
Serial.println("Going to sleep now");
7592
esp_deep_sleep_start();

0 commit comments

Comments
 (0)