Random Nerd Tutorials
Shares

ESP8266 Deep Sleep with Arduino IDE

Shares

In this guide, you’re going to learn what’s Deep Sleep and how to use it with the ESP8266 in the Arduino IDE.

Watch the video version

This guide is available in video format (watch below) and in written format (continue reading this page).


ESP battery powered

If you’ve made a project with an ESP8266 board that is powered with a battery, or if you just connected your ESP NodeMCU board to a power bank, after running it for a while, you realize the battery doesn’t last long.

With most of the ESP8266 modules, you can’t change the hardware to save power, but you can write software to do it. If you use the sleep functions with the ESP8266, it will draw less power and your batteries will last longer. In this guide, we’re going to talk about Deep Sleep with the ESP8266.

Types of Sleep

There are three types of sleep modes: modem sleep, light sleep, and deep sleep.

They all have different purposes and they should be used in different applications.

Deep Sleep

In this project example we want that everything for the ESP8266 is always off, except the Real Time Clock (RTC), which is how the ESP keeps time.

So, we’ll use the Deep Sleep mode which is the most power efficient option and the ESP chip only draws approximately 20uA, of course an assembled ESP8266 board will draw a lot more current.

Other sleep modes

Other sleep modes like the modem sleep and light sleep are helpful if you still need to have your ESP8266 functioning and you want some additional power options. However, if you need some serious power saving, Deep Sleep is the only way to go.

With deep sleep, an example application looks like this:

  1. ESP8266 connects to Wi-Fi
  2. The ESP8266 performs an action (reads a sensor, publishes an MQTT message, etc)
  3. Sleeps for a defined number of microseconds
  4. Repeats that process over and over

For these examples I’m going to use the Arduino IDE and you can go to this link to learn how to prepare the Arduino IDE for the ESP12-E and ESP-01 and how to upload code to an ESP board.

Deep Sleep mode

Let’s start with a simple example. You need to use a wire to connect the RST pin to GPIO 16 which is labeled as D0, in a NodeMCU board. Simply follow the next schematic:

If you take a look at the NodeMCU pinout, you can see that GPIO 16 is a special pin and it has a WAKE feature

 

The RST pin of the ESP8266 is always HIGH while the ESP8266 is running. However, when the RST pin receives a LOW signal, it restarts the microcontroller.

If you set a Deep Sleep timer with the ESP8266, once the timer ends, GPIO 16 sends a LOW signal. That means that GPIO 16 when connected to RST pin can wake up the ESP8266 every time the timer ends.

Writing the ESP12-E code

Having the ESP8266 add-on for the Arduino IDE installed (how to Install the ESP8266 Board in Arduino IDE), go to Tools and select “NodeMCU (ESP-12E Module)”. Here’s the code that you need to upload to your ESP:

In this example, we print a message in the serial monitor, the ESP goes to sleep for 30 seconds, and repeat. In the real world, we’ll want to perform a useful task, like make  any request, while the device is awake.

Note: the deepSleep() time is specified in microseconds (µs).

About the ESP-01

If you want to make a similar setup with an ESP-01 board, you would need to solder a wire as shown below, because that tiny pin 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…

Another setup

Having the ESP8266 add-on for the Arduino IDE installed (how to Install the ESP8266 Board in Arduino IDE), go to Tools and select “Generic ESP8266 Board”.  Then, you can also upload a code that puts your ESP in constant Deep Sleep mode.

As you can see we put a 0 inside the deepSleep function, so your ESP8266 only awakes when something resets the board, it can be the press of a pushbutton or a magnetic reed switch being closed for example.

Here’s how the ESP8266 looks with a pushbutton connected:

When the reset button is pushed, wakes the ESP8266 up, does the programmed task and goes back to sleep until a new reset event is triggered.

Measuring current

Let’s measure the current with a multimeter when it’s on Deep Sleep, here’s how you should place your multimeter probes.

When the ESP-01 is in Deep Sleep mode it’s only using 0.3mA which is approximately 300uA.

Now, when I press the pushbutton the ESP wakes up does some action, in this case it prints a message in the Arduino IDE serial monitor. After a few milliseconds it goes to Deep Sleep.

Keep in mind that during normal usage with Wi-Fi the ESP8266 can consume between 50mA and 170mA.

Wrapping up

Now that you know how to use the Deep Sleep function your battery powered project can last longer.

This is an excerpt from my Home Automation using ESP8266 eBook. If you like ESP8266 and you want to learn more about it. I recommend downloading my course: Home Automation using ESP8266.

I hope this guide was useful. Thanks for reading!

Recommended Resources

Home Automation using ESP8266 »
Build IoT projects and home automation gadgets with the ESP8266 Wi-Fi module.

Build a Home Automation System »
Learn how to build a automation system using open-source hardware and software from scratch.

Arduino Step-by-Step Projects »
Build 25 cool Arduino projects with our couse even with no prior experience!

Leave a Comment:

Add Your Reply

⏰ Early Black Friday 2017 Special ⏰
Download all our courses up to 35% off