├── README.md └── deep_sleep_demo └── deep_sleep_demo.ino /README.md: -------------------------------------------------------------------------------- 1 | # ESP32 Deep Sleep Demo 2 | 3 | You can watch a video walkthrough of this code [here](https://youtu.be/YOjgZUg_skU) 4 | 5 | [![Demo Video](https://img.youtube.com/vi/YOjgZUg_skU/0.jpg)](https://www.youtube.com/watch?v=YOjgZUg_skU) 6 | 7 | ## Porting to another board 8 | 9 | You will probably want to change this line: 10 | 11 | ``` 12 | #define BUTTON_GPIO GPIO_NUM_35 13 | ``` 14 | 15 | To match one of the buttons on your board. 16 | 17 | ## Deep sleep using a timer to wake up 18 | 19 | Make sure this is uncommented: 20 | 21 | ``` 22 | #define DEEP_SLEEP_TIMER 23 | ``` 24 | 25 | When you push the button, the ESP32 will enter deep sleep for 30 seconds. 26 | 27 | ## Deep sleep using a single GPIO pin to wake up 28 | 29 | Make sure this is uncommented: 30 | 31 | ``` 32 | #define DEEP_SLEEP_EXT0 33 | ``` 34 | 35 | The ESP32 will wake up when the button is pressed. 36 | 37 | ## Deep sleep using multiple GPIO pins to wake up 38 | 39 | There are two options here: 40 | 41 | `#define DEEP_SLEEP_EXT1_ANY_HIGH` 42 | 43 | This will wake the ESP32 if either pins 12 or 15 go high. 44 | 45 | Or: 46 | 47 | `#define DEEP_SLEEP_EXT1_ALL_LOW` 48 | 49 | This will wake the ESP32 when both pins 12 and 15 are taken low. 50 | 51 | # Deep sleep using touch to wake up 52 | 53 | Uncomment: 54 | 55 | ``` 56 | #define DEEP_SLEEP_TOUCH 57 | ``` 58 | 59 | The device will wake up when touch pin 9 is touched (GPIO 32). You can just connect a wire to pin 32 and when you touch the wire the device will wake up. 60 | -------------------------------------------------------------------------------- /deep_sleep_demo/deep_sleep_demo.ino: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // Built in button GPIO - adjust for your board 4 | #define BUTTON_GPIO GPIO_NUM_35 5 | 6 | // Select the deep sleep mode you want to test 7 | 8 | //Will sleep for 30 seconds 9 | #define DEEP_SLEEP_TIMER 10 | 11 | //Will wake up when BUTTON_GPIO goes LOW 12 | //#define DEEP_SLEEP_EXT0 13 | 14 | //Will wake up when pins 15 or 12 go HIGH 15 | //#define DEEP_SLEEP_EXT1_ANY_HIGH 16 | 17 | //Will wake up when pins 15 or 12 are both LOW 18 | //#define DEEP_SLEEP_EXT1_ALL_LOW 19 | 20 | //Will wake up on touch of touch pin 9 (GPIO 32) 21 | //#define DEEP_SLEEP_TOUCH 22 | 23 | 24 | // keep track of how many times we've come out of deep sleep 25 | RTC_DATA_ATTR int sleep_count = 0; 26 | 27 | // dump out the pin that woke us from EXT1 deep sleep 28 | void show_ext1_wakeup_reason() 29 | { 30 | uint64_t ext1_waskeup_reason = esp_sleep_get_ext1_wakeup_status(); 31 | for (int i = 0; i < GPIO_NUM_MAX; i++) 32 | { 33 | if (ext1_waskeup_reason & (1ULL << i)) 34 | { 35 | Serial.printf("GPIO %d\n", i); 36 | } 37 | } 38 | } 39 | 40 | 41 | // dump out the touch pin that caused the wake up 42 | void show_touch_wakeup_reason() 43 | { 44 | touch_pad_t tp = esp_sleep_get_touchpad_wakeup_status(); 45 | Serial.printf("T %d\n", tp); 46 | } 47 | 48 | // work out why we were woken up 49 | void show_wake_reason() 50 | { 51 | sleep_count++; 52 | auto cause = esp_sleep_get_wakeup_cause(); 53 | switch (cause) 54 | { 55 | case ESP_SLEEP_WAKEUP_UNDEFINED: 56 | Serial.println("Undefined"); // most likely a boot up after a reset or power cycly 57 | break; 58 | case ESP_SLEEP_WAKEUP_EXT0: 59 | Serial.println("Wakeup reason: EXT0"); 60 | break; 61 | case ESP_SLEEP_WAKEUP_EXT1: 62 | Serial.println("Wakeup reason: EXT1"); 63 | show_ext1_wakeup_reason(); 64 | break; 65 | case ESP_SLEEP_WAKEUP_TIMER: 66 | Serial.println("Wakeup reason: TIMER"); 67 | break; 68 | case ESP_SLEEP_WAKEUP_TOUCHPAD: 69 | Serial.println("Wakeup reason: TOUCHPAD"); 70 | show_touch_wakeup_reason(); 71 | break; 72 | case ESP_SLEEP_WAKEUP_ULP: 73 | Serial.println("Wakeup reason: ULP"); 74 | break; 75 | default: 76 | Serial.printf("Wakeup reason: %d\n", cause); 77 | } 78 | Serial.printf("Count %d\n", sleep_count); 79 | } 80 | 81 | // touch handler - needed for waking from touch 82 | void touch_isr_handler() 83 | { 84 | Serial.println("Touch!"); 85 | } 86 | 87 | // count down 3 seconds and then go to sleep 88 | void enter_sleep() 89 | { 90 | Serial.println("Going to sleep..."); 91 | delay(1000); 92 | Serial.println("3"); 93 | delay(1000); 94 | Serial.println("2"); 95 | delay(1000); 96 | Serial.println("1"); 97 | delay(1000); 98 | #ifdef DEEP_SLEEP_TIMER 99 | esp_sleep_enable_timer_wakeup(30000000); 100 | #endif 101 | #ifdef DEEP_SLEEP_EXT0 102 | pinMode(BUTTON_GPIO, INPUT_PULLUP); 103 | rtc_gpio_hold_en(BUTTON_GPIO); 104 | esp_sleep_enable_ext0_wakeup(BUTTON_GPIO, LOW); 105 | #endif 106 | #ifdef DEEP_SLEEP_EXT1_ANY_HIGH 107 | pinMode(GPIO_NUM_15, INPUT_PULLDOWN); 108 | rtc_gpio_hold_en(GPIO_NUM_15); 109 | pinMode(GPIO_NUM_12, INPUT_PULLDOWN); 110 | rtc_gpio_hold_en(GPIO_NUM_12); 111 | esp_sleep_enable_ext1_wakeup((1 << GPIO_NUM_12) | (1 << GPIO_NUM_15), ESP_EXT1_WAKEUP_ANY_HIGH); 112 | #endif 113 | #ifdef DEEP_SLEEP_EXT1_ALL_LOW 114 | pinMode(GPIO_NUM_15, INPUT_PULLUP); 115 | rtc_gpio_hold_en(GPIO_NUM_15); 116 | pinMode(GPIO_NUM_12, INPUT_PULLUP); 117 | rtc_gpio_hold_en(GPIO_NUM_12); 118 | esp_sleep_enable_ext1_wakeup((1 << GPIO_NUM_12) | (1 << GPIO_NUM_15), ESP_EXT1_WAKEUP_ALL_LOW); 119 | #endif 120 | #ifdef DEEP_SLEEP_TOUCH 121 | touchAttachInterrupt(T9, touch_isr_handler, 50); 122 | esp_sleep_enable_touchpad_wakeup(); 123 | #endif 124 | esp_deep_sleep_start(); 125 | } 126 | 127 | void setup(void) 128 | { 129 | Serial.begin(115200); 130 | // we've just started up - show the reason why 131 | show_wake_reason(); 132 | // enable the button on pin 35 133 | pinMode(BUTTON_GPIO, INPUT_PULLUP); 134 | } 135 | 136 | void loop() 137 | { 138 | if (digitalRead(BUTTON_GPIO) == LOW) 139 | { 140 | Serial.println("Button pressed - enter sleep\n"); 141 | enter_sleep(); 142 | } 143 | } 144 | --------------------------------------------------------------------------------