MicroPython: ESP8266 Deep Sleep and Wake Up Sources

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.

MicroPython ESP8266 Deep Sleep and Wake Up Sources

If you have an ESP32, we recommend reading our MicroPython ESP32 Deep Sleep and Wake Up Sources Guide.

Prerequisites

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:

Introducing Deep Sleep

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.

In deep sleep mode, the ESP8266 chip consumes approximately
only 20uA.

Deep Sleep ESP8266 chip power consumption

However, you should keep in mind that in an assembled ESP8266 board, it consumes a lot more current.

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: Low Power Weather Station Datalogger using ESP8266 and BME280 with MicroPython

Timer Wake Up

timer wake up ESP8266 micropython

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.

def deep_sleep(msecs):
  #configure RTC.ALARM0 to be able to wake the device
  rtc = machine.RTC()
  rtc.irq(trigger=rtc.ALARM0, wake=machine.DEEPSLEEP)
  # set RTC.ALARM0 to fire after Xmilliseconds, waking the device
  rtc.alarm(rtc.ALARM0, msecs)
  #put the device to sleep
  machine.deepsleep()

We recommend copying the previous function to the beginning of your script, and then call the deep_sleep() function to put the ESP8266 in deep sleep mode.

This deep_sleep() 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.

Script

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.

# Complete project details at https://RandomNerdTutorials.com

import machine
from machine import Pin
from time import sleep

led = Pin(2, Pin.OUT)

def deep_sleep(msecs):
  # configure RTC.ALARM0 to be able to wake the device
  rtc = machine.RTC()
  rtc.irq(trigger=rtc.ALARM0, wake=machine.DEEPSLEEP)

  # set RTC.ALARM0 to fire after X milliseconds (waking the device)
  rtc.alarm(rtc.ALARM0, msecs)

  # put the device to sleep
  machine.deepsleep()
  
#blink LED
led.value(1)
sleep(1)
led.value(0)
sleep(1)

# wait 5 seconds so that you can catch the ESP awake to establish a serial communication later
# you should remove this sleep line in your final script
sleep(5)

print('Im awake, but Im going to sleep')

#sleep for 10 seconds (10000 milliseconds)
deep_sleep(10000)

View raw code

How the Code Works

First, import the necessary libraries:

import machine
from machine import Pin
from time import sleep

Create a Pin object that refers to GPIO 2 called led. For our board, it refers to the on-board LED.

led = Pin(2, Pin.OUT)

After that, add the deep_sleep() function to your code:

def deep_sleep(msecs):
  #configure RTC.ALARM0 to be able to wake the device
  rtc = machine.RTC()
  rtc.irq(trigger=rtc.ALARM0, wake=machine.DEEPSLEEP)
  # set RTC.ALARM0 to fire after Xmilliseconds, waking the device
  rtc.alarm(rtc.ALARM0, msecs)
  #put the device to sleep
  machine.deepsleep()

The following lines blink the LED.

led.value(1)
sleep(1)
led.value(0)
sleep(1)

Before going to sleep, we add a delay of 5 seconds and print a message to indicate it is going to sleep.

sleep(5)
print('Im awake, but Im going to sleep')

It’s 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’t 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.

Finally, put the ESP8266 in deep sleep for 10 seconds (10 000 milliseconds) by calling the deep_sleep() function and passing as argument the number of milliseconds.

deep_sleep(10000)

After 10 seconds, the ESP8266 wakes up and runs the code from the start, similarly of when you press the RESET button.

Uploading the Code

Copy the code provided to the main.py file and upload it to your ESP8266.

If you don’t know how to upload the script follow this tutorial if you’re using Thonny IDE, or this one if you’re using uPyCraft IDE.

After uploading the code, you need to connect GPIO16 (D0) to the RST pin so that the ESP8266 can wake itself up.

MicroPython ESP8266 NodeMCU Timer Wake Up deep sleep Circuit schematic

Important: if you don’t connect GPIO16 to the RST pin, the ESP8266 will not wake up.

Demonstration

After uploading the code and connecting GPIO 16 (D0) to the RST pin, the ESP8266 should blink the on-board LED and print a message in the shell.

Then, it goes to sleep for 10 seconds, wakes up and runs the code again. This proccess is repeated over and over again.

MicroPython ESP8266 Timer Wake Up deep sleep demonstration

Deep Sleep with ESP-01

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.

MicroPython ESP-01 Timer Wake Up deep sleep Circuit schematic

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.

MicroPython ESP-01 Timer Wake Up deep sleep Circuit schematic

Learn more about the ESP8266 GPIOs: ESP8266 Pinout Reference: Which GPIO pins should you use?


External Wake Up

external wake up ESP8266 micropython

The ESP8266 doens’t support external wake up like the ESP32 does. But, there is something we can do about that.

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 RST 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.

The ESP8266 resets when the RST pin goes LOW.

Schematic Diagram

To test this method, wire a pushbutton to the RST pin. You need the following components for the circuit:

You can use the preceding links or go directly to MakerAdvisor.com/tools to find all the parts for your projects at the best price!

