{"id":85320,"date":"2019-05-16T15:08:56","date_gmt":"2019-05-16T15:08:56","guid":{"rendered":"https:\/\/randomnerdtutorials.com\/?p=85320"},"modified":"2019-05-16T15:19:27","modified_gmt":"2019-05-16T15:19:27","slug":"micropython-esp32-deep-sleep-wake-up-sources","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/micropython-esp32-deep-sleep-wake-up-sources\/","title":{"rendered":"MicroPython: ESP32 Deep Sleep and Wake Up Sources"},"content":{"rendered":"\n<p>This tutorial shows how to put the ESP32 in deep sleep mode and wake it up using different wake up sources using MicroPython firmware. We&#8217;ll cover timer wake up and external wake up.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" width=\"1200\" height=\"675\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/esp32-deep-sleep-micropython.jpg?resize=1200%2C675&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"MicroPython Deep Sleep with ESP32 timer wake up external wake up\" class=\"wp-image-85357\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/esp32-deep-sleep-micropython.jpg?w=1280&amp;quality=100&amp;strip=all&amp;ssl=1 1280w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/esp32-deep-sleep-micropython.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/esp32-deep-sleep-micropython.jpg?resize=768%2C432&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/esp32-deep-sleep-micropython.jpg?resize=1024%2C576&amp;quality=100&amp;strip=all&amp;ssl=1 1024w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/figure><\/div>\n\n\n\n<p>The ESP32 can also be awaken from deep sleep using the touch pins by defining a threshold. Although this feature is already implemented in MicroPython, it is not working properly at the time of writing this tutorial. We&#8217;ll update this article as soon as it is working.<\/p>\n\n\n\n<p>Deep sleep with the ESP8266 works a bit different than with the ESP32. So, we&#8217;ve created a dedicated article to deep sleep with ESP8266 using MicroPython.<\/p>\n\n\n\n<p>If you have an ESP8266, we recommend reading our <a href=\"https:\/\/randomnerdtutorials.com\/micropython-esp8266-deep-sleep-wake-up-sources\/\">MicroPython ESP8266 Deep Sleep and Wake Up Sources Guide<\/a>. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Prerequisites<\/h2>\n\n\n\n<p>To follow this tutorial you need MicroPython firmware installed in your ESP32 or ESP8266 boards. You also need an IDE to write and upload the code to your board. We suggest using Thonny IDE or uPyCraft IDE:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Thonny IDE:<ul><li><a href=\"https:\/\/randomnerdtutorials.com\/getting-started-thonny-micropython-python-ide-esp32-esp8266\/\">Installing and getting started with Thonny IDE<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/flashing-micropython-firmware-esptool-py-esp32-esp8266\/\">Flashing MicroPython Firmware with esptool.py<\/a><\/li><\/ul><\/li><li>uPyCraft IDE:<ul><li><a href=\"https:\/\/randomnerdtutorials.com\/getting-started-micropython-esp32-esp8266\/\">Getting Started with uPyCraft IDE<\/a><\/li><li>Install uPyCraft IDE (<a href=\"https:\/\/randomnerdtutorials.com\/install-upycraft-ide-windows-pc-instructions\/\">Windows<\/a>,&nbsp;<a href=\"https:\/\/randomnerdtutorials.com\/install-upycraft-ide-mac-os-x-instructions\/\">Mac OS X<\/a>,&nbsp;<a href=\"https:\/\/randomnerdtutorials.com\/install-upycraft-ide-linux-ubuntu-instructions\/\">Linux<\/a>)<\/li><\/ul><ul><li><a href=\"https:\/\/randomnerdtutorials.com\/flash-upload-micropython-firmware-esp32-esp8266\/\">Flash\/Upload MicroPython Firmware to ESP32 and ESP8266<\/a><\/li><\/ul><\/li><\/ul>\n\n\n\n<p class=\"rntbox rntclblue\">Learn more about MicroPython: <a href=\"https:\/\/randomnerdtutorials.com\/micropython-programming-with-esp32-and-esp8266\/\">MicroPython Programming with ESP32 and ESP8266 eBook<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introducing Deep Sleep<\/h2>\n\n\n\n<p>Having your ESP32\/ESP8266 running on active mode with batteries it\u2019s not ideal, since the power from batteries will drain very quickly.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"244\" height=\"122\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/02\/drain-batteries.jpg?resize=244%2C122&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" class=\"wp-image-81670\"\/><\/figure><\/div>\n\n\n\n<p>If you put your ESP32 in deep sleep mode, it will reduce the power consumption and your batteries will last longer. Having the ESP32 in deep sleep mode means cutting with the activities that consume more power while operating but leave just enough activity to wake up the processor when something interesting happens.<\/p>\n\n\n\n<p>When operating in deep sleep mode, the ESP32 have a current consumption on the \u03bcA range. With a custom and carefully designed board you can get a minimal comsumption of only 5 \u03bcA. However, if you use a full-feature ESP32 development board with built-in programmer, on-board LEDs, and so on (like the ESP32 DOIT board) you won\u2019t be able to achieve such a low power state, but you can still save power.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Wake Up Sources<\/h3>\n\n\n\n<p>After putting the ESP32 into deep sleep mode, there are several ways to wake it up:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>You can use the <strong>timer<\/strong>: waking up the ESP32 after predefined periods of time.<\/li><li>You can use an <strong>external wake up<\/strong>: this means the ESP32 can wake up when a change in the state of a pin occurs.<\/li><li>You can use the <strong>touch pins<\/strong>: implemented, but not working as expected at the time of writing, so we won&#8217;t cover this at the moment;<\/li><li>You can use the ULP co-processor to wake up: we haven&#8217;t tested this feature yet.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Timer Wake Up<\/h2>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter is-resized\"><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/02\/timer-wake-up.png?resize=150%2C150&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" class=\"wp-image-81689\" width=\"150\" height=\"150\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/02\/timer-wake-up.png?w=300&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/02\/timer-wake-up.png?resize=150%2C150&amp;quality=100&amp;strip=all&amp;ssl=1 150w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/02\/timer-wake-up.png?resize=80%2C80&amp;quality=100&amp;strip=all&amp;ssl=1 80w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/02\/timer-wake-up.png?resize=220%2C220&amp;quality=100&amp;strip=all&amp;ssl=1 220w\" sizes=\"(max-width: 150px) 100vw, 150px\" \/><\/figure><\/div>\n\n\n\n<p>The ESP32 can go into deep sleep mode, and then wake up at  predefined periods of time. This feature is especially useful if you are running projects that require time stamping or daily tasks, while maintaining low power consumption.<\/p>\n\n\n\n<p>To put the ESP32 in deep sleep mode for a predetermined number of seconds, you just have to use the <span class=\"rnthl rntliteral\">deepsleep()<\/span> function from the <span class=\"rnthl rntliteral\">machine<\/span> module. This function accepts as arguments, the sleep time in milliseconds as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>machine.deepsleep(sleep_time_ms)<\/code><\/pre>\n\n\n\n<p>Let\u2019s look at a simple example to see how it works. In the following code, the ESP32 is in deep sleep mode for 10 seconds, then it wakes up, blinks an LED, and goes back to sleep.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-python\"># Complete project details at https:\/\/RandomNerdTutorials.com\/micropython-programming-with-esp32-and-esp8266\/\n\nfrom machine import deepsleep\nfrom machine import Pin\nfrom time import sleep\n\nled = Pin (2, Pin.OUT)\n\n#blink LED\nled.value(1)\nsleep(1)\nled.value(0)\nsleep(1)\n\n# wait 5 seconds so that you can catch the ESP awake to establish a serial communication later\n# you should remove this sleep line in your final script\nsleep(5)\n\nprint('Im awake, but Im going to sleep')\n\n#sleep for 10 seconds (10000 milliseconds)\ndeepsleep(10000)\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/ESP-MicroPython\/raw\/master\/code\/GPIOs\/Deep_sleep\/deep_sleep_esp32.py\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How the Code Works<\/h3>\n\n\n\n<p>First, import the necessary libraries:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>import machine\nfrom machine import Pin\nfrom time import sleep<\/code><\/pre>\n\n\n\n<p>Create a <span class=\"rnthl rntliteral\">Pin<\/span> object that refers to <span class=\"rnthl rntclblue\">GPIO 2<\/span> called <span class=\"rnthl rntliteral\">led<\/span>. This refers to the on-board LED.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>led = Pin (2, Pin.OUT)<\/code><\/pre>\n\n\n\n<p>The following commands blink the LED.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>led.value(1)\nsleep(1)\nled.value(0)\nsleep(1)<\/code><\/pre>\n\n\n\n<p>In this case, we&#8217;re blinking an LED for demonstration purposes, but the idea is to add your main code in this section of the script.<\/p>\n\n\n\n<p>Before going to sleep, we add a delay of 5 seconds and print a message to indicate that it\u2019s going to sleep.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>sleep(5)\nprint('Im awake, but Im going to sleep')<\/code><\/pre>\n\n\n\n<p>It\u2019s important to add a 5 seconds delay before going to sleep when we are developing the scripts. When you want to upload a new code to the board, it needs to be awake. So, if you don\u2019t have the delay, it will be difficult to catch it awake to upload new code later on. After having the final code, you can delete that delay.<\/p>\n\n\n\n<p>Finally, put the ESP32 in deep sleep for 10 seconds (10 000 milliseconds).<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>machine.deepsleep(10000)<\/code><\/pre>\n\n\n\n<p>After 10 seconds, the ESP32 wakes up and runs the code from the start, similarly to when you press the EN\/RST button.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Demonstration<\/h3>\n\n\n\n<p>Copy the code provided to the <em>main.py<\/em> file of your ESP32. Upload the new code and press the EN\/RST button after uploading. <\/p>\n\n\n\n<p>The ESP32 should blink the on-board LED and print a message. Then, it goes to sleep. This is repeated over and over again.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"666\" height=\"457\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/deep_sleep_reset_esp32.png?resize=666%2C457&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" class=\"wp-image-85333\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/deep_sleep_reset_esp32.png?w=666&amp;quality=100&amp;strip=all&amp;ssl=1 666w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/deep_sleep_reset_esp32.png?resize=300%2C206&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 666px) 100vw, 666px\" \/><\/figure><\/div>\n\n\n\n<p class=\"rntbox rntclblue\">If you don&#8217;t know how to upload the script <a href=\"https:\/\/randomnerdtutorials.com\/getting-started-thonny-micropython-python-ide-esp32-esp8266\/\">follow this tutorial if you&#8217;re using Thonny IDE<\/a>, or <a href=\"https:\/\/randomnerdtutorials.com\/getting-started-micropython-esp32-esp8266\/\">this one if you&#8217;re using uPyCraft IDE<\/a>.<\/p>\n\n\n\n<hr class=\"wp-block-separator is-style-wide\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">External Wake Up<\/h2>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter is-resized\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/02\/external-wake-up.png?resize=175%2C125&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" class=\"wp-image-81693\" width=\"175\" height=\"125\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/02\/external-wake-up.png?w=350&amp;quality=100&amp;strip=all&amp;ssl=1 350w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/02\/external-wake-up.png?resize=300%2C214&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 175px) 100vw, 175px\" \/><\/figure><\/div>\n\n\n\n<p>The ESP32 can also be awaken from sleep when there is a change on the state of a pin. There are two possibilities of external wake up with the ESP32: <strong>ext0 <\/strong>and <strong>ext1<\/strong>.<\/p>\n\n\n\n<p>The ext0 mode allows you to use one GPIO as a wake up source. The ext1 mode allows you to set more than one GPIO as a wake up source at the same time.<\/p>\n\n\n\n<p><strong>Only RTC GPIOs can be used as a wake up source<\/strong>. The RTC GPIOs are highlighted with an orange rectangle box in the next diagram.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"538\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/08\/ESP32-DOIT-DEVKIT-V1-Board-Pinout-36-GPIOs-updated.jpg?resize=750%2C538&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" class=\"wp-image-72102\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/08\/ESP32-DOIT-DEVKIT-V1-Board-Pinout-36-GPIOs-updated.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/08\/ESP32-DOIT-DEVKIT-V1-Board-Pinout-36-GPIOs-updated.jpg?resize=300%2C215&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n\n<p>Learn more about the ESP32 GPIOs: <a href=\"https:\/\/randomnerdtutorials.com\/esp32-pinout-reference-gpios\/\">ESP32 Pinout Reference: Which GPIO pins should you use?<\/a> <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">External wake up &#8211; ext0<\/h3>\n\n\n\n<p>To illustrate how to use the external wake up ext0, we&#8217;ll use one pushbutton as a wake up source. The ESP32 awakes when you press the pushbutton.<\/p>\n\n\n\n<p>For this example, you need the following components:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a rel=\"noreferrer noopener\" href=\"https:\/\/makeradvisor.com\/tools\/esp32-dev-board-wi-fi-bluetooth\/\" target=\"_blank\">ESP32<\/a> (read <a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/makeradvisor.com\/esp32-development-boards-review-comparison\/\" target=\"_blank\">Best ESP32 development boards<\/a>)<\/li><li><a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/makeradvisor.com\/tools\/pushbuttons-kit\/\" target=\"_blank\">Pushbutton<\/a><\/li><li><a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/makeradvisor.com\/tools\/resistors-kits\/\" target=\"_blank\">10k Ohm resistor<\/a><\/li><li><a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/makeradvisor.com\/tools\/mb-102-solderless-breadboard-830-points\/\" target=\"_blank\">Breadboard<\/a><\/li><li><a href=\"https:\/\/makeradvisor.com\/tools\/jumper-wires-kit-120-pieces\/\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\">Jumper wires<\/a><\/li><\/ul>\n\n\n<p>You can use the preceding links or go directly to <a href=\"https:\/\/makeradvisor.com\/tools\/?utm_source=rnt&utm_medium=post&utm_campaign=post\" target=\"_blank\">MakerAdvisor.com\/tools<\/a> to find all the parts for your projects at the best price!<\/p><p style=\"text-align:center;\"><a href=\"https:\/\/makeradvisor.com\/tools\/?utm_source=rnt&utm_medium=post&utm_campaign=post\" target=\"_blank\"><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/10\/header-200.png?w=1200&#038;quality=100&#038;strip=all&#038;ssl=1\"><\/a><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\n\nSchematic Diagram\n\n<\/h4>\n\n\n\n<p>Wire the circuit by following the next schematic diagram. In this example, we&#8217;re using <span class=\"rnthl rntcblue\">GPIO14<\/span> to wake up the ESP32, but you can use any other RTC GPIO.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1010\" height=\"728\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/one-external-wake-ups_bb.png?resize=1010%2C728&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 pushbutton external wake up micropython ext0\" class=\"wp-image-85334\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/one-external-wake-ups_bb.png?w=1010&amp;quality=100&amp;strip=all&amp;ssl=1 1010w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/one-external-wake-ups_bb.png?resize=300%2C216&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/one-external-wake-ups_bb.png?resize=768%2C554&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 1010px) 100vw, 1010px\" \/><\/figure><\/div>\n\n\n\n<h4 class=\"wp-block-heading\">Script<\/h4>\n\n\n\n<p>The following script shows how ext0 works: it uses one GPIO as an external wake up source.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-python\"># Complete project details at https:\/\/RandomNerdTutorials.com\/micropython-programming-with-esp32-and-esp8266\/\n\nimport esp32\nfrom machine import Pin\nfrom machine import deepsleep\nfrom time import sleep\n\nwake1 = Pin(14, mode = Pin.IN)\n\n#level parameter can be: esp32.WAKEUP_ANY_HIGH or esp32.WAKEUP_ALL_LOW\nesp32.wake_on_ext0(pin = wake1, level = esp32.WAKEUP_ANY_HIGH)\n\n#your main code goes here to perform a task\n\nprint('Im awake. Going to sleep in 10 seconds')\nsleep(10)\nprint('Going to sleep now')\ndeepsleep()\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/ESP-MicroPython\/raw\/master\/code\/GPIOs\/Deep_Sleep_ESP32_External\/deep_sleep_ext0_esp32.py\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">How the code works<\/h4>\n\n\n\n<p>First, you need to import the necessary modules. You need to import the <span class=\"rnthl rntliteral\">esp32<\/span> module that contains the methods to set a pin as a wake up source.<\/p>\n\n\n\n<p>After importing the necessary modules, define a wake up pin. In this case we&#8217;re using <span class=\"rnthl rntcblue\">GPIO14<\/span> and we call it <span class=\"rnthl rntliteral\">wake1<\/span>. This GPIO should be set as an input (<span class=\"rnthl rntliteral\">Pin.IN<\/span>).<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>wake1 = Pin(14, mode = Pin.IN)<\/code><\/pre>\n\n\n\n<p>Then, set ext0 as a wake up source using the <span class=\"rnthl rntliteral\">wake_on_ext0()<\/span> method as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>esp32.wake_on_ext0(pin = wake1, level = esp32.WAKEUP_ANY_HIGH)<\/code><\/pre>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">wake_on_ext0()<\/span> method accepts as arguments the pin and the level:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>pin<\/strong>: an object of type <span class=\"rnthl rntliteral\">Pin<\/span> (the GPIO that acts as a wake up source)<\/li><li><strong>level<\/strong>: defines the state of the GPIO that wakes up the ESP32. The level can be one of the following parameters:<ul><li><span class=\"rnthl rntliteral\">WAKEUP_ANY_HIGH<\/span><\/li><li><span class=\"rnthl rntliteral\">WAKEUP_ALL_LOW<\/span><\/li><\/ul><\/li><\/ul>\n\n\n\n<p>In this case, we&#8217;re using the <span class=\"rnthl rntliteral\">WAKEUP_ANY_HIGH<\/span> method that wakes up the ESP32 when the GPIO goes HIGH.<\/p>\n\n\n\n<p>Your main code to execute a task should go after defining the wake up source and right before going to sleep.<\/p>\n\n\n\n<p>We add a 10 second delay before going to sleep. To put the ESP32 into deep sleep, you just need to use the <span class=\"rnthl rntliteral\">deepsleep()<\/span> method as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>machine.deepsleep()<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Demonstration<\/h3>\n\n\n\n<p>Upload the code to the ESP32 <em>main.py<\/em> file. Press the EN\/RESET button to run the code. The ESP32 should go to sleep.<\/p>\n\n\n\n<p>Now, you can press the button to wake it up from deep sleep.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"768\" height=\"417\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/external-wake-up.png?resize=768%2C417&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" class=\"wp-image-85339\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/external-wake-up.png?w=768&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/external-wake-up.png?resize=300%2C163&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 768px) 100vw, 768px\" \/><\/figure><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">External wake up &#8211; ext1<\/h3>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"421\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/ESP32-Deep-Sleep-External-Wake-Up-MicroPython-ext0-ext1.jpg?resize=750%2C421&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 micropython external wake up ext1\" class=\"wp-image-85345\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/ESP32-Deep-Sleep-External-Wake-Up-MicroPython-ext0-ext1.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/ESP32-Deep-Sleep-External-Wake-Up-MicroPython-ext0-ext1.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\n<p>External wake up ext1 works very similar to ext0 but it allows you to set more than one GPIO as a wake up source. To demonstrate how it works we&#8217;ll use two pushbuttons wired to different GPIOs.<\/p>\n\n\n\n<p>For this example, you need the following components:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/makeradvisor.com\/tools\/esp32-dev-board-wi-fi-bluetooth\/\" target=\"_blank\">ESP32<\/a> (read <a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/makeradvisor.com\/esp32-development-boards-review-comparison\/\" target=\"_blank\">Best ESP32 development boards<\/a>)<\/li><li>2x <a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/makeradvisor.com\/tools\/pushbuttons-kit\/\" target=\"_blank\">Pushbutton<\/a><\/li><li>2x <a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/makeradvisor.com\/tools\/resistors-kits\/\" target=\"_blank\">10k Ohm resistor<\/a><\/li><li><a href=\"https:\/\/makeradvisor.com\/tools\/mb-102-solderless-breadboard-830-points\/\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\">Breadboard<\/a><\/li><li><a rel=\"noreferrer noopener\" href=\"https:\/\/makeradvisor.com\/tools\/jumper-wires-kit-120-pieces\/\" target=\"_blank\">Jumper wires<\/a><\/li><\/ul>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"mce_89\">Schematic Diagram <\/h4>\n\n\n\n<p>Wire the circuit by following the next schematic diagram.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1010\" height=\"740\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/two-external-wake-ups_bb.png?resize=1010%2C740&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 ext1 external wake up source micropython deep sleep\" class=\"wp-image-85340\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/two-external-wake-ups_bb.png?w=1010&amp;quality=100&amp;strip=all&amp;ssl=1 1010w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/two-external-wake-ups_bb.png?resize=300%2C220&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/two-external-wake-ups_bb.png?resize=768%2C563&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 1010px) 100vw, 1010px\" \/><\/figure><\/div>\n\n\n\n<p>In this case, we&#8217;re using <span class=\"rnthl rntcblue\">GPIO14<\/span> and <span class=\"rnthl rntcgreen\">GPIO12<\/span>. You can use any other suitable GPIOs, but these need to be RTC GPIOs, otherwise this method doesn&#8217;t work. <\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"mce_45\">Script<\/h4>\n\n\n\n<p>The following script shows how ext1 works: it uses two GPIOs as an external wake up source, but you can use more if you want.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-python\"># Complete project details at https:\/\/RandomNerdTutorials.com\/micropython-programming-with-esp32-and-esp8266\/\n\nimport esp32\nfrom machine import deepsleep\nfrom machine import Pin\nfrom time import sleep\n\nwake1 = Pin(14, mode = Pin.IN)\nwake2 = Pin(12, mode = Pin.IN)\n\n#level parameter can be: esp32.WAKEUP_ANY_HIGH or esp32.WAKEUP_ALL_LOW\nesp32.wake_on_ext1(pins = (wake1, wake2), level = esp32.WAKEUP_ANY_HIGH)\n\n#your main code goes here to perform a task\n\nprint('Im awake. Going to sleep in 10 seconds')\nsleep(10)\nprint('Going to sleep now')\ndeepsleep()\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/ESP-MicroPython\/raw\/master\/code\/GPIOs\/Deep_Sleep_ESP32_External\/deep_sleep_ext1_esp32.py\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"mce_47\">How the code works<\/h4>\n\n\n\n<p>The code is similar to the ext0 example, but it uses the <span class=\"rnthl rntliteral\">wake_on_ext1()<\/span> method instead.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>esp32.wake_on_ext1(pins = (wake1, wake2), level = esp32.WAKEUP_ANY_HIGH)<\/code><\/pre>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">wake_on_ext1()<\/span> method accepts as arguments the pins and the level: <\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>pins<\/strong>: a tupple or list with objects of type <span class=\"rnthl rntliteral\">Pin<\/span> (the GPIOs that act as a wake up source)<\/li><li><strong>level<\/strong>: defines the state of the GPIOs that will wake up the ESP32. The level can be one of the following parameters:<ul><li><span class=\"rnthl rntliteral\">WAKEUP_ANY_HIGH<\/span><\/li><li><span class=\"rnthl rntliteral\">WAKEUP_ALL_LOW<\/span><\/li><\/ul><\/li><\/ul>\n\n\n\n<p>After defining the wake up source, you can put the ESP32 in deep sleep:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>machine.deepsleep()<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Wrapping Up<\/h2>\n\n\n\n<p>We hope you&#8217;ve found this tutorial about deep sleep with the ESP32 using MicroPython useful. We&#8217;ve covered timer wake up and external wake up. There is also the possibility to wake up the ESP32 using the touch pins. Although this is already available in the latest MicroPython firmware, it is not working as expected, so we didn&#8217;t include it in the tutorial. In the meanwhile, you can use <a href=\"https:\/\/randomnerdtutorials.com\/esp32-touch-wake-up-deep-sleep\/\">ESP32 Touch Wake Up with Arduino IDE<\/a>.<\/p>\n\n\n\n<p>If you want to learn more about programming the ESP32 and ESP8266 boards with MicroPython, take a look our eBook: <a href=\"https:\/\/randomnerdtutorials.com\/micropython-programming-with-esp32-and-esp8266\/\">MicroPython Programming with ESP32 and ESP8266<\/a>.<\/p>\n\n\n\n<p>We have other tutorials related with deep sleep that you might be interested:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/randomnerdtutorials.com\/low-power-weather-station-datalogger-using-esp8266-bme280-micropython\/\">Low Power Weather Station Datalogger (MicroPython)<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-deep-sleep-arduino-ide-wake-up-sources\/\">ESP32 Deep Sleep and Wake Up Sources (Arduino IDE)<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/esp8266-deep-sleep-with-arduino-ide\/\">ESP8266 Deep Sleep (Arduino IDE)<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/micropython-esp8266-deep-sleep-wake-up-sources\/\">ESP8266 Deep Sleep and Wake Up Sources (MicroPython)<\/a><\/li><\/ul>\n\n\n\n<p>Thanks for reading.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial shows how to put the ESP32 in deep sleep mode and wake it up using different wake up sources using MicroPython firmware. We&#8217;ll cover timer wake up and &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"MicroPython: ESP32 Deep Sleep and Wake Up Sources\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/micropython-esp32-deep-sleep-wake-up-sources\/#more-85320\" aria-label=\"Read more about MicroPython: ESP32 Deep Sleep and Wake Up Sources\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":5,"featured_media":85357,"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":[281,265,310,309,264],"tags":[],"class_list":["post-85320","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-esp32-project","category-esp8266-project","category-micropython","category-0-esp32-micropython","category-project"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/esp32-deep-sleep-micropython.jpg?fit=1280%2C720&quality=100&strip=all&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/85320","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=85320"}],"version-history":[{"count":0,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/85320\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/85357"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=85320"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=85320"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=85320"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}