ESP8266 Deep Sleep with Arduino IDE

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

Related Content: ESP32 Deep Sleep with Arduino IDE and Wake Up Sources

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:

/*
 * ESP8266 Deep sleep mode example
 * Rui Santos 
 * Complete Project Details https://randomnerdtutorials.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() {
}

View raw code

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.

/*
 * ESP8266 Deep sleep mode example
 * Rui Santos 
 * Complete Project Details https://randomnerdtutorials.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() {
}

View raw code

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!


Learn how to program and build projects with the ESP32 and ESP8266 using MicroPython firmware DOWNLOAD »

Learn how to program and build projects with the ESP32 and ESP8266 using MicroPython firmware DOWNLOAD »


Enjoyed this project? Stay updated by subscribing our weekly newsletter!

31 thoughts on “ESP8266 Deep Sleep with Arduino IDE”

  1. Thank you for wonderful explanation.
    In your explanation, you mentioned ESP-01 its hard to solder the GPIO16 to Reset.
    However in Live example you have used ESP-01 to show connect circuit and, I did not figure out how you managed to solder the GPIO to Reset? (I notice you had a Green PCB below the ESP-01 though, not sure if that did the trick)

    Question is: Can you advise how one can use ESP-01 with deepSleep()? If it really needs the Solder then what is trick to do it?

    • I managed to solder a very thin wire to GPIO 16, and that’s the only way to do it… At least I don’t know any other tricks to do it. (The green PCB only made the ESP-01 breadboard friendly..)

  2. Hi, I am using the code with a NodeMCU board, and I have a consumption of 11m [A] in deep-sleep mode and I do not know why I can´t have 20 u [A].

    • Hello Luciano,
      The ESP8266 chip might consume 20 uA, but the NodeMCU board has many passive components that consume a lot of current, so it has a bad performance. For a real world solution, you should use the ESP8266 chip in a custom PCB board to lower the power consumption.
      Regards,
      Rui

  3. Great tutorial, many thanks.

    Plamen

    P.S. You have reversed the terminals of the amp-meter. Proof: from the power source follow its positive terminal, which is connected to a red wire, which then is connected to the black lead of the amp-meter, which is connected to the common (negative) terminal on the amp-meter. Therefore the current will flow through the amp-meter from its negative to its positive terminal, which is in reverse. A digital amp-meter will show

  4. (cont) a digital amp-meter will show a negative number, an analogue one will show its needle struggling to turn left beyond the zero mark and/or pushing against the small vertical pin just left of tbe zero mark.

    Apologies for the interrupted post (fingers trouble).

    • Hi Plamen.
      You are right. We’ve switch the red probe with the black probe.
      In the case of a digital multimeter, the only difference is that we see a negative number.
      But it can be problematic using an analog multimeter, you are right!
      Thank you for noticing that and sharing it with us.
      Regards,
      Sara 🙂

  5. When I run the sleep routine it crashes after the set time (prints out garbage and then restarts) this is both on an Node MCU and an ESP-01 with the hardware modified. Any comment?

  6. Hi Dear
    In this article, you say:
    “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”.

    The way I programmed the ESP8266 – 01 is by using an FTDI to do the communication. To do this, it requires some cable connections between them. I have done that in my breadboard and code was downloaded successfully on the ESP01. In a second breadboard, I built the circuit with the push button, which seems to work ok (every time I push the button, the light flashes). But I have 2 problems:
    1- How am I gonna check on the Serial Monitor if ESP is not connected to my PC?
    2- Im using a Li-Ion Battery 3.7v to power the circuit. I used my digi muiltimeter to measure the current. It shows 22mA. I think this is way to high.

    • Hi Diego.
      if your ESP is not connected to your computer, you can connect an LED and add a few lines of code to blink the LED when the ESP wakes up. This way you can keep that that woken up.
      It shows 22mA in deep sleep mode?
      Regards,
      Sara 🙂

  7. Hey,could you please guide me here…I want my nodeMCU to go to deep sleep just after connecting to client as a server and send data to it…….when i get it to practice,and go for web page formed ,my esp goes to deepsleep and i cant get any data!!!!
    now,is there a way in which i can use both the properties of nodeMCU,ie. i want it to connect to server then send data then go to deep sleep

    • Hi Sabi.
      Are you using the same ESP to send data and host the web server?
      When the ESP is in deep sleep you can’t access the web server because it is sleeping, so it can’t respond to client requests.
      Regards,
      Sara

  8. hiii
    we want code for esp8266 with temperature sensor(ds18b20)
    we want some feature in our code
    if you interested to write sketch for us
    please inform us with your quotation
    thanks

  9. Thanks very much: I want to do some battery-operated temperature sensors, and needed info on how to do deep sleep. This is just what I needed (including, alas, the fact that the soldering to use my ESP-01s is too fiddly for an old through-hole guy. 🙁 Ah, well: ESP-12s are cheap now…).

  10. Hi Sara,

    I have ESP8266-01 module and Arduino UNO.

    I have connected VCC and CH_PD of ESP8266 with 3.3V of Arduino UNO, GND with GND and RX and TX of ESP8266 with Pin 4 and 3 respectively of Arduino UNO. They both are working accordingly.

    Now to enable deep sleep mode:
    1. After the soldering of GPIO16 and RST, is there need to install ESP8266 board on IDE or by just simply writing the code will work as I have already connected ESP with Arduino IDE.

    2. Except soldering and NodeMCU, by just writing the code deep sleep mode work?

    3. Is there need to include library of ESPWIFI.h in my code?

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.