If you’re using an ESP8266 12-E NodeMCU kit, follow the next schematic diagram.

MicroPython ESP8266 NodeMCU External Wake Up deep sleep Circuit schematic

If you’re using an ESP-01, you can follow the next schematic diagram.

MicroPython ESP-01 NodeMCU External Wake Up deep sleep Circuit schematic

Script

Upload the following code to the ESP8266 as main.py.

# Complete project details at https://RandomNerdTutorials.com

import machine
from machine import Pin
from time import sleep

led = Pin (2, Pin.OUT)
  
#blink LED
led.value(0)
sleep(1)
led.value(1)
sleep(1)

# wait 5 seconds so that you can catch the ESP awake to establish a serial communication later
# you should remove this sleep line in your final script
sleep(5)
print('Im awake, but Im going to sleep')
sleep(1)

#sleep for indefinite time
machine.deepsleep()

View raw code

In this case, you just need to call machine.deepsleep() after the execution of the main code.

When you call machine.deepsleep() without any arguments, the ESP8266 will go into deep sleep mode indefinitely until you press the RST button.

Demonstration

After wiring the circuit and uploading the code, you can test your setup. Every time you press the pushbutton that is connected to the RST pin, the ESP8266 resets and wakes up. It blinks the on-board LED and goes back to sleep.

MicroPython ESP8266 NodeMCU External Wake Up deep sleep demonstration

Wrapping Up

We hope you’ve found this project about deep sleep with the ESP8266 useful. We have other tutorials about deep sleep that you might like:

If you want to learn more about programming the ESP32 and ESP8266 boards with MicroPython, take a look our eBook: MicroPython Programming with ESP32 and ESP8266.

Thanks for reading.


Learn how to program and build projects with the ESP32 and ESP8266 using MicroPython firmware DOWNLOAD »

Learn how to program and build projects with the ESP32 and ESP8266 using MicroPython firmware DOWNLOAD »


Enjoyed this project? Stay updated by subscribing our weekly newsletter!

9 thoughts on “MicroPython: ESP8266 Deep Sleep and Wake Up Sources”

  1. Hi Rui
    Thanks for great tutorial. I have tried your examples, they works fine so as this one. The NodeMCU goes to deep sleep and consumes less power. However when I connect sensor like MQ 135, the sensor still consumes energy even if NodeMCU in deep sleep. So please provide details on how to save the power consumed by MQ 135 in deep sleep. Is there any way I can stop the power supply to MQ 135 when NodeMCU goes to deep sleep. Because without having sensor attached to NodeMCU and sending it to deep sleep does not make any sense.

    • You Timothy.
      You can connect the VCC pin of your sensor to one of the GPIOs.
      Then, when the ESP wake up, set that pin to HIGH to power the sensor.
      I think this should solve the problem.
      Regards,
      Sara

      • Hi Sara,
        Thanks for your tip. I did exactly as you said and it worked for me. But I have noticed that connecting MQ135 sensor VCC to GPIO pin of NodeMCU gives me value around 30 ppm, connecting VCC to 3.3v gives around 150 ppm, and connecting VCC to Vin (5v) of NodeMCU gives me around 300 ppm. So why is this difference. The datasheet of MQ 135 specifies that Circuit voltage of sensor should be 5v. So I wonder if I am supplying right voltage to sensor. What should I do to supply right voltage (around 5v) to sensor using GPIO since I want to put everything off in deepsleep. Kindly help.

  2. Rui/ Sara, I have been asking but I wanted to know the pin layout for a ESP-32 WROOM-32 board. Got it from Aliexpress, but doesn’t look like the original ESP-32. Can I still use that board as a pin layout? Thank you if you can help.

    • Hi Logan
      Please find the datasheet of WROOM-32 board on following link. Hope it helps.
      espressif.com/sites/default/files/documentation/esp32-wroom-32_datasheet_en.pdf

  3. Hello Rui, Sara,

    I’ve got a Lolin NodeMcu V3 and when it wakes up after deep sleep it freezes and I need to do a full power cycle to get it going again. It always freezes after a deep sleep. It runs on firmware: esp8266-20180511-v1.9.4. I’ve followed instructions to the letter and it just won’t work as expected.

    any suggestions? Thank you for any help!

    • Hi Ruben.
      Please make sure that you have the RST pin connected to GPIO 16 (D1). Otherwise the ESP8266 can’t wake itself up.
      I don’t know if that’s the problem, but what you describe happens when we forget to connect RST to GPIO16.
      I hope this helps.
      Regards,
      Sara

      • Hi Sara,

        Sadly it does not. I’ve tried all pins on the board and it simply freezes. Not a big deal, I’ll use these boards for other projects. My ESP32 deep sleeps perfectly!

        Thank you for your help though.
        Ruben

        • I’m sorry for that issue.
          I don’t know what can be the problem.
          The project works fine with our ESP8266.
          Regards,
          Sara

Leave a Reply to Timothy Malche Cancel reply

Download our Free eBooks and Resources

Get instant access to our FREE eBooks, Resources, and Exclusive Electronics Projects by entering your email address below.