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/micropython-programming-with-esp32-and-esp8266/

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/micropython-programming-with-esp32-and-esp8266/

from machine import deepsleep
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
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 build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »
Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »

Enjoyed this project? Stay updated by subscribing our newsletter!

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

    Reply
    • 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

      Reply
      • 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.

        Reply
        • As far as I know, this sensor uses heating of active measuring surface. This heating is probably dependent on current consumed from power supply. Your GPIO pin propbably does not give you sufficient current. Better solution can be to use some MOSFET controlled by GPIO to attach your directly to 5V power supply. Anyway, this type os sensor probably will not be the best chice for battery powered device due to its relatively large consumption.

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

    Reply
    • 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

      Reply
  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!

    Reply
    • 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

      Reply
      • 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

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

          Reply
  4. Hello Team,
    Congrats to your site.
    I did your project using an ESP8266 with your uPython deep sleep program and it works, but after a while (after 1 or 2 days) the ESP wake ups stopping run the program.
    I’m trying to monitor the module without any results/conclusions.
    Do you have any idea of what is going on? Do I move to do program deep sleep in C?
    Thank you in advance,
    AH

    Reply
  5. Hello,
    I’m using an ESP8266 Huzzah from adafruit.com. I’ve been trying to get your deep sleep code working with this board. The board will go into deep sleep mode, but it won’t ever come out and restart. I can run machine.deepsleep(), then press the reset button and the board boots as expected. Any help would be greatly appreciated. I’m going to post this on the adafruit forum also.
    Thanks

    Reply
    • Hi.
      Don’t forget that to use deep sleep with the ESP8266, after uploading the code, you need to connect GPIO16 (D0) to the RST pin so that the ESP8266 can wake itself up.
      Regards,
      Sara

      Reply
  6. Hi Sara,
    I am working on ESP8266 for home automation I am trying to connect ESP 8266 with PIR sensor.Already have written the code for the same including Wifi Manager.

    Planning to intorduce Deep sleep of Node mCu and wake only when theres an input from PIR sensor. Is there any code for the same.

    Thanks in Advance

    Reply
    • Hi.
      To wake up your boards from deep sleep using the PIR motion sensor, take a look at deep sleep with external wake up.
      Regards,
      Sara

      Reply
  7. Hi. I had problems waking the D1 Mini after deepsleep. It sent the usual gibberish on serial and then stopped. Solution:
    station.active(False)
    sleep(5)
    deep_sleep(10000)

    Reply

Leave a Comment

Download Our Free eBooks and Resources

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