Shares
How to Run Your ESP8266 for Years on a Battery

How to Run Your ESP8266 for Years on a Battery

Last Update: / by Marco Schwartz

For most of the projects I am building with the ESP8266 WiFi chip, I usually don’t care too much about the power consumption aspect. I for example build data loggers that are constantly connected to the mains electricity, and appliances controller which also have an easy access to power. However, in some cases, we want to build projects that are only powered by batteries. This is for example the case for a motion sensor that you will install in your home, or a data logger you would put in a remote location.

For those cases, you don’t want to be changing the batteries constantly. For example, an ESP8266 chip with a standard 2500mAh LiPo battery would last for about 30 hours. Not good enough. That’s why in this article, I will show you how to significantly reduce the power consumption of your ESP8266 boards using the deep sleep mode of the chip, so you can build projects that will last for years on a single battery. Let’s dive in!

Hardware & Software Requirements

Let’s first see what we need to build this project. The first thing you need is an ESP8266 board. Here, as we want the project to be low-power, the most important is to choose a board without a lot of features, so there are no extra components to reduce the battery life of your project. Here, I choose the SparkFun ESP8266 Thing as it allows to work at very low powers.

You will also need a 3.3V FTDI USB adapter, as well as a breadboard and jumper wires. Optionally, to test the power consumption part, you will also need a breadboard power supply, a multimeter, and a LiPo battery.

This is the list of the required components for this project:

You will also need the latest version of the Arduino IDE, as well as the ESP8266 board definitions.

Hardware Configuration

Let’s now assemble the project. As we just want to lower the power consumption of the board, the configuration will be quite simple here. If you just want to use the project with a low power consumption, you simply need to connect the DTR pin of the board to the XPD pin, which will make sure the chip can wake up from the deep sleep mode.

Here, I want to measure the power consumption as well, so I’ll also be using a breadboard power supply, and connect the power to a multimeter so I can measure the current flowing through the chip. Here is a closeup picture of the project:

How to Run Your ESP8266 for Years on a Battery

This is a picture from farther away, showing the connections to the multimeter:

How to Run Your ESP8266 for Years on a Battery

Reducing the Power Consumption of Your ESP8266

We are now going to see how to lower the power consumption of your ESP8266 WiFi chip. To do that, we are going to use the deep sleep functions of the chip, that will simply sleep when no actions are required. As a simple example, we are going to log a simple dummy message to Dweet.io, which is a cloud service that is used to log data online. This will for example illustrate a data logger project that will only make measurements every 10 minutes for example, and sleep the rest of the time.

This is the complete code for this part:

// Library
#include 

// WiFi settings
const char* ssid = "wifi-name";
const char* password = "wifi-password";

// Time to sleep (in seconds):
const int sleepTimeS = 10;

// Host
const char* host = "dweet.io";

void setup() 
{

  // Serial
  Serial.begin(115200);
  Serial.println("ESP8266 in normal mode");
  
  // Connect to WiFi
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  
  // Print the IP address
  Serial.println(WiFi.localIP());

  // Logging data to cloud
  Serial.print("Connecting to ");
  Serial.println(host);
  
  // Use WiFiClient class to create TCP connections
  WiFiClient client;
  const int httpPort = 80;
  if (!client.connect(host, httpPort)) {
    Serial.println("connection failed");
    return;
  }
  
  // This will send the request to the server
  client.print(String("GET /dweet/for/myesp8266?message=lowpower") + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" + 
               "Connection: close\r\n\r\n");
  delay(10);
  
  // Read all the lines of the reply from server and print them to Serial
  while(client.available()){
    String line = client.readStringUntil('\r');
    Serial.print(line);
  }
  
  Serial.println();
  Serial.println("closing connection");

  // Sleep
  Serial.println("ESP8266 in sleep mode");
  ESP.deepSleep(sleepTimeS * 1000000);
  
}

void loop() 
{

}

This code is quite long, but let’s now focus on what we need for the deep sleep functions. First, we define how long we want the chip to stay in deep sleep mode. For test purposes, I set it to 10 seconds here:

const int sleepTimeS = 10;

Then, inside the setup() function of the sketch, after sending the request to Dweet.io we put the chip in deep sleep mode:

Serial.println("ESP8266 in sleep mode");
ESP.deepSleep(sleepTimeS * 1000000);

