Deep Sleep Mode with ESP-12E for Power Saving

ESP8266, as we know, is power hungry. If you use an ESP-12E board that is powered by a battery or 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 Mode with the ESP-12E

Types of Sleep

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

Sleep Modes

Sleep Modes

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

Deep Sleep

In this 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

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

2

2

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:

/*
 * ESP8266 Deep sleep mode example
 */
 
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, 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 deep sleep() time is specified in microseconds (µs).

sarah ali

sarah ali

Sarah is a passionate writer and blogger. As an early adopter, she enjoys trying out new social media and Internet tools along with WordPress plugins and Web apps.
sarah ali

Leave a Reply

Your email address will not be published.