ESP8266 Deep sleep mode

By snorlaxprime

I always wanted to learn about the deep sleep mode in ESP8266. So I stumble across a few post in the internet, and comparing various post, there are several ways the ESP8266 can perform the deep sleep. They are:

  1. Deep-sleep
  2. Light-sleep
  3. Modem-sleep

Each mode can be described in the following tables:

As you can see in the table above, the point to have Deep-sleep is to conserve as much current as possible. In all mode above the Wi-Fi will be off. This will ensure that maximum power saving is achieved. In Deep-sleep the only things that awake is the RTC, so the typical application for this would be for a sensor reading that will happened periodically to take up every few minutes for non mission critical and update the reading. A good example for this would be a temperature sensor that wakes up every few seconds or even minutes to read the temperature and update this reading, this can be through MQTT, Thingspeak or IFTTT.

According to ESP8266 SDK, you can only sleep for 4,294,967,295 µs which is about 71 minutes.

Wake up Trigger

When ESP8266 is in Deep-sleep, it will need a mechanism to wake-up, given that everything, including CPU is OFF. The only thing that is still on is RTC. To wake up that means we have 2 options, first one is to set the time to wake up, and the second one is to press the reset button on the ESP8266 to wake it up. The first one can be done using the following code:

ESP.deepSleep(15e6);  // deep sleep for 15 seconds

To allow this to happen you will need to connect the WAKE up pin GPIO 16 (D0) to the RST pin. This can be shown in the following diagram:

Please note that when the RST is connected to D0, this will not allow the code to be uploaded to the ESP8266. The suggestion is to have a jumper or switch to allow the control of the connection between RST and D0 pin.

So with a practical example to allow reading of temperature sensor every 15 seconds we can modify the circuit above by adding the LM35 temperature sensor and a switch to allow connection and disconnection between RST and D0. This is shown in the following diagram:

The normal setup() function will need to contain the code to connect to WiFi and the posting of the reading of the temperature. This can be done using the following code:

void setup() {
   Serial.println("device is in Wake up mode");
   while (!Serial) { }
   connectToWifi();                // connect to WiFi
   int value = analogRead(A0);
   float volts=(value * 3.03) /1024;      //conversion to volts   
   tempC = volts*100;             //conversion to temp Celsius
   Serial.print("Temperature C: ");
   data();                       // post data else where
   Serial.println("deep sleep for 15 seconds");

In the code above once the ESP8266 wake up, the first thing it will do is to connect the the WiFi via the connectToWifi() function. Then it will read the sensor reading from A0, and convert this reading to celsius and posted the data and go back to sleep. The connectToWifi() function is shown below:

void connectToWifi() {
   Serial.print("Connecting to: SSID NAME"); //uncomment next line to show SSID name
   WiFi.begin(ssid, password);  
   Serial.println(" ");// print an empty line
   Serial.print("Attempting to connect: ");
 //try to connect for 10 seconds
   int i = 10;
   while(WiFi.status() != WL_CONNECTED && i >=0) {
     Serial.print(", ");
   Serial.println(" ");// print an empty line
 //print connection result
   if(WiFi.status() == WL_CONNECTED){
     Serial.println(" ");// print an empty line
     Serial.print("NodeMCU ip address: "); 
   else {
     Serial.println("Connection failed - check your credentials or connection");

Thus, the loop() function will not contain any code, as this will never get executed at all. This is shown below:

void loop() {
   //if deep sleep is working, this code will never run.
   Serial.println("This shouldn't get printed");

This conclude my learning on ESP8266 Deep sleep, please let me know if it is useful and don’t forget to subscribe for more and don’t hesitate to let me know if you have any questions.