Note that here we need to put the whole code inside the setup() function of the sketch, as whenever the chip goes out of deep sleep mode, it starts again at the start of the setup() function.

You can get the whole code from the GitHub repository of the project:

https://github.com/openhomeautomation/esp8266-battery

It’s now time to test the project! First, remove the connection between DTR and XPD, so you can actually program the board. Also modify the WiFi credentials inside the code. Then, upload the code to the board, and connect the jumper cable again.

If you have a multimeter monitoring the current consumption of the ESP8266, this is what you first should see when the chip is booting:

How to Run Your ESP8266 for Years on a Battery

This is the current that is used when the board is uploading data to Dweet.io, but it is also what the chip would use if we didn’t do any kind of optimisation for power. In that case, a 2500 mAh battery would last about 28.5 hours.

After a few seconds, the chip will enter deep sleep mode, and you should immediately see the power consumption going down:

How to Run Your ESP8266 for Years on a Battery

As you can see, we already have a 10 times lower current consumption! At this rate, if we take the case of a data logger that just stays in sleep mode most of the time, the battery would now last 300 hours, or 12.5 days! It’s already a great improvement, but we can do much more with the SparkFun Thing.

Actually, most of this power is now used by … the power indicator LED on the board! This is great when you are developing applications on your desk, but not that useful when you are deploying your project in the field. Therefore, we are going to get rid of this LED here.

For newest versions of the SparkFun thing, you can simply unsolder the “PWR” jumper at the back of the board. For older versions like the one I have, you can simply cut the trace between the PWR LED and the nearby resistor. After that, just power the project again. The reading on the multimeter immediately changed to 77 uA, or 0.077 mA. This means that the same project will now last on the same battery for … 3.7 years! Of course, this doesn’t take into account the characteristics of the battery, so in reality you will end up with 1-2 years battery life for your project.

How to Go Further

In this article, we learned how to reduce the power consumption of the ESP8266 WiFi chip, so you can build projects that last for years on a single battery. Of course, this is an ideal situation, and it can’t be applied to all projects, and in reality probably the battery will be dead before the time I calculated in the article. However, this is a great solution for anybody interested in data logging projects where the device is spending most of the time doing nothing.

You can now use what you learned in the project, and build your own projects with it. You can simply use the code I used in this article, and just add a few lines to make measurements from sensors, and send those measurements to Dweet.io or another cloud platform of your choice.

Note that this article is a continuation of the very popular article I wrote before about How to Run an Arduino for Years on a Battery.

Want to learn more? Get my free eBook about the ESP8266!
The ESP8266 is an amazing WiFi chip that you can use to build all sorts of projects. Download today my free eBook "Build a WiFi Weather Station with the ESP8266". Simply click on the button below!

Leave a Comment

Please enter your name.
Please enter your email.
Please enter your comment.
yrhumbleservant 2 months ago
Are all the image links in this article broken or is it just me?
Reply
Please enter your name.
Please enter your email.
Please enter your comment.
sircuri yrhumbleservant a month ago
Same here. Would like to see which connection to cut to get rid of the LED.
Peter Johnston 7 months ago
Anyone ever tried to get the sgp30 sensor to sleep? It uses more current than the esp8266 itself when measuring, but there documentation mentions a sleep mode you can enable.
Reply
Please enter your name.
Please enter your email.
Please enter your comment.
Rob Mort 8 months ago
I'm developing an even lower power system by using an ultra low power PCF 8563 timer to switch on power to the ESP8266 only when it is needed (about twice a day in my case to monitor a beehive). This elimates the quiescent current of the ESP8266. It does complicate the start up of the 8266 each time but that's only software.
Reply
Please enter your name.
Please enter your email.
Please enter your comment.
Morten Vinding Rob Mort 6 months ago
PCF8563 is using 25μ according to datasheet, the ESP8266 is using 20μ in deep sleep (which can do RTC wake up), so what is the point?
joe mckee 8 months ago
Using an ESP8266 12E and it doent have DTR pin of the board to the XPD pin , whats my options ?
Reply
Please enter your name.
Please enter your email.
Please enter your comment.
Steve joe mckee 8 months ago
Connect RST to D0.
Steve joe mckee 8 months ago
Also, you will have to disconnect the the wire when programming the ESP.
John Jacobs 8 months ago
Nice post! To integrate this into an existing project what area of the code do I just paste into my project?
Reply
Please enter your name.
Please enter your email.
Please enter your comment.
Marco Schwartz John Jacobs 8 months ago

