Design a site like this with WordPress.com
Get started

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.

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.

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

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.

# welcome to Gnd_To_Vcc!!

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)

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.

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.

https://tpc.googlesyndication.com/safeframe/1-0-37/html/container.html

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.

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.

https://tpc.googlesyndication.com/safeframe/1-0-37/html/container.html

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


External Wake Up

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:

  • ESP8266 (read Best ESP8266 development boards)
  • Pushbutton
  • 10kOhm resistor
  • Breadboard
  • Jumper wires

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

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

Script

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

# welcome to Gnd_To_vcc!

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()

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.

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.

Advertisement

Published by Gnd_To_Vcc

Here to spread my knowledge . Knowledge should always be spread not stored.

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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: