{"id":85348,"date":"2019-05-16T15:08:42","date_gmt":"2019-05-16T15:08:42","guid":{"rendered":"https:\/\/randomnerdtutorials.com\/?p=85348"},"modified":"2019-05-16T15:24:00","modified_gmt":"2019-05-16T15:24:00","slug":"micropython-esp8266-deep-sleep-wake-up-sources","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/micropython-esp8266-deep-sleep-wake-up-sources\/","title":{"rendered":"MicroPython: ESP8266 Deep Sleep and Wake Up Sources"},"content":{"rendered":"\n<p>This guide shows how to use deep sleep with the ESP8266 and how to wake it up with a timer or external wake up using MicroPython firmware.<\/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\/esp8266-deep-sleep-micropython.jpg?resize=1200%2C675&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"MicroPython ESP8266 Deep Sleep and Wake Up Sources\" class=\"wp-image-85355\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/esp8266-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\/esp8266-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\/esp8266-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\/esp8266-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>If you have an ESP32, we recommend reading our <a href=\"https:\/\/randomnerdtutorials.com\/micropython-esp32-deep-sleep-wake-up-sources\/\">MicroPython ESP32 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 flashed in your ESP8266. 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><\/ul><ul><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><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<h2 class=\"wp-block-heading\">Introducing Deep Sleep<\/h2>\n\n\n\n<p>When the ESP8266 is in deep sleep mode, everything is off except the Real Time Clock (RTC), which is how the ESP8266 keeps track of time.<\/p>\n\n\n\n<p>In deep sleep mode, the ESP8266 chip consumes approximately  <br>only 20uA. <\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"1024\" height=\"462\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/10\/table-deepsleep-datasheet.jpg?resize=1024%2C462&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Deep Sleep ESP8266 chip power consumption\" class=\"wp-image-44622\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/10\/table-deepsleep-datasheet.jpg?resize=1024%2C462&amp;quality=100&amp;strip=all&amp;ssl=1 1024w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/10\/table-deepsleep-datasheet.jpg?resize=300%2C135&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/10\/table-deepsleep-datasheet.jpg?resize=768%2C346&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/10\/table-deepsleep-datasheet.jpg?w=1466&amp;quality=100&amp;strip=all&amp;ssl=1 1466w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n\n<p>However, you should keep in mind that in an assembled ESP8266 board, it consumes a lot more current.<\/p>\n\n\n\n<p>We were able to build a weather station data logger with the ESP8266 using MicroPython that only consumes 7uA when it is in deep sleep mode: <a href=\"https:\/\/randomnerdtutorials.com\/low-power-weather-station-datalogger-using-esp8266-bme280-micropython\/\">Low Power Weather Station Datalogger using ESP8266 and BME280 with MicroPython<\/a><\/p>\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=\"timer wake up ESP8266 micropython\" 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>There are slightly different ways to wake up the ESP8266 with a timer after deep sleep. One of the easiest ways is using the following function in your code.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>def deep_sleep(msecs):\n  #configure RTC.ALARM0 to be able to wake the device\n  rtc = machine.RTC()\n  rtc.irq(trigger=rtc.ALARM0, wake=machine.DEEPSLEEP)\n  # set RTC.ALARM0 to fire after Xmilliseconds, waking the device\n  rtc.alarm(rtc.ALARM0, msecs)\n  #put the device to sleep\n  machine.deepsleep()<\/code><\/pre>\n\n\n\n<p>We recommend copying the previous function to the beginning of your script, and then call the <span class=\"rnthl rntliteral\">deep_sleep()<\/span> function to put the ESP8266 in deep sleep mode.<\/p>\n\n\n\n<p>This <span class=\"rnthl rntliteral\">deep_sleep()<\/span> function creates a timer that wakes up the ESP8266 after a predetermined number of seconds. To use this function later in your code, you just need to pass as an argument the sleep time in milliseconds.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Script<\/h3>\n\n\n\n<p>In the following code, the ESP8266 is in deep sleep mode for 10 seconds. When it wakes up, it blinks an LED, and goes back to sleep again. This process is repeated over and over again.<\/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 machine\nfrom machine import Pin\nfrom time import sleep\n\nled = Pin(2, Pin.OUT)\n\ndef deep_sleep(msecs):\n  # configure RTC.ALARM0 to be able to wake the device\n  rtc = machine.RTC()\n  rtc.irq(trigger=rtc.ALARM0, wake=machine.DEEPSLEEP)\n\n  # set RTC.ALARM0 to fire after X milliseconds (waking the device)\n  rtc.alarm(rtc.ALARM0, msecs)\n\n  # put the device to sleep\n  machine.deepsleep()\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)\ndeep_sleep(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_esp8266.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 rntcblue\">GPIO 2<\/span> called <span class=\"rnthl rntliteral\">led<\/span>. For our board, it 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>After that, add the <span class=\"rnthl rntliteral\">deep_sleep()<\/span> function to your code:<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>def deep_sleep(msecs):\n  #configure RTC.ALARM0 to be able to wake the device\n  rtc = machine.RTC()\n  rtc.irq(trigger=rtc.ALARM0, wake=machine.DEEPSLEEP)\n  # set RTC.ALARM0 to fire after Xmilliseconds, waking the device\n  rtc.alarm(rtc.ALARM0, msecs)\n  #put the device to sleep\n  machine.deepsleep()<\/code><\/pre>\n\n\n\n<p>The following lines 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>Before going to sleep, we add a delay of 5 seconds and print a message to indicate it is 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 that delay of 5 seconds before going to sleep when we are developing the script. When you want to upload a new code to your board, it needs to be awaken. So, if you don\u2019t have the delay, it will be difficult to catch it awake to upload code later on. After having the final code, you can remove that delay.<\/p>\n\n\n\n<p>Finally, put the ESP8266 in deep sleep for 10 seconds (10 000 milliseconds) by calling the <span class=\"rnthl rntliteral\">deep_sleep()<\/span> function and passing as argument the number of milliseconds.<\/p>\n\n\n\n<pre class=\"wp-block-code language-python\"><code>deep_sleep(10000)<\/code><\/pre>\n\n\n\n<p>After 10 seconds, the ESP8266 wakes up and runs the code from the start, similarly of when you press the RESET button.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Uploading the Code<\/h3>\n\n\n\n<p>Copy the code provided to the <em>main.py<\/em> file and upload it to your ESP8266. <\/p>\n\n\n\n<p class=\"rntbox rntclblue\">If you don\u2019t know how to upload the script <a href=\"https:\/\/randomnerdtutorials.com\/getting-started-thonny-micropython-python-ide-esp32-esp8266\/\">follow this tutorial if you\u2019re using Thonny IDE<\/a>, or&nbsp;<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<p>After uploading the code, you need to connect <span class=\"rnthl rntcblue\">GPIO16<\/span> (D0) to the RST pin so that the ESP8266 can wake itself up.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"579\" height=\"389\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/enable_deep_sleep_esp8266.png?resize=579%2C389&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"MicroPython ESP8266 NodeMCU Timer Wake Up deep sleep Circuit schematic\" class=\"wp-image-85349\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/enable_deep_sleep_esp8266.png?w=579&amp;quality=100&amp;strip=all&amp;ssl=1 579w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/enable_deep_sleep_esp8266.png?resize=300%2C202&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 579px) 100vw, 579px\" \/><\/figure><\/div>\n\n\n\n<p class=\"rntbox rntcred\"><strong>Important: <\/strong> if you don&#8217;t connect GPIO16 to the RST pin, the ESP8266 will not wake up.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Demonstration<\/h3>\n\n\n\n<p>After uploading the code and connecting <span class=\"rnthl rntcblue\">GPIO 16<\/span> (D0) to the RST pin, the ESP8266 should blink the on-board LED and print a message in the shell.<\/p>\n\n\n\n<p>Then, it goes to sleep for 10 seconds, wakes up and runs the code again. This proccess 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=\"650\" height=\"209\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/esp8266-deep-sleep-timer.png?resize=650%2C209&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"MicroPython ESP8266 Timer Wake Up deep sleep demonstration\" class=\"wp-image-85371\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/esp8266-deep-sleep-timer.png?w=650&amp;quality=100&amp;strip=all&amp;ssl=1 650w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/esp8266-deep-sleep-timer.png?resize=300%2C96&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 650px) 100vw, 650px\" \/><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Deep Sleep with ESP-01<\/h2>\n\n\n\n<p>If you want to make a similar setup with an ESP-01 board, you need to solder a wire as shown in the following figure. That tiny pin in the chip is GPIO16 and it needs to be connected to the RST pin. <\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"199\" height=\"296\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/10\/esp-01.png?resize=199%2C296&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"MicroPython ESP-01 Timer Wake Up deep sleep Circuit schematic\" class=\"wp-image-44609\"\/><\/figure><\/div>\n\n\n\n<p>However, the pins are so tiny that it is really hard to solder a wire like that to the GPIO 16 without damaging the chip.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"773\" height=\"715\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/10\/Slide21.png?resize=773%2C715&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"MicroPython ESP-01 Timer Wake Up deep sleep Circuit schematic\" class=\"wp-image-44620\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/10\/Slide21.png?w=773&amp;quality=100&amp;strip=all&amp;ssl=1 773w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/10\/Slide21.png?resize=300%2C277&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/10\/Slide21.png?resize=768%2C710&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 773px) 100vw, 773px\" \/><\/figure><\/div>\n\n\n\n<p>Learn more about the ESP8266 GPIOs: <a href=\"https:\/\/randomnerdtutorials.com\/esp8266-pinout-reference-gpios\/\">ESP8266 Pinout Reference: Which GPIO pins should you use?<\/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=\"external wake up ESP8266 micropython\" 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 ESP8266 doens&#8217;t support external wake up like the ESP32 does. But, there is something we can do about that.<\/p>\n\n\n\n<p>If we put the ESP8266 in deep sleep for an indefinite time, it will only wake up when something resets the board. So, we can wire something to the <span class=\"rnthl rntcpurple\">RST<\/span> pin and use it as an external wake up. It can be the press of a pushbutton or a magnetic reed switch being close, for example.<\/p>\n\n\n\n<p>The ESP8266 resets when the <span class=\"rnthl rntcpurple\">RST<\/span> pin goes LOW.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Schematic Diagram<\/h3>\n\n\n\n<p>To test this method, wire a pushbutton to the <span class=\"rnthl rntcpurple\">RST<\/span> pin. You need the following components for the circuit:<\/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\/esp8266-esp-12e-nodemcu-wi-fi-development-board\/\" target=\"_blank\">ESP8266<\/a> (read <a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/makeradvisor.com\/best-esp8266-wi-fi-development-board\/\" target=\"_blank\">Best ESP8266 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\">10kOhm 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<p>If you&#8217;re using an <a href=\"https:\/\/makeradvisor.com\/tools\/esp8266-esp-12e-nodemcu-wi-fi-development-board\/\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\">ESP8266 12-E NodeMCU kit<\/a>, follow 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=\"895\" height=\"567\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/esp8266-external-wake-up-micropython_bb.png?resize=895%2C567&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"MicroPython ESP8266 NodeMCU External Wake Up deep sleep Circuit schematic\" class=\"wp-image-85374\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/esp8266-external-wake-up-micropython_bb.png?w=895&amp;quality=100&amp;strip=all&amp;ssl=1 895w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/esp8266-external-wake-up-micropython_bb.png?resize=300%2C190&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/esp8266-external-wake-up-micropython_bb.png?resize=768%2C487&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 895px) 100vw, 895px\" \/><\/figure><\/div>\n\n\n\n<p>If you&#8217;re using an <a href=\"https:\/\/makeradvisor.com\/tools\/esp-01-wi-fi-board\/\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\">ESP-01<\/a>, you can follow 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=\"668\" height=\"770\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/10\/esp-01-pushbutton_bb.png?resize=668%2C770&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"MicroPython ESP-01 NodeMCU External Wake Up deep sleep Circuit schematic\" class=\"wp-image-44610\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/10\/esp-01-pushbutton_bb.png?w=668&amp;quality=100&amp;strip=all&amp;ssl=1 668w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/10\/esp-01-pushbutton_bb.png?resize=260%2C300&amp;quality=100&amp;strip=all&amp;ssl=1 260w\" sizes=\"(max-width: 668px) 100vw, 668px\" \/><\/figure><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Script<\/h3>\n\n\n\n<p>Upload the following code to the ESP8266 as<em> main.py<\/em>.<\/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(0)\nsleep(1)\nled.value(1)\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)\nprint('Im awake, but Im going to sleep')\nsleep(1)\n\n#sleep for indefinite time\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_ESP8266_External\/deep_sleep_external_esp8266.py\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<p>In this case, you just need to call <span class=\"rnthl rntliteral\">machine.deepsleep()<\/span> after the execution of the main code.<\/p>\n\n\n\n<p>When you call <span class=\"rnthl rntliteral\">machine.deepsleep()<\/span> without any arguments, the ESP8266 will go into deep sleep mode indefinitely until you press the <span class=\"rnthl rntcpurple\">RST<\/span> button. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Demonstration<\/h3>\n\n\n\n<p>After wiring the circuit and uploading the code, you can test your setup. Every time you press the pushbutton that is connected to the <span class=\"rnthl rntcpurple\">RST<\/span> pin, the ESP8266 resets and wakes up. It blinks the on-board LED and goes back to sleep.<\/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=\"421\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/ESP8266-Deep-Sleep-External-Wake-Up-Pushbutton-MicroPython.jpg?resize=750%2C421&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"MicroPython ESP8266 NodeMCU External Wake Up deep sleep demonstration\" class=\"wp-image-85378\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/ESP8266-Deep-Sleep-External-Wake-Up-Pushbutton-MicroPython.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/ESP8266-Deep-Sleep-External-Wake-Up-Pushbutton-MicroPython.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<h2 class=\"wp-block-heading\">Wrapping Up<\/h2>\n\n\n\n<p>We hope you&#8217;ve found this project about deep sleep with the ESP8266 useful. We have other tutorials about deep sleep that you might like:<\/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\/esp8266-deep-sleep-with-arduino-ide\/\">ESP8266 Deep Sleep (Arduino IDE)<\/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\/micropython-esp32-deep-sleep-wake-up-sources\/\">ESP32 Deep Sleep and Wake Up Sources (MicroPython)<\/a><\/li><\/ul>\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>Thanks for reading.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This guide shows how to use deep sleep with the ESP8266 and how to wake it up with a timer or external wake up using MicroPython firmware. If you have &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"MicroPython: ESP8266 Deep Sleep and Wake Up Sources\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/micropython-esp8266-deep-sleep-wake-up-sources\/#more-85348\" aria-label=\"Read more about MicroPython: ESP8266 Deep Sleep and Wake Up Sources\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":5,"featured_media":85355,"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-85348","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\/esp8266-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\/85348","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=85348"}],"version-history":[{"count":0,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/85348\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/85355"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=85348"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=85348"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=85348"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}