{"id":163571,"date":"2025-03-20T15:25:47","date_gmt":"2025-03-20T15:25:47","guid":{"rendered":"https:\/\/randomnerdtutorials.com\/?p=163571"},"modified":"2025-03-24T11:36:44","modified_gmt":"2025-03-24T11:36:44","slug":"esp32-light-sleep-arduino","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/esp32-light-sleep-arduino\/","title":{"rendered":"ESP32 Light Sleep Mode and Wake-Up Sources (Arduino IDE)"},"content":{"rendered":"\n<p>Light Sleep is one of the power-saving modes supported by the ESP32. In this guide, we&#8217;ll look at how to implement the Light Sleep mode with the ESP32 and the different wake-up sources. We&#8217;ll cover timer wake-up, different methods of external wake-up, GPIO wake-up, touch wake-up, and wake-up via UART.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" width=\"1200\" height=\"675\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/11\/ESP32-Light-Sleep-and-wake-up-sources.jpg?resize=1200%2C675&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 Light Sleep Mode and Wake-Up Sources (Arduino IDE)\" class=\"wp-image-163745\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/11\/ESP32-Light-Sleep-and-wake-up-sources.jpg?w=1920&amp;quality=100&amp;strip=all&amp;ssl=1 1920w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/11\/ESP32-Light-Sleep-and-wake-up-sources.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/11\/ESP32-Light-Sleep-and-wake-up-sources.jpg?resize=1024%2C576&amp;quality=100&amp;strip=all&amp;ssl=1 1024w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/11\/ESP32-Light-Sleep-and-wake-up-sources.jpg?resize=768%2C432&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/11\/ESP32-Light-Sleep-and-wake-up-sources.jpg?resize=1536%2C864&amp;quality=100&amp;strip=all&amp;ssl=1 1536w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/figure><\/div>\n\n\n<p><strong>Table of Contents<\/strong><\/p>\n\n\n\n<p>In this tutorial, we&#8217;ll cover the following topics:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"#light-sleep-intro\" title=\"\">Introducing Light Sleep<\/a><\/li>\n\n\n\n<li><a href=\"#set-light-sleep\" title=\"\">Setting Up Light Sleep on the ESP32<\/a><\/li>\n\n\n\n<li><a href=\"#wake-up-sources\" title=\"\">Light Sleep Wake-Up Sources<\/a><\/li>\n\n\n\n<li><a href=\"#timer\" title=\"\">Light Sleep with Timer Wake-Up<\/a><\/li>\n\n\n\n<li><a href=\"#external-wake-up\" title=\"\">Light Sleep with External Wake-Up<\/a>\n<ul class=\"wp-block-list\">\n<li><a href=\"#ext0-external-wake-up\" title=\"\">ext0 External Wake-Up<\/a><\/li>\n\n\n\n<li><a href=\"#ext1-wake-up-source\" title=\"\">ext1 External Wake-Up<\/a><\/li>\n\n\n\n<li><a href=\"#gpio-external-wake-up\" title=\"\">GPIO External Wake-Up<\/a><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><a href=\"#touch-wake-up\" title=\"\">Light Sleep with Touch Wake-Up<\/a><\/li>\n\n\n\n<li><a href=\"#uart-wake-up\" title=\"\">Light Sleep with UART Wake-Up<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Prerequisites<\/h2>\n\n\n\n<p>This tutorial focuses on programming the ESP32 using the Arduino core. Before proceeding, you should have the ESP32 Arduino core installed in your Arduino IDE. Follow the next tutorial to install the ESP32 on the Arduino IDE, if you haven\u2019t already.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/installing-esp32-arduino-ide-2-0\/\" title=\"\">Installing the ESP32 Board in Arduino IDE (Windows, Mac OS X, and Linux instructions)<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"light-sleep-intro\">Introducing Light Sleep<\/h2>\n\n\n\n<p>The ESP32 offers several power-saving modes: modem sleep, light sleep, and deep sleep. We covered <a href=\"https:\/\/randomnerdtutorials.com\/esp32-deep-sleep-arduino-ide-wake-up-sources\/\" title=\"\">deep sleep in great detail in a previous post<\/a>.<\/p>\n\n\n\n<p>You can compare the different modes in the following table from the ESP32 Espressif datasheet.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"667\" height=\"174\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/02\/POWER-MODES.jpg?resize=667%2C174&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"power consumption esp32\" class=\"wp-image-81665\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/02\/POWER-MODES.jpg?w=667&amp;quality=100&amp;strip=all&amp;ssl=1 667w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/02\/POWER-MODES.jpg?resize=300%2C78&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 667px) 100vw, 667px\" \/><\/figure><\/div>\n\n\n<p>Unlike deep sleep, which fully powers down the CPU and most peripherals, light sleep retains the CPU&#8217;s state and keeps RAM data intact. This means program variables and states are preserved, allowing the ESP32 to resume tasks almost instantly upon waking. <\/p>\n\n\n\n<p>This means that when the ESP32 wakes up, it will resume the code where it left off before going to sleep (contrary to deep sleep which runs the code from the start). Additionally, light sleep mode supports more wake-up sources like GPIO wake-up and UART wake-up.<\/p>\n\n\n\n<p>The <a href=\"http:\/\/espressif.com\/sites\/default\/files\/documentation\/esp32_datasheet_en.pdf\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\">ESP32 Espressif datasheet<\/a> also provides a table comparing the power consumption of the different power modes.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"857\" height=\"344\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/02\/power-consumption-1.png?resize=857%2C344&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"esp32 different sleep modes\" class=\"wp-image-81668\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/02\/power-consumption-1.png?w=857&amp;quality=100&amp;strip=all&amp;ssl=1 857w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/02\/power-consumption-1.png?resize=300%2C120&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/02\/power-consumption-1.png?resize=768%2C308&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/02\/power-consumption-1.png?resize=250%2C100&amp;quality=100&amp;strip=all&amp;ssl=1 250w\" sizes=\"(max-width: 857px) 100vw, 857px\" \/><\/figure><\/div>\n\n\n<p>In terms of power consumption, light sleep consumes less than active mode, but more than deep sleep.<\/p>\n\n\n\n<p>Light sleep is ideal for applications that need moderate power savings but must still respond quickly to certain events or interrupts, such as temporarily inactive sensors that periodically check data.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"set-light-sleep\">Setting Up Light Sleep on the ESP32<\/h2>\n\n\n\n<p>Putting the ESP32 in light sleep mode is super simple. After setting up the wake-up source or sources, you just need to call the <span class=\"rnthl rntliteral\">esp_light_sleep_start()<\/span> function.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>esp_light_sleep_start();<\/code><\/pre>\n\n\n\n<p>After that, the ESP32 will wake up when the required wake-up source(s) is triggered. When the ESP32 wakes up, it will return to where it was in the code.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"wake-up-sources\">Light Sleep Wake-Up Sources<\/h2>\n\n\n\n<p>After putting the ESP32 into light sleep mode, there are several ways to wake it up.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Timer Wake-Up<\/strong>: the RTC timer can be set to wake the ESP32 after a specified time interval. Use the <span class=\"rnthl rntliteral\">esp_sleep_enable_timer_wakeup()<\/span> function.<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>GPIO Wake-Up<\/strong>: GPIOs can be configured to trigger a wake-up on a high or low signal. This is useful to wake up the board with a pushbutton or a signal from sensors. You can use the <span class=\"rnthl rntliteral\">esp_sleep_enable_ext0_wakeup()<\/span>, the <span class=\"rnthl rntliteral\">esp_sleep_enable_ext1_wakeup_io()<\/span>, or the <span class=\"rnthl rntliteral\">esp_sleep_enable_gpio_wakeup()<\/span>.<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Touch Wake-Up<\/strong>: wake up from light sleep when a touch is detected on the ESP32 touch pins. Use the <span class=\"rnthl rntliteral\">touchSleepWakeUpEnable()<\/span> function.<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>UART Wake-Up<\/strong>: wake-up the ESP32 when data is received via UART on a specific port using the <span class=\"rnthl rntliteral\">esp_sleep_enable_uart_wakeup()<\/span> function.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Disable Sleep Wake-Up Sources<\/h3>\n\n\n\n<p>To disable previously enabled wake-up sources, use the <span class=\"rnthl rntliteral\">esp_sleep_disable_wakeup_source()<\/span> function and pass as argument the wake-up source you want to disable. If you want to disable all wake-up sources pass <span class=\"rnthl rntliteral\">ESP_SLEEP_WAKEUP_ALL<\/span> as argument.<\/p>\n\n\n\n<p>A list of the wake-up sources can be found here: <a href=\"https:\/\/docs.espressif.com\/projects\/esp-idf\/en\/latest\/esp32\/api-reference\/system\/sleep_modes.html#_CPPv418esp_sleep_source_t\" target=\"_blank\" rel=\"noopener\" title=\"\">sleep wake-up source argument<\/a>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"timer\">Light Sleep with Timer Wake-Up<\/h2>\n\n\n\n<p>In this section, we&#8217;ll show you how to put the ESP32 in light sleep mode and wake it up after a defined interval.<\/p>\n\n\n\n<p>To wake up the ESP32 after a predetermined period, you need to set up a timer wake-up using the <span class=\"rnthl rntliteral\">esp_sleep_enable_timer_wakeup()<\/span> function. Pass as argument the time in microseconds.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>esp_sleep_enable_timer_wakeup(uint64_t time_in_us)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">ESP32 Light Sleep with Timer Wake-Up Example<\/h3>\n\n\n\n<p>The following example will put the ESP32 into Light Sleep mode. The ESP32 will wake up every 10 seconds.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">\/*********\n  Rui Santos &amp; Sara Santos - Random Nerd Tutorials\n  Complete project details at https:\/\/RandomNerdTutorials.com\/esp32-light-sleep-arduino\/\n*********\/\n\nint counter = 0;\nconst int ledPin = 2;           \/\/ GPIO pin for onboard LED\nuint64_t sleepTime = 10000000;  \/\/ Sleep duration in microseconds (10 seconds)\n\nvoid setup() {\n    Serial.begin(115200);\n    pinMode(ledPin, OUTPUT);\n\n    \/\/ Enable wake-up by timer\n    esp_err_t result = esp_sleep_enable_timer_wakeup(sleepTime);\n\n    if (result == ESP_OK) {\n        Serial.println(&quot;Timer Wake-Up set successfully as wake-up source.&quot;);\n    } else {\n        Serial.println(&quot;Failed to set Timer Wake-Up as wake-up source.&quot;);\n    }\n}\n\nvoid loop() {\n    Serial.printf(&quot;Counter: %d\\n&quot;, counter);\n    counter++;\n\n    digitalWrite(ledPin, HIGH);  \/\/ LED on to indicate wake-up\n    delay(2000);\n    digitalWrite(ledPin, LOW);   \/\/ Turn off LED before going to sleep\n\n    Serial.println(&quot;Going into light sleep mode&quot;);\n    delay(500);\n    esp_light_sleep_start();     \/\/ Enter light sleep\n    Serial.println(&quot;Returning from light sleep&quot;);\n}\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/ESP32\/ESP32_Light_Sleep\/ESP32_Light_Sleep_Timer_Wake_Up.ino\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<p>This example turns an LED on when the ESP32 is woken up and increases a <span class=\"rnthl rntliteral\">counter<\/span> variable in each loop. This example demonstrates that the ESP32 RAM retains data, so variables and program states remain intact when the ESP32 wakes up. <\/p>\n\n\n\n<p>When it wakes up, it returns to where it was in the code (contrary to <a href=\"https:\/\/randomnerdtutorials.com\/esp32-deep-sleep-arduino-ide-wake-up-sources\/\" title=\"\">deep sleep<\/a> that starts running the code from the beginning).<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">How Does the Code Work?<\/h4>\n\n\n\n<p>Let&#8217;s take a quick look at the relevant parts for light sleep with timer wake-up.<\/p>\n\n\n\n<p>In the <span class=\"rnthl rntliteral\">setup()<\/span>, we call the <span class=\"rnthl rntliteral\">esp_sleep_enable_timer_wakeup()<\/span> function to enable timer wake-up. We pass as an argument the <span class=\"rnthl rntliteral\">sleepTime<\/span> variable(10000000 microseconds = 10 seconds).<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>esp_err_t result = esp_sleep_enable_timer_wakeup(sleepTime);<\/code><\/pre>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">esp_sleep_enable_timer_wakeup()<\/span> function returns <span class=\"rnthl rntliteral\">ESP_OK<\/span> if timer wake-up was successfully set up. We check that in the following lines. <\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>if (result == ESP_OK) {\n    Serial.println(\"Timer Wake-Up set successfully as wake-up source.\");\n} else {\n    Serial.println(\"Failed to set Timer Wake-Up as wake-up source.\");\n}<\/code><\/pre>\n\n\n\n<p>In every <span class=\"rnthl rntliteral\">loop()<\/span>, we increase the <span class=\"rnthl rntliteral\">counter<\/span> variable.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>counter++;<\/code><\/pre>\n\n\n\n<p>Light up the onboard LED for two seconds.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>digitalWrite(ledPin, HIGH);    \/\/ LED on to indicate wake-up\ndelay(2000);\n digitalWrite(ledPin, LOW); \/\/ Turn off LED before going to sleep<\/code><\/pre>\n\n\n\n<p>And we put the ESP32 in light sleep mode by calling the <span class=\"rnthl rntliteral\">esp_light_sleep_start()<\/span> function.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>esp_light_sleep_start();       \/\/ Enter light sleep<\/code><\/pre>\n\n\n\n<p>When the ESP32 wakes up from sleep, it will continue running the code. So, after waking up, it will print the following message.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>Serial.println(\"Returning from light sleep\");<\/code><\/pre>\n\n\n\n<p>In every <span class=\"rnthl rntliteral\">loop()<\/span> and wake-up cycle, the <span class=\"rnthl rntliteral\">counter<\/span> variable increases. This proves that the ESP32 can retain the values of variables during light sleep.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"749\" height=\"290\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/ESP32-Light-Sleep-Timer-Wake-Up-Demonstration-Serial-Monitor.png?resize=749%2C290&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 Light Sleep Mode with Timer Wake-Up Testing Serial Monitor \" class=\"wp-image-163582\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/ESP32-Light-Sleep-Timer-Wake-Up-Demonstration-Serial-Monitor.png?w=749&amp;quality=100&amp;strip=all&amp;ssl=1 749w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/ESP32-Light-Sleep-Timer-Wake-Up-Demonstration-Serial-Monitor.png?resize=300%2C116&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 749px) 100vw, 749px\" \/><\/figure><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"external-wake-up\">Light Sleep with External Wake-Up<\/h2>\n\n\n\n<p>In this section, you&#8217;ll learn how to put the ESP32 into light sleep mode and wake it up with an external wake-up, in this case, a pushbutton. Any GPIO that is an RTC GPIO can be used to wake up the ESP32.<\/p>\n\n\n\n<p>There are three different ways to use external wake-up with light sleep: the <strong>ext0 <\/strong>method, the <strong>ext1 <\/strong>method, and the <strong>GPIO <\/strong>wake-up. We&#8217;ll take a look at each of them.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"ext0-external-wake-up\">ext0 External Wake-Up<\/h3>\n\n\n\n<p>The <strong>ext0<\/strong> external wake-up allows you to wake-up the ESP32 when an RTC GPIO is set to a predefined state. RTC peripherals are kept on during sleep if this wake-up source is requested.<\/p>\n\n\n\n<p>To use this wake-up source, use the <span class=\"rnthl rntliteral\">esp_sleep_enable_ext0_wakeup()<\/span> function and pass as argument the GPIO number in this format <span class=\"rnthl rntliteral\">GPIO_NUM_X<\/span> (in which X corresponds to the GPIO number), and the level (<span class=\"rnthl rntliteral\">0<\/span>=LOW, or <span class=\"rnthl rntliteral\">1<\/span>=HIGH).<\/p>\n\n\n\n<p>For example:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>esp_sleep_enable_ext0_wakeup(GPIO_NUM_27, level);<\/code><\/pre>\n\n\n\n<p><strong>Note:<\/strong>&nbsp;you can only use pins that are&nbsp;<strong>RTC GPIOs<\/strong>&nbsp;with this wake-up source. Here\u2019s a list of the RTC GPIOs for different ESP32 chip models:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>ESP32-S3<\/strong>: 0-21;<\/li>\n\n\n\n<li><strong>ESP32<\/strong>: 0, 2, 4, 12-15, 25-27, 32-39;<\/li>\n\n\n\n<li><strong>ESP32-S2<\/strong>: 0-21;<\/li>\n<\/ul>\n\n\n\n<p class=\"rntbox rntclblue\"><strong>Recommended reading:<\/strong> <a href=\"https:\/\/randomnerdtutorials.com\/esp32-pinout-reference-gpios\/\" title=\"\">ESP32 Pinout Reference: Which GPIO pins should you use?<\/a><\/p>\n\n\n\n<p class=\"rntbox rntclgreen\"><strong>Recommended reading:<\/strong> <a href=\"https:\/\/randomnerdtutorials.com\/esp32-s3-devkitc-pinout-guide\/\">ESP32-S3 DevKitC Pinout Refence Guide: GPIOs Explained<\/a><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Light Sleep with External Wake-Up Example (ext0)<\/h4>\n\n\n\n<p>Here&#8217;s a simple example using light sleep with external wake-up <strong>ext0<\/strong>. This example wakes up the ESP32 with the press of a pushbutton. Every time it wakes up, it turns on an LED for five seconds and increases the <span class=\"rnthl rntliteral\">counter<\/span> variable.<\/p>\n\n\n\n<p>To test this example, wire a pushbutton to the ESP32 GPIO 27 as shown in the picture below (because we&#8217;re using an internal pull-down resistor, we don&#8217;t need to add to the the circuit).<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"847\" height=\"832\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/11\/ESP32-pushbutton-internal-pull-down-resistor-1.png?resize=847%2C832&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 with pushbutton with internal pull down resistor\" class=\"wp-image-163751\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/11\/ESP32-pushbutton-internal-pull-down-resistor-1.png?w=847&amp;quality=100&amp;strip=all&amp;ssl=1 847w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/11\/ESP32-pushbutton-internal-pull-down-resistor-1.png?resize=300%2C295&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/11\/ESP32-pushbutton-internal-pull-down-resistor-1.png?resize=768%2C754&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 847px) 100vw, 847px\" \/><\/figure><\/div>\n\n\n<p>After wiring the circuit, you can upload the code to your board.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">\/*********\n  Rui Santos &amp; Sara Santos - Random Nerd Tutorials\n  Complete project details at https:\/\/RandomNerdTutorials.com\/esp32-light-sleep-arduino\/\n*********\/\n\nint counter = 0;\nconst int ledPin = 2;            \/\/ GPIO pin for onboard LED\n#define buttonPin  GPIO_NUM_27   \/\/ Connect a pushbutton to GPIO 27\n\nvoid setup() {\n    Serial.begin(115200);\n    pinMode(ledPin, OUTPUT);\n    pinMode(buttonPin, INPUT_PULLDOWN); \/\/ pull-down resistor\n\n    \/\/ Enable wake-up by EXT0 using the button on GPIO 27\n    esp_err_t result = esp_sleep_enable_ext0_wakeup(buttonPin, 1); \/\/ 1 = wake up on HIGH signal\n\n    if (result == ESP_OK) {\n        Serial.println(&quot;EXT0 Wake-Up set successfully as wake-up source.&quot;);\n    } else {\n        Serial.println(&quot;Failed to set EXT0 Wake-Up as wake-up source.&quot;);\n    }\n}\n\nvoid loop() {\n    Serial.printf(&quot;Counter: %d\\n&quot;, counter);\n    counter++;\n\n    digitalWrite(ledPin, HIGH); \/\/ LED on to indicate wake-up\n    delay(5000);\n    digitalWrite(ledPin, LOW);  \/\/ Turn off LED before going to sleep\n\n    Serial.println(&quot;Going into light sleep mode&quot;);\n    delay(500);\n    esp_light_sleep_start();    \/\/ Enter light sleep\n    Serial.println(&quot;Returning from light sleep&quot;);\n}<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/ESP32\/ESP32_Light_Sleep\/ESP32_Light_Sleep_ext0.ino\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">How does the code work?<\/h4>\n\n\n\n<p>First, define the GPIO that will be used for wake-up. In this example, it&#8217;s GPIO 27.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#define buttonPin  GPIO_NUM_27     \/\/ GPIO pin for pushbutton<\/code><\/pre>\n\n\n\n<p>In the <span class=\"rnthl rntliteral\">setup()<\/span>, we define the pushbutton as an input with a pull-down resistor.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>pinMode(buttonPin, INPUT_PULLDOWN); \/\/ pull-down resistor<\/code><\/pre>\n\n\n\n<p>Then, we enable the <strong>ext0 <\/strong>wake-up source on GPIO 27. We pass <span class=\"rnthl rntliteral\">1<\/span> for the level, which means it will wake-up the ESP32 on a HIGH signal\u2014because we&#8217;re using a pull-down resistor, it will receive a HIGH signal when you press the pushbutton.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ Enable wake-up by EXT0 using the button on GPIO 27\nesp_err_t result = esp_sleep_enable_ext0_wakeup(buttonPin, 1); \/\/ 1 = wake up on HIGH signal<\/code><\/pre>\n\n\n\n<p>In the <span class=\"rnthl rntliteral\">loop()<\/span>, we increase the <span class=\"rnthl rntliteral\">counter<\/span> variable, light up the onboard LED for five seconds and finally, call the <span class=\"rnthl rntliteral\">esp_light_sleep_start()<\/span> to put the board into light sleep mode.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>esp_light_sleep_start();    \/\/ Enter light sleep<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Testing the Code<\/h4>\n\n\n\n<p>Upload the code to your board. Open the Serial Monitor at a baud rate of 115200. Press the pushbutton to wake up the ESP32.<\/p>\n\n\n\n<p>Every time you press the pushbutton, the ESP32 will wake up, print, and increase the <span class=\"rnthl rntliteral\">counter<\/span> variable, light up the onboard LED for five seconds and will go back to sleep.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"666\" height=\"324\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/11\/ESP32-light-sleep-ext0-demonstration.png?resize=666%2C324&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 light sleep with external wake-up\" class=\"wp-image-163754\" style=\"width:666px;height:auto\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/11\/ESP32-light-sleep-ext0-demonstration.png?w=666&amp;quality=100&amp;strip=all&amp;ssl=1 666w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/11\/ESP32-light-sleep-ext0-demonstration.png?resize=300%2C146&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 666px) 100vw, 666px\" \/><\/figure><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"ext1-wake-up-source\">ext1 External Wake-Up<\/h3>\n\n\n\n<p>You can wake-up the ESP32 using multiple GPIOs with this wake-up source. The <strong>ext1<\/strong> wake-up source is implemented by the RTC controller. So, RTC peripherals and RTC memories can be powered off in this mode.<\/p>\n\n\n\n<p>Use the <span class=\"rnthl rntliteral\">esp_sleep_enable_ext1_wakeup_io()<\/span> function. Pass as arguments the bitmask of the GPIOs used for wake-up, and the mode\/logic for waking up.<\/p>\n\n\n\n<p>You can use one of the following two logics:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span class=\"rnthl rntliteral\">ESP_EXT1_WAKEUP_ALL_LOW<\/span>: wake up when all GPIOs go low;<\/li>\n\n\n\n<li><span class=\"rnthl rntliteral\">ESP_EXT1_WAKEUP_ANY_HIGH<\/span>: wake up if any of the GPIOs go high.<\/li>\n<\/ul>\n\n\n\n<p>If you\u2019re using an ESP32-S2, ESP32-S3, ESP32-C6 or ESP32-H2, these are the available modes:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span class=\"rnthl rntliteral\">ESP_EXT1_WAKEUP_ANY_LOW<\/span>: wake up when any of the selected GPIOs is low<\/li>\n\n\n\n<li><span class=\"rnthl rntliteral\">ESP_EXT1_WAKEUP_ANY_HIGH<\/span>: wake up when any of the selected GPIOs is high<\/li>\n<\/ul>\n\n\n\n<p><strong>Note<\/strong>: you can only use pins that are RTC GPIOs.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Light Sleep with External Wake-Up () Example (ext1)<\/h4>\n\n\n\n<p>In the following example, we&#8217;ll use two pushbuttons to wake up the ESP32. The ESP32 will wake up when any of the pushbuttons are pressed.<\/p>\n\n\n\n<p>To test this example, we&#8217;ll connect a pushbutton to GPIO27 and another to GPIO26.  We&#8217;ll be using the ESP32 internal pull-down resistors. You can use the following diagram as a reference to wire your circuit.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"808\" height=\"747\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/11\/ESP32-with-two-pushbuttons-internal-pull-down-resistors.png?resize=808%2C747&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 with two pushbuttons circuit\" class=\"wp-image-163756\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/11\/ESP32-with-two-pushbuttons-internal-pull-down-resistors.png?w=808&amp;quality=100&amp;strip=all&amp;ssl=1 808w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/11\/ESP32-with-two-pushbuttons-internal-pull-down-resistors.png?resize=300%2C277&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/11\/ESP32-with-two-pushbuttons-internal-pull-down-resistors.png?resize=768%2C710&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 808px) 100vw, 808px\" \/><\/figure><\/div>\n\n\n<p>And this is the code you should upload to your board.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">\/*********\n  Rui Santos &amp; Sara Santos - Random Nerd Tutorials\n  Complete project details at https:\/\/RandomNerdTutorials.com\/esp32-light-sleep-arduino\/\n*********\/\n\n#include &quot;esp_sleep.h&quot;\n#include &quot;driver\/rtc_io.h&quot;\n\nint counter = 0;\nconst int ledPin = 2;                      \/\/ GPIO pin for onboard LED\nconst gpio_num_t buttonPin1 = GPIO_NUM_26; \/\/ RTC IO for pushbutton 1\nconst gpio_num_t buttonPin2 = GPIO_NUM_27; \/\/ RTC IO for pushbutton 2\n#define BUTTON_PIN_BITMASK(GPIO) (1ULL &lt;&lt; GPIO)  \/\/ Macro for individual GPIO bitmask\n\n\/\/ Create a bitmask for GPIO 26 and GPIO 27\nuint64_t bitmask = BUTTON_PIN_BITMASK(buttonPin1) | BUTTON_PIN_BITMASK(buttonPin2);\n\n\/\/ Method to print the GPIO that triggered the wakeup\nvoid print_GPIO_wake_up(){\n  int GPIO_reason = esp_sleep_get_ext1_wakeup_status();\n  Serial.print(&quot;GPIO that triggered the wake up: GPIO &quot;);\n  Serial.println((log(GPIO_reason))\/log(2), 0);\n}\n\nvoid setup() {\n    Serial.begin(115200);\n    pinMode(ledPin, OUTPUT);\n    pinMode(buttonPin1, INPUT_PULLDOWN); \/\/ pull-down resistor\n    pinMode(buttonPin2, INPUT_PULLDOWN); \/\/ pull-down resistor\n\n    \/\/ Configure GPIO 26 and GPIO 27 as RTC IOs for EXT1 wake-up\n    rtc_gpio_deinit(buttonPin1);\n    rtc_gpio_deinit(buttonPin2);\n\n    \/\/ Enable EXT1 wake-up source\n    esp_err_t result = esp_sleep_enable_ext1_wakeup(bitmask, ESP_EXT1_WAKEUP_ANY_HIGH);\n\n    if (result == ESP_OK) {\n        Serial.println(&quot;EXT1 Wake-Up set successfully.&quot;);\n    } else {\n        Serial.println(&quot;Failed to set EXT1 Wake-Up as wake-up source.&quot;);\n    }\n}\n\nvoid loop() {\n    Serial.printf(&quot;Counter: %d\\n&quot;, counter);\n    counter++;\n\n    digitalWrite(ledPin, HIGH); \/\/ LED on to indicate wake-up\n    delay(2000);\n    digitalWrite(ledPin, LOW);  \/\/ Turn off LED before going to sleep\n\n    Serial.println(&quot;Going into light sleep mode&quot;);\n    delay(500);\n    esp_light_sleep_start();    \/\/ Enter light sleep\n\n    \/\/ After wake-up, disable the hold function on the RTC GPIOs\n    rtc_gpio_hold_dis(buttonPin1);\n    rtc_gpio_hold_dis(buttonPin2);\n\n    Serial.println(&quot;----------------------&quot;);\n    Serial.println(&quot;Returning from light sleep&quot;);\n    \/\/ Print the GPIO (button) that caused the wake-up\n    print_GPIO_wake_up();\n}<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/ESP32\/ESP32_Light_Sleep\/ESP32_Light_Sleep_ext1.ino\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<p>When you press any of the buttons, the ESP32 will wake-up and print which GPIO\/button was pressed.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"768\" height=\"366\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/11\/ESP32-light-sleep-ext1-multiple-gpios.png?resize=768%2C366&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 Light Sleep External Wake-Up ext1 Demonstration\" class=\"wp-image-163758\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/11\/ESP32-light-sleep-ext1-multiple-gpios.png?w=768&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/11\/ESP32-light-sleep-ext1-multiple-gpios.png?resize=300%2C143&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 768px) 100vw, 768px\" \/><\/figure><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"gpio-external-wake-up\">GPIO External Wake-Up<\/h3>\n\n\n\n<p>Besides the <strong>ext0 <\/strong>and <strong>ext1 <\/strong>methods, there is another method that allows you to configure individual GPIOs to wake up the ESP32 on either HIGH or LOW signals. With this method, you can use any GPIO, whether it&#8217;s an RTC GPIO or a &#8220;regular&#8221; digital input.<\/p>\n\n\n\n<p><strong>1) <\/strong>To use this method, first, you need to use the <span class=\"rnthl rntliteral\">gpio_wakeup_enable()<\/span> function to define the GPIOs you want to use as a wake-up source and the corresponding level. The GPIO number must be in this format <span class=\"rnthl rntliteral\">GPIO_NUM_X<\/span> (X is the number of the GPIO). The level can be one of the following options:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span class=\"rnthl rntliteral\">GPIO_INTR_LOW_LEVEL<\/span> &#8211; wake up the ESP32 when the GPIO goes LOW<\/li>\n\n\n\n<li><span class=\"rnthl rntliteral\">GPIO_INTR_HIGH_LEVEL<\/span> &#8211; wake up the ESP32 when the GPIO goes HIGH<\/li>\n<\/ul>\n\n\n\n<p><strong>2)<\/strong> You can call the <span class=\"rnthl rntliteral\">gpio_wakeup_enable()<\/span> function several times to set up multiple GPIOs.<\/p>\n\n\n\n<p><strong>3)<\/strong> After setting the GPIOs that will wake-up the ESP32, you must call the <span class=\"rnthl rntliteral\">esp_sleep_enable_gpio_wakeup()<\/span> function to enable this wake-up source.<\/p>\n\n\n\n<p>Then, you just need to call the <span class=\"rnthl rntliteral\">esp_light_sleep_start()<\/span> function to put the ESP32 in light sleep state.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Light Sleep with GPIO External Wake-Up Example<\/h4>\n\n\n\n<p>The following example works exactly like the previous one, but we use this new method. We use interrupts on the pushbuttons to determine which one was the cause of the wake-up.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">\/*********\n  Rui Santos &amp; Sara Santos - Random Nerd Tutorials\n  Complete project details at https:\/\/RandomNerdTutorials.com\/esp32-light-sleep-arduino\/\n*********\/\n\n#include &quot;esp_sleep.h&quot;\n#include &quot;driver\/gpio.h&quot;\n\nint counter = 0;\nconst int ledPin = 2;                      \/\/ GPIO pin for onboard LED\nconst gpio_num_t buttonPin1 = GPIO_NUM_26; \/\/ GPIO for pushbutton 1\nconst gpio_num_t buttonPin2 = GPIO_NUM_27; \/\/ GPIO for pushbutton 2\n\nint wakeup_gpio; \/\/ Variable to store the GPIO that caused wake-up\n\n\/\/ ISR for buttonPin1\nvoid IRAM_ATTR handleInterrupt1() {\n    wakeup_gpio = buttonPin1;\n}\n\n\/\/ ISR for buttonPin2\nvoid IRAM_ATTR handleInterrupt2() {\n    wakeup_gpio = buttonPin2;\n}\n\nvoid setup() {\n    Serial.begin(115200);\n    pinMode(ledPin, OUTPUT);\n    pinMode(buttonPin1, INPUT_PULLDOWN); \/\/ pull-down resistor\n    pinMode(buttonPin2, INPUT_PULLDOWN); \/\/ pull-down resistor\n\n    \/\/ Configure GPIOs as wake-up source\n    gpio_wakeup_enable(buttonPin1, GPIO_INTR_HIGH_LEVEL); \/\/ Trigger wake-up on high level\n    gpio_wakeup_enable(buttonPin2, GPIO_INTR_HIGH_LEVEL); \/\/ Trigger wake-up on high level\n\n    \/\/ Enable GPIO wake-up source\n    esp_err_t result = esp_sleep_enable_gpio_wakeup();\n\n    if (result == ESP_OK) {\n        Serial.println(&quot;GPIO Wake-Up set successfully.&quot;);\n    } else {\n        Serial.println(&quot;Failed to set GPIO Wake-Up as wake-up source.&quot;);\n    }\n\n    \/\/ Attach interrupts to GPIO pins\n    attachInterrupt(digitalPinToInterrupt(buttonPin1), handleInterrupt1, RISING);\n    attachInterrupt(digitalPinToInterrupt(buttonPin2), handleInterrupt2, RISING);\n}\n\nvoid loop() {\n    Serial.printf(&quot;Counter: %d\\n&quot;, counter);\n    counter++;\n\n    digitalWrite(ledPin, HIGH); \/\/ LED on to indicate wake-up\n    delay(5000);\n    digitalWrite(ledPin, LOW);  \/\/ Turn off LED before going to sleep\n\n    Serial.println(&quot;Going into light sleep mode&quot;);\n    delay(500);\n    esp_light_sleep_start();    \/\/ Enter light sleep\n\n    Serial.println(&quot;----------------------&quot;);\n    Serial.println(&quot;Returning from light sleep&quot;);\n\n    \/\/ Print the GPIO that caused the wake-up\n    Serial.printf(&quot;Wake-up caused by GPIO %d\\n&quot;, wakeup_gpio);\n}<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/ESP32\/ESP32_Light_Sleep\/ESP32_Light_Sleep_Multiple_GPIOs_Wake_Up.ino\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<p>In this code, you start by defining the GPIOs connected to the pushbuttons in the following format:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>const gpio_num_t buttonPin1 = GPIO_NUM_26; \/\/ GPIO for pushbutton 1\nconst gpio_num_t buttonPin2 = GPIO_NUM_27; \/\/ GPIO for pushbutton 2<\/code><\/pre>\n\n\n\n<p>We create a global variable to save the GPIO that caused the wake-up.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>int wakeup_gpio; \/\/ Variable to store the GPIO that caused wake-up<\/code><\/pre>\n\n\n\n<p>In the <span class=\"rnthl rntliteral\">setup()<\/span>, use the <span class=\"rnthl rntliteral\">gpio_wakeup_enable()<\/span> function to set the GPIOs as wake-up sources and the level.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ Configure GPIOs as wake-up source\ngpio_wakeup_enable(buttonPin1, GPIO_INTR_HIGH_LEVEL); \/\/ Trigger wake-up on high level\ngpio_wakeup_enable(buttonPin2, GPIO_INTR_HIGH_LEVEL); \/\/ Trigger wake-up on high level<\/code><\/pre>\n\n\n\n<p>Then, to enable the GPIOs as a wake-up source, call the <span class=\"rnthl rntliteral\">esp_sleep_enable_gpio_wakeup()<\/span> function.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ Enable GPIO wake-up source\nesp_err_t result = esp_sleep_enable_gpio_wakeup();<\/code><\/pre>\n\n\n\n<p>Still in the <span class=\"rnthl rntliteral\">setup()<\/span>, we set the pushbuttons as interrupts so that they trigger a function when they&#8217;re pushed. This allows us to check which GPIO caused the wake-up.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ Attach interrupts to GPIO pins\nattachInterrupt(digitalPinToInterrupt(buttonPin1), handleInterrupt1, RISING);\nattachInterrupt(digitalPinToInterrupt(buttonPin2), handleInterrupt2, RISING);<\/code><\/pre>\n\n\n\n<p class=\"rntbox rntclblue\"><strong>Recommended reading<\/strong>: <a href=\"https:\/\/randomnerdtutorials.com\/esp32-pir-motion-sensor-interrupts-timers\/\" title=\"\">Interrupts with the ESP32 using a PIR Motion Sensor<\/a>.<\/p>\n\n\n\n<p>The interrupt handler functions are defined before the <span class=\"rnthl rntliteral\">setup()<\/span>, and update the <span class=\"rnthl rntliteral\">wakeup_gpio<\/span> variable.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ISR for buttonPin1\nvoid IRAM_ATTR handleInterrupt1() {\n    wakeup_gpio = buttonPin1;\n}\n\n\/\/ISR for buttonPin2\nvoid IRAM_ATTR handleInterrupt2() {\n    wakeup_gpio = buttonPin2;\n}<\/code><\/pre>\n\n\n\n<p>In the <span class=\"rnthl rntliteral\">loop()<\/span>, like in previous examples, we update the <span class=\"rnthl rntliteral\">counter<\/span> variable, light up the onboard LED, and put the board into light sleep mode.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>Serial.printf(\"Counter: %d\\n\", counter);\ncounter++;\n\ndigitalWrite(ledPin, HIGH); \/\/ LED on to indicate wake-up\ndelay(5000);\ndigitalWrite(ledPin, LOW);  \/\/ Turn off LED before going to sleep\n\nSerial.println(\"Going into light sleep mode\");\ndelay(500);\nesp_light_sleep_start();    \/\/ Enter light sleep<\/code><\/pre>\n\n\n\n<p>After waking up from sleep, we print the GPIO that caused the wake-up.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>Serial.println(\"----------------------\");\nSerial.println(\"Returning from light sleep\");\n\n\/\/ Print the GPIO that caused the wake-up\nSerial.printf(\"Wake-up caused by GPIO %d\\n\", wakeup_gpio);<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"touch-wake-up\">Light Sleep with Touch Wake-Up<\/h2>\n\n\n\n<p>It&#8217;s possible to wake up the ESP32 from light sleep when touching its touch pins\u2014basically when a touch sensor interrupt occurs. For that, use the <span class=\"rnthl rntliteral\">touchSleepWakeUpEnable()<\/span> function that accepts as argument the touch pin and the threshold to wake up. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>touchSleepWakeUpEnable(T3, THRESHOLD);<\/code><\/pre>\n\n\n\n<p>The values read by a touch pin decrease when you touch it. The threshold value means that the ESP32 will wake up when the value read on the touch pin is below 40. You can adjust that value depending on the desired sensitivity.<\/p>\n\n\n\n<p class=\"rntbox rntclgray\"><strong>Recommended reading:<\/strong> <a href=\"https:\/\/randomnerdtutorials.com\/esp32-touch-pins-arduino-ide\/\">ESP32 Touch Pins with Arduino IDE<\/a>.<\/p>\n\n\n\n<p><strong>Important:<\/strong>&nbsp;however, if you\u2019re using an ESP32-S2 or ESP32-S3 model, things work a little differently. In that case, the lower the value, the more the sensitivity.<\/p>\n\n\n\n<p><strong>Note<\/strong>: the touchpins are numbered as follows (in case of the ESP32):<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>T0 (GPIO 4)<\/li>\n\n\n\n<li>T1 (GPIO 0)<\/li>\n\n\n\n<li>T2 (GPIO 2)<\/li>\n\n\n\n<li>T3 (GPIO 15)<\/li>\n\n\n\n<li>T4 (GPIO 13)<\/li>\n\n\n\n<li>T5 (GPIO 12)<\/li>\n\n\n\n<li>T6 (GPIO 14)<\/li>\n\n\n\n<li>T7 (GPIO 27)<\/li>\n\n\n\n<li>T8 (GPIO 33)<\/li>\n\n\n\n<li>T9 (GPIO 32)<\/li>\n<\/ul>\n\n\n\n<p>If you&#8217;re using an ESP32-S2 or <a href=\"https:\/\/randomnerdtutorials.com\/esp32-s3-devkitc-pinout-guide\/\" title=\"\">ESP32-S3<\/a>, the numbering is different.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Light Sleep with Touch Wake-Up Example<\/h3>\n\n\n\n<p>The following code will wake-up the ESP32 when it senses touch on GPIO 15 (T3) or GPIO27 (T7).<\/p>\n\n\n\n<p><strong>Note<\/strong>: I tested this wake-up source with light sleep, and in my case, it was a bit unreliable. It worked well sometimes, and others, the ESP32 crashed after waking up. I&#8217;m not sure if it&#8217;s because of my hardware or the code. Try it yourself and check your results. (Let me know if you have any tips to make this method more reliable).<\/p>\n\n\n\n<p><strong>Note<\/strong>: If you&#8217;re using ESP32-S2 or ESP32-S3, it can only detect touch wake-up on a single GPIO.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">\/*********\n  Rui Santos &amp; Sara Santos - Random Nerd Tutorials\n  Complete project details at https:\/\/RandomNerdTutorials.com\/esp32-light-sleep-arduino\/\n*********\/\n\n#if CONFIG_IDF_TARGET_ESP32\n  #define THRESHOLD 40    \/\/ Greater the value, more the sensitivity \n#else                     \/\/ ESP32-S2 and ESP32-S3 + default for other chips (to be adjusted)\n  #define THRESHOLD 5000  \/\/ Lower the value, more the sensitivity\n#endif\n\nint counter = 0;\nconst int ledPin = 2;  \/\/ GPIO pin for onboard LED\ntouch_pad_t touchPin;\n\n\/\/Method to print the reason by which ESP32 has been awaken from sleep\nvoid print_wakeup_reason() {\n  esp_sleep_wakeup_cause_t wakeup_reason;\n\n  wakeup_reason = esp_sleep_get_wakeup_cause();\n\n  switch (wakeup_reason) {\n    case ESP_SLEEP_WAKEUP_EXT0:     Serial.println(&quot;Wakeup caused by external signal using RTC_IO&quot;); break;\n    case ESP_SLEEP_WAKEUP_EXT1:     Serial.println(&quot;Wakeup caused by external signal using RTC_CNTL&quot;); break;\n    case ESP_SLEEP_WAKEUP_TIMER:    Serial.println(&quot;Wakeup caused by timer&quot;); break;\n    case ESP_SLEEP_WAKEUP_TOUCHPAD: Serial.println(&quot;Wakeup caused by touchpad&quot;); break;\n    case ESP_SLEEP_WAKEUP_ULP:      Serial.println(&quot;Wakeup caused by ULP program&quot;); break;\n    default:                        Serial.printf(&quot;Wakeup was not caused by deep sleep: %d\\n&quot;, wakeup_reason); break;\n  }\n}\n\n\/\/Method to print the touchpad by which ESP32 has been awaken from sleep\nvoid print_wakeup_touchpad() {\n  touchPin = esp_sleep_get_touchpad_wakeup_status();\n\n#if CONFIG_IDF_TARGET_ESP32\n  switch (touchPin) {\n    case 0:  Serial.println(&quot;Touch detected on GPIO 4&quot;); break;\n    case 1:  Serial.println(&quot;Touch detected on GPIO 0&quot;); break;\n    case 2:  Serial.println(&quot;Touch detected on GPIO 2&quot;); break;\n    case 3:  Serial.println(&quot;Touch detected on GPIO 15&quot;); break;\n    case 4:  Serial.println(&quot;Touch detected on GPIO 13&quot;); break;\n    case 5:  Serial.println(&quot;Touch detected on GPIO 12&quot;); break;\n    case 6:  Serial.println(&quot;Touch detected on GPIO 14&quot;); break;\n    case 7:  Serial.println(&quot;Touch detected on GPIO 27&quot;); break;\n    case 8:  Serial.println(&quot;Touch detected on GPIO 33&quot;); break;\n    case 9:  Serial.println(&quot;Touch detected on GPIO 32&quot;); break;\n    default: Serial.println(&quot;Wakeup not by touchpad&quot;); break;\n  }\n#else\n  if (touchPin &lt; TOUCH_PAD_MAX) {\n    Serial.printf(&quot;Touch detected on GPIO %d\\n&quot;, touchPin);\n  } else {\n    Serial.println(&quot;Wakeup not by touchpad&quot;);\n  }\n#endif\n}\n\nvoid setup() {\n  Serial.begin(115200);\n  pinMode(ledPin, OUTPUT);\n\n  delay(1000);  \/\/Take some time to open up the Serial Monitor\n\n#if CONFIG_IDF_TARGET_ESP32\n  \/\/Setup sleep wakeup on Touch Pad 3 + 7 (GPIO15 + GPIO 27)\n  touchSleepWakeUpEnable(T3, THRESHOLD);\n  touchSleepWakeUpEnable(T7, THRESHOLD);\n#else  \/\/ESP32-S2 + ESP32-S3\n  \/\/Setup sleep wakeup on Touch Pad 3 (GPIO3)\n  touchSleepWakeUpEnable(T3, THRESHOLD);\n#endif\n\n}\n\nvoid loop() {\n  Serial.printf(&quot;Counter: %d\\n&quot;, counter);\n  counter++;\n\n  digitalWrite(ledPin, HIGH); \/\/ LED on to indicate wake-up\n  delay(5000);\n  digitalWrite(ledPin, LOW);  \/\/ LED on to indicate wake-up\n\n  Serial.println(&quot;Going into light sleep...&quot;);\n  delay(500);\n  esp_light_sleep_start(); \n  \n  Serial.println(&quot;----------------------&quot;);\n  Serial.println(&quot;Returning from light sleep&quot;);  \n  delay(2000);\n  \n  print_wakeup_reason();\n  print_wakeup_touchpad();\n}<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/ESP32\/ESP32_Light_Sleep\/ESP32_Light_Sleep_Touch_Wake_Up.ino\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">How Does the Code Work?<\/h4>\n\n\n\n<p>Let&#8217;s take a quick look at the relevant parts of this code.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Setting the Threshold<\/h4>\n\n\n\n<p>The first thing you need to do is setting a threshold value for the touch pins.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#define THRESHOLD 40 \/\/ Greater the value, more the sensitivity<\/code><\/pre>\n\n\n\n<p>The values read by a touch pin decrease when you touch it. The threshold value means that the ESP32 will wake up when the value read on the touch pin is below 40. You can adjust that value depending on the desired sensitivity.<\/p>\n\n\n\n<p><strong>Important:<\/strong> however, if you\u2019re using an ESP32-S2 or ESP32-S3 model, things work a little<br>differently. That\u2019s why there\u2019s a different section in the code defining a different threshold for those boards. In this case, the lower the value, the more the sensitivity.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#if CONFIG_IDF_TARGET_ESP32\n#define THRESHOLD 40 \/\/ Greater the value, more the sensitivity\n#else \/\/ ESP32-S2 and ESP32-S3 + default for other chips (to be adjusted)\n#define THRESHOLD 5000 \/\/ Lower the value, more the sensitivity\n#endif<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Wake-Up Reason and Wake-Up Touch Pad<\/h4>\n\n\n\n<p>We have a function called <span class=\"rnthl rntliteral\">print_wakeup_reason()<\/span> to check and print what caused the wake-up.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/Method to print the reason by which ESP32 has been awaken from sleep\nvoid print_wakeup_reason() {\n  esp_sleep_wakeup_cause_t wakeup_reason;\n\n  wakeup_reason = esp_sleep_get_wakeup_cause();\n\n  switch (wakeup_reason) {\n    case ESP_SLEEP_WAKEUP_EXT0:     Serial.println(\"Wakeup caused by external signal using RTC_IO\"); break;\n    case ESP_SLEEP_WAKEUP_EXT1:     Serial.println(\"Wakeup caused by external signal using RTC_CNTL\"); break;\n    case ESP_SLEEP_WAKEUP_TIMER:    Serial.println(\"Wakeup caused by timer\"); break;\n    case ESP_SLEEP_WAKEUP_TOUCHPAD: Serial.println(\"Wakeup caused by touchpad\"); break;\n    case ESP_SLEEP_WAKEUP_ULP:      Serial.println(\"Wakeup caused by ULP program\"); break;\n    default:                        Serial.printf(\"Wakeup was not caused by deep sleep: %d\\n\", wakeup_reason); break;\n  }\n}<\/code><\/pre>\n\n\n\n<p>And a function called <span class=\"rnthl rntliteral\">print_wakeup_touchpad()<\/span> to determine which touch pin caused the wake-up.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ Method to print the touchpad by which ESP32 has been awaken from sleep\nvoid print_wakeup_touchpad() {\n  touchPin = esp_sleep_get_touchpad_wakeup_status();\n\n#if CONFIG_IDF_TARGET_ESP32\n  switch (touchPin) {\n    case 0:  Serial.println(\"Touch detected on GPIO 4\"); break;\n    case 1:  Serial.println(\"Touch detected on GPIO 0\"); break;\n    case 2:  Serial.println(\"Touch detected on GPIO 2\"); break;\n    case 3:  Serial.println(\"Touch detected on GPIO 15\"); break;\n    case 4:  Serial.println(\"Touch detected on GPIO 13\"); break;\n    case 5:  Serial.println(\"Touch detected on GPIO 12\"); break;\n    case 6:  Serial.println(\"Touch detected on GPIO 14\"); break;\n    case 7:  Serial.println(\"Touch detected on GPIO 27\"); break;\n    case 8:  Serial.println(\"Touch detected on GPIO 33\"); break;\n    case 9:  Serial.println(\"Touch detected on GPIO 32\"); break;\n    default: Serial.println(\"Wakeup not by touchpad\"); break;\n  }\n#else\n  if (touchPin &lt; TOUCH_PAD_MAX) {\n    Serial.printf(\"Touch detected on GPIO %d\\n\", touchPin);\n  } else {\n    Serial.println(\"Wakeup not by touchpad\");\n  }\n#endif<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Setting Touch Pins as a Wake-up Source<\/h4>\n\n\n\n<p>To set a touch pin as a wake-up source, you can use the <span class=\"rnthl rntliteral\">touchSleepWakeUpEnable()<\/span> function that accepts as arguments the touch pin and the threshold value that will wake up the board.<\/p>\n\n\n\n<p>In this example, it sets GPIO 15 (T3) and GPIO 27 (T7) as wake-up sources with the same threshold value.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#if CONFIG_IDF_TARGET_ESP32\n  \/\/ Setup sleep wakeup on Touch Pad 3 + 7 (GPIO15 + GPIO 27)\n  touchSleepWakeUpEnable(T3, THRESHOLD);\n  touchSleepWakeUpEnable(T7, THRESHOLD);<\/code><\/pre>\n\n\n\n<p>If you\u2019re using an ESP32-S2 or S3 model, the example just defines wake-up on GPIO 3 (T3). Note that the touch pin numbering might be different depending on the board model you\u2019re using.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#else \/\/ ESP32-S2 + ESP32-S3\n  \/\/ Setup sleep wakeup on Touch Pad 3 (GPIO3)\n  touchSleepWakeUpEnable(T3, THRESHOLD);<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">loop()<\/h4>\n\n\n\n<p>In each <span class=\"rnthl rntliteral\">loop()<\/span>, we increase the <span class=\"rnthl rntliteral\">counter<\/span> variable to keep track of the number of times the ESP32 has woken up. We also light up the onboard LED for a few seconds before putting the ESP32 into light sleep mode.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>void loop() {\n  Serial.printf(\"Counter: %d\\n\", counter);\n  counter++;\n\n  digitalWrite(ledPin, HIGH); \/\/ LED on to indicate wake-up\n  delay(5000);\n  digitalWrite(ledPin, LOW); \/\/ LED on to indicate wake-up\n\n  Serial.println(\"Going into light sleep...\");\n  delay(500);\n  esp_light_sleep_start(); <\/code><\/pre>\n\n\n\n<p>After waking up from sleep, the ESP32 prints the wake-up reason and which touch pin caused the wake-up.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>print_wakeup_reason();\nprint_wakeup_touchpad();<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Wiring the Circuit<\/h3>\n\n\n\n<p>To test this example, wire a jumper wire to GPIO 15 and\/or GPIO 27, as shown in the schematic below.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"858\" height=\"349\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/02\/Zero_Touch_Wake_Up_bb.png?resize=858%2C349&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Touch wake up Wiring the Circuit\" class=\"wp-image-81674\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/02\/Zero_Touch_Wake_Up_bb.png?w=858&amp;quality=100&amp;strip=all&amp;ssl=1 858w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/02\/Zero_Touch_Wake_Up_bb.png?resize=300%2C122&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/02\/Zero_Touch_Wake_Up_bb.png?resize=768%2C312&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 858px) 100vw, 858px\" \/><\/figure><\/div>\n\n\n<p>If you\u2019re using an ESP32-S2 or <a href=\"https:\/\/randomnerdtutorials.com\/esp32-s3-devkitc-pinout-guide\/\" title=\"\">ESP32-S3 model<\/a>, please check the location of your touch pins.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Testing the Example<\/h3>\n\n\n\n<p>Upload the code to your ESP32, and open the Serial Monitor at a baud rate of 115200.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"41\" height=\"39\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/02\/serial-monitor-logo-arduino-ide-2.png?resize=41%2C39&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 Open Serial Monitor with Arduino IDE\" class=\"wp-image-148549\"\/><\/figure><\/div>\n\n\n<p>The ESP32 goes into deep sleep mode.<\/p>\n\n\n\n<p>You can wake it up by touching the wire connected to Touch Pin 3 or Touch Pin 7.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"421\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/ESP32-Touch-Sensor-Pins-Arduino-IDE.jpg?resize=750%2C421&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 Touch Sensor Pins Arduino IDE\" class=\"wp-image-163125\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/ESP32-Touch-Sensor-Pins-Arduino-IDE.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/10\/ESP32-Touch-Sensor-Pins-Arduino-IDE.jpg?resize=300%2C168&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<p>When you touch the wire, the ESP32 displays on the Serial Monitor: the boot number, the wake-up cause, and which touch-sensitive GPIO caused the wake-up.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"666\" height=\"322\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/11\/ESP32-light-sleep-touch-wake-up-demonstration.png?resize=666%2C322&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 light sleep with touch wake-up demonstration\" class=\"wp-image-163736\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/11\/ESP32-light-sleep-touch-wake-up-demonstration.png?w=666&amp;quality=100&amp;strip=all&amp;ssl=1 666w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/11\/ESP32-light-sleep-touch-wake-up-demonstration.png?resize=300%2C145&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 666px) 100vw, 666px\" \/><\/figure><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"uart-wake-up\">Light Sleep with UART Wake-Up<\/h2>\n\n\n\n<p>The ESP32 can wake up from light sleep on UART input by enabling UART wake-up with <span class=\"rnthl rntliteral\">esp_sleep_enable_uart_wakeup()<\/span>. <\/p>\n\n\n\n<p>You should also call the <span class=\"rnthl rntliteral\">uart_set_wakeup_threshold()<\/span> function to set the number of positive edges on the RX pin that will cause the wake-up. Three positive edges is the number set for most cases.<\/p>\n\n\n\n<p>The following example shows how to wake up the ESP32 using UART. This specific example will wake up the ESP32 on UART 0. We&#8217;ll send a message via the Serial Monitor to wake up the ESP32. But, for a practical application, you&#8217;ll want to connect a sensor or another board to the ESP32 RX pin and send bytes via serial to wake it up. <\/p>\n\n\n\n<p>To learn more about UART with the ESP32, we recommend taking a look at our guide: <a href=\"https:\/\/randomnerdtutorials.com\/esp32-uart-communication-serial-arduino\/\" title=\"\">ESP32 UART Communication (Serial): Set Pins, Interfaces, Send and Receive Data (Arduino IDE)<\/a>. <\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">\/*********\n  Rui Santos &amp; Sara Santos - Random Nerd Tutorials\n  Complete project details at https:\/\/RandomNerdTutorials.com\/esp32-light-sleep-arduino\/\n*********\/\n\n#include &lt;driver\/uart.h&gt;\n\nint counter = 0;\nconst int ledPin = 2;         \/\/ GPIO pin for onboard LED\nString receivedMessage = &quot;&quot;;  \/\/ Variable to store the complete message\n\n\/\/ Method to print the reason by which ESP32 has been awaken from sleep\nvoid print_wakeup_reason() {\n  esp_sleep_wakeup_cause_t wakeup_reason;\n\n  wakeup_reason = esp_sleep_get_wakeup_cause();\n\n  switch (wakeup_reason) {\n    case ESP_SLEEP_WAKEUP_EXT0:     Serial.println(&quot;Wakeup caused by external signal using RTC_IO&quot;); break;\n    case ESP_SLEEP_WAKEUP_EXT1:     Serial.println(&quot;Wakeup caused by external signal using RTC_CNTL&quot;); break;\n    case ESP_SLEEP_WAKEUP_TIMER:    Serial.println(&quot;Wakeup caused by timer&quot;); break;\n    case ESP_SLEEP_WAKEUP_TOUCHPAD: Serial.println(&quot;Wakeup caused by touchpad&quot;); break;\n    case ESP_SLEEP_WAKEUP_ULP:      Serial.println(&quot;Wakeup caused by ULP program&quot;); break;\n    case ESP_SLEEP_WAKEUP_UART:     Serial.println(&quot;Wakeup caused by UART&quot;); break;\n    default:                        Serial.printf(&quot;Wakeup was not caused by light sleep: %d\\n&quot;, wakeup_reason); break;\n  }\n}\n\nvoid setup() {\n  Serial.begin(115200);\n  pinMode(ledPin, OUTPUT);\n\n  delay(1000);  \/\/Take some time to open up the Serial Monitor\n\n  \/\/ Enable UART wake-up from light sleep\n  uart_set_wakeup_threshold(UART_NUM_0, 3);  \/\/ 3 edges on U0RXD to wakeup\n  esp_sleep_enable_uart_wakeup(0);           \/\/ UART0 (default Serial (includes Serial Monitor))\n}\n\nvoid loop() {\n  Serial.printf(&quot;Counter: %d\\n&quot;, counter);\n  counter++;\n\n  digitalWrite(ledPin, HIGH); \/\/ LED on to indicate wake-up\n  delay(5000);\n  digitalWrite(ledPin, LOW); \/\/ LED on to indicate wake-up\n\n  Serial.println(&quot;Going into light sleep...&quot;);\n  delay(500);\n  esp_light_sleep_start(); \n  \n  Serial.println(&quot;----------------------&quot;);\n  Serial.println(&quot;Returning from light sleep&quot;);  \n  delay(2000);\n  \n  print_wakeup_reason();\n   \/\/ Clear the internal wake-up indication by sending some extra data\n  Serial.write(' ');   \/\/ Send a single space character\n\n while (Serial.available()) {\n    char incomingChar = Serial.read();  \/\/ Read each character from the buffer\n    \n    if (incomingChar == '\\n') {  \/\/ Check if the user pressed Enter (new line character)\n      \/\/ Print the message\n      Serial.print(&quot;You sent: &quot;);\n      Serial.println(receivedMessage);\n      \n      \/\/ Clear the message buffer for the next input\n      receivedMessage = &quot;&quot;;\n    } else {\n      \/\/ Append the character to the message string\n      receivedMessage += incomingChar;\n    }\n  }  \n}<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/ESP32\/ESP32_Light_Sleep\/ESP32_Light_Sleep_UART_Wake_Up.ino\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<p>This code is similar to previous examples but uses UART wake-up. We call the following two functions in the <span class=\"rnthl rntliteral\">setup()<\/span> to set up UART wake-up on UART0 (<a href=\"https:\/\/randomnerdtutorials.com\/esp32-uart-communication-serial-arduino\/#uart0-serial-monitor\" title=\"\">which is also the UART used for serial communication with the Serial Monitor)<\/a>.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ Enable UART wake-up from light sleep\nuart_set_wakeup_threshold(UART_NUM_0, 3);   \/\/ 3 edges on U0RXD to wakeup\nesp_sleep_enable_uart_wakeup(0); \/\/ UART0 (default Serial (includes Serial Monitor))<\/code><\/pre>\n\n\n\n<p>In the <span class=\"rnthl rntliteral\">loop()<\/span>, we call the <span class=\"rnthl rntliteral\">esp_light_sleep_start()<\/span> function to put the ESP32 in deep sleep mode.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>esp_light_sleep_start(); <\/code><\/pre>\n\n\n\n<p>When it wakes up from sleep, it prints the wake-up reason, and checks if it has any incoming UART data to read.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>print_wakeup_reason();\n\/\/ Clear the internal wake-up indication by sending some extra data\nSerial.write(' ');   \/\/ Send a single space character\n\nwhile (Serial.available()) {\n  char incomingChar = Serial.read();  \/\/ Read each character from the buffer\n    \n  if (incomingChar == '\\n') {  \/\/ Check if the user pressed Enter (new line character)\n    \/\/ Print the message\n    Serial.print(\"You sent: \");\n    Serial.println(receivedMessage);\n      \n    \/\/ Clear the message buffer for the next input\n    receivedMessage = \"\";\n  } else {\n    \/\/ Append the character to the message string\n    receivedMessage += incomingChar;\n  }\n}  <\/code><\/pre>\n\n\n\n<p>Then, it starts running the <span class=\"rnthl rntliteral\">loop()<\/span> from the start. It increases the <span class=\"rnthl rntliteral\">counter<\/span> number and lights up the onboard LED before going to sleep again.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>Serial.printf(\"Counter: %d\\n\", counter);\ncounter++;\n\ndigitalWrite(ledPin, HIGH); \/\/ LED on to indicate wake-up\ndelay(5000);\ndigitalWrite(ledPin, LOW); \/\/ LED on to indicate wake-up\n\nSerial.println(\"Going into light sleep...\");\ndelay(500);\nesp_light_sleep_start(); <\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Testing the Code<\/h3>\n\n\n\n<p>After uploading the code to the board, open the Serial Monitor at a baud rate of 115200. The ESP32 will go into light sleep mode.<\/p>\n\n\n\n<p>Send something to the ESP32 via Serial using the Serial Monitor\u2014a single character will do. The ESP32 will instantly wake up and resume the code where it left. If you want it to read serial data, you need to send it right after waking up.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"666\" height=\"306\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/11\/ESP32-light-sleep-uart-wake-up.png?resize=666%2C306&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 light sleep wake-up using UART\" class=\"wp-image-163739\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/11\/ESP32-light-sleep-uart-wake-up.png?w=666&amp;quality=100&amp;strip=all&amp;ssl=1 666w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/11\/ESP32-light-sleep-uart-wake-up.png?resize=300%2C138&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 666px) 100vw, 666px\" \/><\/figure><\/div>\n\n\n<h2 class=\"wp-block-heading\">Wrapping Up<\/h2>\n\n\n\n<p>In this tutorial, we&#8217;ve looked at light sleep with the ESP32 and different ways to wake it up. It supports timer wake-up, external wake-up using the GPIOs, using the touch pins, and even via UART.<\/p>\n\n\n\n<p>We hope you&#8217;ve found this tutorial useful. If you need a more power-saving option, you may want to take a look at deep sleep with the ESP32 instead:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-deep-sleep-arduino-ide-wake-up-sources\/\" title=\"\">ESP32 Deep Sleep with Arduino IDE and Wake Up Sources<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/micropython-esp32-deep-sleep-wake-up-sources\/\">MicroPython: ESP32 Deep Sleep and Wake Up Sources<\/a><\/li>\n<\/ul>\n\n\n\n<p>Learn more about the ESP32 with our resources:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/learn-esp32-with-arduino-ide\/\">Learn ESP32 with Arduino IDE (eBook)<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/projects-esp32\/\">Free ESP32 Projects and Tutorials<\/a><\/li>\n<\/ul>\n\n\n\n<p>Thanks for reading.<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Light Sleep is one of the power-saving modes supported by the ESP32. In this guide, we&#8217;ll look at how to implement the Light Sleep mode with the ESP32 and the &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"ESP32 Light Sleep Mode and Wake-Up Sources (Arduino IDE)\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/esp32-light-sleep-arduino\/#more-163571\" aria-label=\"Read more about ESP32 Light Sleep Mode and Wake-Up Sources (Arduino IDE)\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":5,"featured_media":163745,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[276,281,277,299,264],"tags":[],"class_list":["post-163571","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-esp32","category-esp32-project","category-esp32-arduino-ide","category-0-esp32","category-project"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2024\/11\/ESP32-Light-Sleep-and-wake-up-sources.jpg?fit=1920%2C1080&quality=100&strip=all&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/163571","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/comments?post=163571"}],"version-history":[{"count":29,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/163571\/revisions"}],"predecessor-version":[{"id":168242,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/163571\/revisions\/168242"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/163745"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=163571"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=163571"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=163571"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}