Using the Arduino IDE, this tutorial shows how to use the ESP8266 NodeMCU deep sleep. We'll go over deep sleep with timer wake-up as well as deep sleep with external wake-up using the reset (RST) pin.
To put the ESP8266 into deep sleep mode, use
ESP.deepSleep(uS)
and pass the sleep time in microseconds as an argument. To wake up the ESP8266, connect GPIO 16 to the reset (RST) pin.
Use
ESP.deepSleep(0)
to put the ESP8266 into deep sleep mode for an infinite period of time. When the RST pin receives a LOW signal, the ESP8266 will wake up.
Introducing Deep Sleep Mode
If you created a project using a battery-powered ESP8266 board or if you just linked your ESP8266 NodeMCU board to a power bank. After running it for a bit, you realize the battery life is short, especially if you're using Wi-Fi.
If you put your ESP8266 into deep sleep mode, it reduces power consumption and makes your batteries last longer.
The ESP8266 in deep power sleep mode means cutting down on activities that cost more power while operating (like Wi-Fi) but leaving just enough activity to wake up the processor when anything interesting occurs.
Types of Sleep
Light sleep, deep sleep, and moderate sleep are the three types of sleep modes. The table below shows the differences between each mode (taken from the ESP8266 datasheet).
Item | Modem-sleep | Light-sleep | Deep-sleep |
Wi-Fi | OFF | OFF | OFF |
System clock | ON | OFF | OFF |
RTC | ON | ON | ON |
CPU | ON | Pending | OFF |
Substrate current | 15 mA | 0.4 mA | ~20 uA |
Average current (DTIM = 1) | 16.2 mA | 1.8 mA | – |
Average current (DTIM = 3) | 15.4 mA | 0.9 mA | – |
Average current (DTIM = 10) | 15.2 mA | 0.55 mA | – |
Note: The table's power consumption refers to the ESP8266 as a standalone chip. If you're using a development board, you'll see that it contains more passive components that use more current.
They should all be employed in different applications since they all serve diverse functions.
We'll go over deep sleep mode in this essay. Except for the Real Time Clock (RTC), which is how the ESP8266 keeps track of time, everything is always turned off.
This is the most power-efficient alternative, with the ESP chip drawing only around 20 uA. However, you won't be able to attain such a low power state if you use a full-featured development board with a built-in programmer, LEDs, and so on.
Deep Sleep Sketch
With deep sleep, an example application looks like this:
- The ESP8266 connects to Wi-Fi.
- The ESP8266 performs a task (reads a sensor, publishes an MQTT message, etc).
- Sleeps for a predefined period of time.
- The ESP8266 wakes up.
- The process is repeated over and over again.
Wake up Sources
After putting the ESP8266 in deep sleep mode, there are different ways to wake it up:
- #1 timer wake up: the ESP8266 wakes itself up after a predefined period of time.
- #2 External wake up: the ESP8266 wakes up when you press the RST button (the ESP8266 restarts).
1) ESP8266 NodeMCU Deep Sleep with Timer Wake Up
You need to connect the RST pin to GPIO 16, which is labeled D0, on a NodeMCU board to use timer wake-up with the ESP8266. Simply follow the next schematic diagram:
After uploading the code, connect the RST pin to GPIO 16 only.
Take a look at the NodeMCU pinout, and you'll see that GPIO 16 is a special pin with a wake feature.
Recommended reading: ESP8266 Pinout Reference Guide
While the ESP8266 is running, the RST pin is always HIGH. When the RST pin receives a LOW signal, the microcontroller is restarted.
After you use the ESP8266 to set a deep sleep timer, GPIO 16 delivers a LOW signal after the timer expires. That means that when GPIO 16 is connected to the RST pin, it may wake up the ESP8266 after a set wake period.
ESP8266 NodeMCU Timer Wake Up Sketch
If you have the ESP8266 add-on for Arduino IDE installed (see How to Install the ESP8266 Board in Arduino IDE), go to “Tools” and select “NodeMCU (ESP-12E Module)“. Here is the code you need to upload to your ESP:
/*
* ESP8266 Deep sleep mode example
* LEDEdit PRO
* Complete Project Details https://lededitpro.com
*/
void setup() {
Serial.begin(115200);
Serial.setTimeout(2000);
// Wait for serial to initialize.
while(!Serial) { }
// Deep sleep mode for 30 seconds, the ESP8266 wakes up by itself when GPIO 16 (D0 in NodeMCU board) is connected to the RESET pin
Serial.println("I'm awake, but I'm going into deep sleep mode for 30 seconds");
ESP.deepSleep(30e6);
// Deep sleep mode until RESET pin is connected to a LOW signal (for example pushbutton or magnetic reed switch)
//Serial.println("I'm awake, but I'm going into deep sleep mode until RESET pin is connected to a LOW signal");
//ESP.deepSleep(0);
}
void loop() {
}
In this example, we print a message in the Serial Monitor:
Serial.println("I'm awake, but I'm going into deep sleep mode until RESET pin is connected to a LOW signal");
After that, the ESP8266 goes to sleep for 30 seconds.
ESP.deepSleep(30e6);
To put the ESP8266 into a deep sleep, you use ESP.deepsleep(uS)
and pass as an argument the sleep time in microseconds.
In this case, 30e6 is equivalent to 30000000 microseconds, or 30 seconds.
After uploading the code, press the RST button to start the code running, and then connect RST to GPIO 16. Every 30 seconds, the ESP8266 should wake up and print a message on the Serial Monitor, as shown below.
In this example, you simply print a message to the Serial Monitor, but in a real-world application, you would do a useful task like sending a request, publishing sensor values, and so on.
ESP-01 Timer Wake Up Circuit
If you wish to create a similar setup using an ESP-01 board, you need to solder a wire as shown. GPIO 16 is the tiny pin that needs to be linked to the RST pin.
However, the pins are so tiny that it is really hard to solder a wire like that to GPIO 16 on the ESP-01. So, for this wake-up mode, you should use the NodeMCU board or a bare ESP12-E chip.
2) ESP8266 NodeMCU Deep Sleep with External Wake Up
You may also wake up the ESP8266 using an external wake-up device, like a button press or a reed switch. To wake up the ESP8266, all you need to do is put it in deep sleep mode for an endless period and then set the RST pin to LOW.
Wire a pushbutton to your ESP8266 board as shown in the schematic diagram below to test this setup at low power. The RST pin goes LOW when you press the pushbutton.
If you’re using an ESP-01, follow the next diagram instead.
ESP8266 External Wake Up Sketch
Then, upload the following code to your ESP8266 board.
/*
* ESP8266 Deep sleep mode example
* LEDEdit PRO
* Complete Project Details https://lededitpro.com
*/
void setup() {
Serial.begin(115200);
Serial.setTimeout(2000);
// Wait for serial to initialize.
while(!Serial) { }
// Deep sleep mode for 30 seconds, the ESP8266 wakes up by itself when GPIO 16 (D0 in NodeMCU board) is connected to the RESET pin
//Serial.println("I'm awake, but I'm going into deep sleep mode for 30 seconds");
//ESP.deepSleep(30e6);
// Deep sleep mode until RESET pin is connected to a LOW signal (for example pushbutton or magnetic reed switch)
Serial.println("I'm awake, but I'm going into deep sleep mode until RESET pin is connected to a LOW signal");
ESP.deepSleep(0);
}
void loop() {
}
This code puts the ESP8266 into deep sleep mode for an indefinite period of time. You just need to pass 0 as an argument to the deepSleep()
method to do this:
ESP.deepSleep(0);
When anything resets the board, the ESP will only wake up. In this case, the press of a pushbutton pulls the RST pin to GND.
When you press the pushbutton, the ESP8266 wakes up, does the programmed task, and then goes back to sleep until a new reset event occurs.
Measuring current
When the board is in deep sleep mode, use a multimeter to measure the current consumption to see how much power it is using.
Here’s how you should place your multimeter probes:
When the ESP-01 is in deep sleep mode, it’s only using 0.3 mA, which is approximately 300 uA.
Keep in mind that during normal usage with Wi-Fi, the ESP8266 can consume between 50 mA and 170 mA.
If you like ESP8266, you may also like:
- Build a Water Level Control System Using ESP8266 NodeMCU
- Getting Started with ESP8266 NodeMCU Board | Complete Guide
- How to Post on Twitter using an ESP8266
- How to Set an ESP8266 NodeMCU Access Point for a Web Server
We hope you find this tutorial useful. Thanks for reading.