|
1 | 1 | /*
|
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]> |
22 | 22 | */
|
| 23 | +#include "driver/rtc_io.h" |
23 | 24 |
|
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 |
26 | 28 | RTC_DATA_ATTR int bootCount = 0;
|
27 | 29 |
|
28 | 30 | /*
|
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 |
31 | 33 | */
|
32 | 34 | void print_wakeup_reason() {
|
33 | 35 | esp_sleep_wakeup_cause_t wakeup_reason;
|
@@ -56,20 +58,35 @@ void setup() {
|
56 | 58 | print_wakeup_reason();
|
57 | 59 |
|
58 | 60 | /*
|
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. |
67 | 69 | */
|
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 |
70 | 79 | //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 |
73 | 90 | //Go to sleep now
|
74 | 91 | Serial.println("Going to sleep now");
|
75 | 92 | esp_deep_sleep_start();
|
|
0 commit comments