I'd take everything of this project and basically include the code you want to run while the board is not sleeping :)

John 10 months ago
Hi Guys, nice post.. I think Mike did a pretty good job correcting Marco's Math. However, everyone is overlooking the fact that the ESP8266 may take as much as 400mA during transmission time! I have tested this power consumption on several units and its consistent. In average, the current during TX is about 80mA but sporadically peaks up to 414 mA during 17 microseconds several times (maybe the radio is the culprit). This short time may not be long but it definitely adds up to the total of power consumed by the module, an oscilloscope is required (and a current to voltage converter) to measure these peaks. It would be "invisible" to the slow Multi-meter. During deep sleep mode the device take 50uA.
Reply
Please enter your name.
Please enter your email.
Please enter your comment.
Zeni a year ago
Hi Thanks for the article. Give ideas to newbies like me. I think solar power may be a solution if unattended long term performance is intended. Can be used indoors also (after all we use solar powered calculators all the time). Just loud thinking.
Reply
Please enter your name.
Please enter your email.
Please enter your comment.
Fabrizio a year ago
Hi Marco, I'm using an ESP8266-12E board with sensors and shields whose overall consumption is about 150mA: this board has a recommended voltage of 5 Volt (and a voltage regulator). Could I safely use 2x18650 batteries in series so to have a nominal voltage of 7.4V?
Reply
Please enter your name.
Please enter your email.
Please enter your comment.
Marco Schwartz Fabrizio a year ago
If it's within the input range of the regulator, that shouldn't be a problem!
Mike a year ago
Your math is WAY off. You aren't figuring in the time that the module is awake and the power usage during that. Your code takes about 0.6 seconds to run. You have it running every 10 seconds. Assume it uses 170ma when awake/running (since it's actively using the wireless and serial port pretty much the whole 0.6s. The 81ma is just as it boots but when the radio is active, it uses around 170ma. Try moving your code to loop() and then remove the commands to make it sleep so that it's constantly running the radio and serial port and then check your power usage and you'll find it's much higher.) So when it's awake, it uses 170ma*0.6s or 102mas (milliamp-seconds.) When it's sleeping, it uses 0.077ma*10s or 0.77mas. Each cycle thus uses up 102.77mas and takes 10.6s to run. The 2500mah battery is a 2500*3600=9,000,000mas battery. So it'd last 9,000,000/102.77 or 87,574 cycles. This comes out to a bit over 10 days. Now if you did have it cycle every 10 mins (instead of 10 secs that the code does) then you'd have it use up 102mas when awake and 46.2mas when asleep. Each cycle would take a wee bit over 10 mins so we'll just say 148mas every 10 mins. Now the battery would last 60,810 cycles. So fewer cycles but each cycle is 60 times longer. So it's now 608,100 mins or 10, 135 hours or 422 days. So your 3.7 years is actually more like 1.15 years. But a LiPo battery will lose about 5%/month just due to self-discharge. So if the battery was left with no load, in 1 year, it loses about 60%. So your "1.15 years" is probably more like 6 months or so, tops.
Reply
Please enter your name.
Please enter your email.
Please enter your comment.
Marco Schwartz Mike a year ago

Hi Mike, your calculation is definitely more accurate that mine, but as you probably know, physics & engineering calculations works by taking a given level of approximation. I just used more approximations than yours to make my calculations, to keep it simple for everyone. 

Francesco Mike 10 months ago
Hi Mike, thanks for your post. It was very interesting form me. It gave me an idea to how to compute a more accurate power consumption computation. For sure I also thank Marco, who first wrote this article so useful for me. :)
Francesco Mike 10 months ago
Hi Mike, thanks for your post. It was very interesting form me. It gave me an idea to how to compute a more accurate power consumption computation. For sure I also thank Marco, who first wrote this article so useful for me. :)
Diana a year ago
Hello, I use the code as is, and the module after returning of deep sleep remains suspended, that is to say the alone code executes once, already I have checked the circuit and you pines, the GPIO16 gives the descending flank, someone me prodria to help. Thank you.
Reply
Please enter your name.
Please enter your email.
Please enter your comment.
John a year ago
nice article
Reply
Please enter your name.
Please enter your email.
Please enter your comment.
Marco Schwartz John a year ago
Thank you!