1| Supported Targets | ESP32 | ESP32-S2 | ESP32-C3 |
2| ----------------- | ----- | -------- | -------- |
3
4# Deep Sleep Example
5
6(See the README.md file in the upper level 'examples' directory for more information about examples.)
7
8The [deep sleep mode](https://docs.espressif.com/projects/esp-idf/en/latest/api-reference/system/sleep_modes.html#sleep-modes) of the ESP32 is a power saving mode that causes the CPU, majority of RAM, and digital peripherals that are clocked from APB_CLK to be powered off. Deep sleep mode can be exited using one of multiple [wake up sources](https://docs.espressif.com/projects/esp-idf/en/latest/api-reference/system/sleep_modes.html#wakeup-sources). This example demonstrates how to use the [`esp_sleep.h`](https://docs.espressif.com/projects/esp-idf/en/latest/api-reference/system/sleep_modes.html#api-reference) API to enter deep sleep mode, then wake up form different sources.
9
10The following wake up sources are demonstrated in this example (refer to the [Wakeup Sources documentation](https://docs.espressif.com/projects/esp-idf/en/latest/api-reference/system/sleep_modes.html#wakeup-sources) for more details regarding wake up sources):
11
121. **Timer:** An RTC timer that can be programmed to trigger a wake up after a preset time. This example will trigger a wake up every 20 seconds.
132. **EXT1:** External wake up 1 which is tied to multiple RTC GPIOs. This example use GPIO2 and GPIO4 to trigger a wake up with any one of the two pins are HIGH.
143. **Touch:** Touch pad sensor interrupt. This example uses touch pads connected to GPIO32, GPIO33 in ESP32 or GPIO9 in ESP32-S2 to trigger a wake up when any of the pads are pressed.
154. **ULP:** Ultra Low Power Coprocessor which can continue to run during deep sleep. This example utilizes the ULP and constantly sample the chip's temperature and trigger a wake up  if the chips temperature exceeds ~5 degrees Celsius.
16
17Note: Some wake up sources can be disabled via configuration (see section on [project configuration](#Configure-the-project))
18
19In this example, the `CONFIG_BOOTLOADER_SKIP_VALIDATE_IN_DEEP_SLEEP` Kconfig option is used, which allows you to reduce the boot time of the bootloader during waking up from deep sleep. The bootloader stores in rtc memory the address of a running partition and uses it when it wakes up. This example allows you to skip all image checks and speed up the boot.
20
21## How to use example
22
23### Hardware Required
24
25This example should be able to run on any commonly available ESP32 development board without any extra hardware if only **Timer** and **ULP** wake up sources are used. However, the following extra connections will be required for the remaining wake up sources.
26
27- **EXT1:** GPIO2 and GPIO4 should be connected to LOW to avoid floating pins. When triggering a wake up, connect one or both of the pins to HIGH. Note that floating pins may trigger a wake up.
28
29- **Touch:** GPIO32, GPIO33 in ESP32 or GPIO9 in ESP32-S2 should be connected to touch sensors (see [Touch Sensor Application Note](https://github.com/espressif/esp-iot-solution/blob/release/v1.0/documents/touch_pad_solution/touch_sensor_design_en.md)).
30
31### Configure the project
32
33```
34idf.py menuconfig
35```
36
37* **Touch wake up** can be enabled/disabled via `Example configuration > Enable touch wake up`
38* **ULT wake up** can be enabled/disabled via `Example configuration > Enable temperature monitoring by ULP`
39
40Wake up sources that are unused or unconnected should be disabled in configuration to prevent inadvertent triggering of wake up as a result of floating pins.
41
42### Build and Flash
43
44Build the project and flash it to the board, then run monitor tool to view serial output:
45
46```
47idf.py -p PORT flash monitor
48```
49
50(Replace PORT with the name of the serial port to use.)
51
52(To exit the serial monitor, type ``Ctrl-]``.)
53
54See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects.
55
56## Example Output
57
58On initial startup, this example will detect that this is the first boot and output the following low:
59
60```
61...
62I (304) cpu_start: Starting scheduler on PRO CPU.
63I (0) cpu_start: Starting scheduler on APP CPU.
64Not a deep sleep reset
65Enabling timer wakeup, 20s
66Enabling EXT1 wakeup on pins GPIO2, GPIO4
67Touch pad #8 average: 2148, wakeup threshold set to 2048.
68Touch pad #9 average: 2148, wakeup threshold set to 2048.
69Enabling touch pad wakeup
70Enabling ULP wakeup
71Entering deep sleep
72```
73
74The ESP32 will then enter deep sleep. When a wake up occurs, the ESP32 must undergo the entire boot process again. However the example will detect that this boot is due to a wake up and indicate the wake up source in the output log such as the following:
75
76```
77...
78I (304) cpu_start: Starting scheduler on PRO CPU.
79I (0) cpu_start: Starting scheduler on APP CPU.
80Wake up from timer. Time spent in deep sleep: 20313ms
81ULP did 110 temperature measurements in 20313 ms
82Initial T=87, latest T=87
83Enabling timer wakeup, 20s
84Enabling EXT1 wakeup on pins GPIO2, GPIO4
85Touch pad #8 average: 2149, wakeup threshold set to 2049.
86Touch pad #9 average: 2146, wakeup threshold set to 2046.
87Enabling touch pad wakeup
88Enabling ULP wakeup
89Entering deep sleep
90```
91