Pages

Friday, July 27, 2018

ESP power reduction with deepsleep


We all now that the ESP8266 draws a lot of power. So what when you want to run it on a battery. That gives you a problem. Luckily we do have power-banks nowadays. They have a large capacity and will run the ESP8266 for a longer time. But even that can sometimes not be enough.

Suppose you have a project in which the ESP8266 is outside your home. Or it is in a place where no power outlet is available. Or you want to hide it. Etc. Etc. Etc. In these cases a battery or a power bank will not last long enough. What to do.

Deepsleep

The ESP8266 has a function that is called 'sleep' which is supported in most languages. If the ESP8266 gets into Deepsleep most functions are put on hold until the ESP wakes again.

There are several versions of sleep called Modem-sleep, Light-sleep and Deep-sleep.



As you can see from the table Deep-sleep is the most efficient and will reduce power use to a minimum. So that is what we want to use.

There are two methods of waking from Deep-sleep. Which one do you use ???

The automatic wake up version.

In this version GPIO16 (D0) is connected to the RST (Reset) pin of the ESP8266. The Reset pin is normally HIGH (even when not connected). When the RST pin is connected to GND it restarts the microcontroller.

Fortunately the build-in Deep-sleep function sends a LOW signal to GPIO16 (D0) when the ESP8266 wakes up from it's Deepsleep. This is done automatically.

It works like this:
We tell the ESP8266 to go into Deep-sleep. The only thing that is running inside is the RTC (Real Time Clock) and when the set time has expired the Clock sends a LOW signal to GPIO16 (D0).
We connect a wire between GPIO16 (D0) and RST. This makes sure that when the ESP8266 wakes GPIO16 (D0) sets RST to LOW and the ESP resets and wakes.

Setback.
This sounds ok but there is a setback. The ESP is a 32 bit processor and therefore the largest unsigned integer value it can handle is 0xffffffff being 4294967295, or 71 minutes. So when this method is used the ESP will wake about every 71 minutes. This is still less than 24 times a day so you will get an enormous power saving.

The manual wake up version.

There is also an infinite Deep-sleep method. If we use this version the ESP will stay in Deep-sleep until we manually give it a push. That is easily done. You just manually send a LOW signal to the RST pin.
You can do that with pressing a button or closing a reed-switch controlled by a magnet or any other mechanical switch that connects RST to GND.

When to use manual or automatic.

This depends on the situation and the project at hand.

Suppose you want the ESP to send the temperature or any other sensor reading every half an hour to Dweet, Thingspeak or your Domotics system you can use the automatic wakeup version.

Now if you want the ESP to send a signal when a button is pressed, or the fridge door is opened, or a window is opened you use the manual version. Just connect the switch or reed contact to GND and RST and you are done.

The different ESP versions.

ESP-01



Unfortunately the ESP-01 has no GPIO16 pin on the connector. So we can not use the automatic wake up function. You can only use the manual wake up function

NodeMCU




The NodeMCU has all the needed pins available. GPIO16 (D0) is available on the top left side. It is even called WAKE in the pin layout. RST can be found on the 3d pin from below on the left side.

Please be aware that this will not work on NodeMCU version 0.95 and lower. It only works on V1.0 and up. Well at least my version V0.95 did not work your mileage may vary but be carefull and test.

Wemos D1 mini



The Wemos D1 has GPIO16 (D0) as the 3d pin from the top on the left side. And RST is even the first pin. Tested and verified. Automatic wake up works on the Wemos D1.

Software commands.

Deepsleep is supported is most languages.

In Arduino (C++) you can use:

ESP.deepSleep(20e6);   // 20e6 is 20 microseconds

ESP.deepSleep(0); // infinite for manual wake-up


In ESP-Basic you can use:

Sleep 60   ‘60 seconds

Sleep 0    'sleep infinite for manual wake-up


In LUA (NodeMCU) you can use:

node.dsleep(60000000)

where 60000000 = 60 seconds = 60 x 1000 x 1000

Arduino example




I used a Wemos D1 mini for this and programmed it with the Arduino IDE. This is the infinite Deep-Sleep version which you have to wake-up manually. I did that with the loose-end wire that is connected to RST. Just plug it into GND to wake the ESP.



void setup()
{
  pinMode(5, OUTPUT);
  digitalWrite(5, HIGH);
  delay (1000);
  digitalWrite(5, LOW);
  ESP.deepSleep(0); 
}


void loop()
{
digitalWrite(0, LOW);
}


The program sets the LED connected to GPIO5 (D1) ON, waits for a second and then sets the LED OFF and goes into Deep-Sleep.
The LED goes ON again when you connect RST to GND.


Basic example.



This is the schematic for automatic wake up. As you can see D0 is connected to RST.



cls
wprint "<h1>Deepsleep Test</h11>"
wprint "<br><br>"
io(po,d7,1)
button "testsleep", [sleeptest]
wait

[sleeptest]
io(po,d7,0)
sleep (60)
wait

The program is straightforward. Save it under the name 'default.bas' It sets a button on the screen and sets the LED ON. When the button is pressed the program jumps to the routine [sleeptest]. In [sleeptest] the LED is put OFF and the ESP is set to sleep for 60 seconds.

Before you run the program go to the SETTINGS page and make sure that 'run default.bas at startup' is checked. Otherwise the program will wake the ESP8266 but it will not restart the program automatically.

Just be aware that the ESP8266 will need about 30 seconds to start your Basic program after it is woken. The Arduino version is bloody fast.



This schematic is the version you will need for manual wake-up. When you want to use manual wake-up just alter the sleep command in:

sleep(0)

Real World usage

Let's see how it functions in real life.


The above reading is the current consumed when the ESP is working that is 0.07 Amps being 70 Ma.



And this is the reading when the ESP is in Deep-sleep.

Official figures from Espressif say that the actual current drawn in Deep-sleep is 300ua that is 0.3ma !!!

In the past I have published several projects on this weblog that could benefit from this technique.  for example the rain-sensor: https://lucstechblog.blogspot.nl/2016/08/rain-sensor-using-esp8266.html and the Thermometer: http://lucstechblog.blogspot.nl/2017/11/oh-no-not-another-wifi-thermometer.html.

I bet you can find projects that can benefit from this technique. I have one in my mind already.

Till next time
Have fun

Luc Volders