In this project, we designed an IoT-based Battery Status Monitoring System using the ESP8266 NodeMCU board. Through this, we can monitor the total charging or discharging status along with battery voltage and percentage 24/7.
As we know, the battery is the most important electronics part for any portable devices as well as some non-portable devices. It powers the entire system where constant power is not reachable. So, it is important for us to monitor the voltage level of the battery, as excessive charging/discharging may lead to damage to the battery badly. Most electronic devices have a separate protection system called a Battery Management System (BMS). BMS monitors all the properties of the battery like the voltage, current, and temperature as well as control the auto-cut-off feature. This ensures the safety and proper handling of Lithium-Ion or Lithium Polymer batteries.
Due to some limitations, it only can monitor the battery’s health and indicates to the user accordingly. But the Internet of Things makes this problem easy for us. Now we can get directly notified about the battery status via smartphone from anywhere.
In this IoT-based Battery Monitoring System, we will use ESP8266 to send the battery status data to the Arduino IoT cloud server. Then the Arduino IoT will display the battery voltage along with the battery percentage in both the charging and discharging cases.
- ESP8266 NodeMCU Board
- TP4056 Lithium-Ion Battery Charging Module
- 100KΩ Resistor (x2)
- Lithium-Ion Battery
- Connection Wires
What is Lithium-Ion Battery?
The main component of our project is the lithium-ion battery. A lithium-ion battery or Li-ion battery is a type of rechargeable battery. Lithium-ion batteries are commonly used for portable electronics and electric vehicles.
During charging, lithium ions move from the positive electrode through an electrolyte to the negative electrode, and vice versa when discharging. Li-ion batteries use an intercalated lithium compound as the material at the positive electrode and typically graphite at the negative electrode. The batteries have a high energy density, no memory effect and low self-discharge.
Most Lithium-Ion batteries have a nominal voltage of 3.7V. When the battery gets the full charge, the maximum voltage is gone to around 4.2V. If you see the manufacturer datasheet, it is clearly mentioned that the cut-off voltage is 3V and varies depending on the battery type and applications. The battery I am using has a discharge cut-off voltage of 2.8V.
Circuit Connection of Battery Status Monitoring System
We will design and implement a battery monitoring system to monitor the battery health status along with voltage, charging, and discharging. We use NodeMCU ESP8266 Board here as the main microcontroller of this project. This WiFi module can interface with the WiFi network and uploads the data on a real-time basis to the server.
We also use the TP4056 (or MCP73831) battery charging module to charge our 3.7V Li-ion battery as it is suitable for a battery management system. Different types of BMS can also be used for this purpose.
The ESP8266 NodeMCU can support direct input Analog voltage of 0V-3.3V (A0 pin) as it has an inbuilt voltage divider circuit. But the voltage of the Li-Ion battery goes up to 4.2V. Hence we have to form a voltage divider network to lower the input voltage.
After designing the schematic diagram of the Battery Status Monitoring System, the assembled components and wiring were too clumsy and looked unprofessional. In fact, the wiring also has a chance of loose connection. Then I realised that we need to design the whole system small so that it will fit in a small box easily. To give it a clean and professional look I decided to build its PCB prototype using EasyEDA software as it is so simple to use. Now come to the main part, where we need to order our PCB prototype. I always prefer PCBWay for their quality assurance, fastest delivery and also for 24/7 customer support.
I’ve done several runs with PCBWay and am happy with the results, it’s good quality. The etching, solder mask, and hole sizes are all done well and that is what matters to me. It takes a few hours for a design to get approved and then a couple of days to finish and ship.
With more than a decade in the field of PCB prototypes and fabrication, PCBWay has proved its assurance from time to time. They always look at the needs of the customer from different regions in terms of quality, on-time delivery, cost-effectiveness and any other demanding requests. PCBWay is a place that brings manufacturers and customers together. As one of the largest and most experienced PCB manufacturers in China, PCBWay is proud to be the best business partner in any aspect of PCB needs.
High Delivery Rate
In previous years, the on-time delivery rate was 99%. Rather than PCB quality, PCBWay knows that the other most important factor is the shortest possible lead time, which is critical for R&D engineers, especially in the prototyping phase. PCBWay keeps working to ensure PCBs will be delivered to customers as soon as possible and they’re delighted. It takes only 3 to 15 days according to the courier service to deliver your products. After PCBWay moved to the new factory, the production capacity of multi-layer PCB can be as high as tens of thousands of square meters per month. So on-time delay is also reduced for that. They have the “Return and Refund” principle, for every unusable board caused by PCBWay, they will rebuild and refund the order soon.
When talking about quality assurance, all the PCB prototypes go through the most stringent tests rather than the basic visual checks. They have most of the testing and inspecting types of equipment used in the PCB prototyping industry, such as Flying Probe Tester, X-Ray Inspection Machine, and Automated Optical Inspection (AOI) Machine.
Previously there have 9 different types of solder masks available in their company. But now recently they have launched extra 4 solder masks that are pink, grey, orange and transparent solder masks.
Also, there is a piece of great news about the price reduction of PCB prototypes. From 5th August you can now buy 4-layers & 6-layers with upto 20% discounts. As the price dropped, don’t think they will compromise on the quality.
There are three types of assembly services:
- Turnkey where PCBWay supplies all PCB parts.
- Kitted or Consigned where the customer is responsible for buying and sending all parts to PCBWay.
- Combo, where the customer supplies some parts and PCBWay, completes the rest.
3D Printing & CNC
It also produces Rapid Prototyping like 3D printing, CNC Machining, Sheet Metal Fabrication, and Injection Molding. The quality of the product is too premium and reliable. And also the best part is that you will receive your order within a minimum of 3 days.
Voltage Divider Calculations
The maximum battery voltage is 4.2V and the cut-off voltage is 2.8V. Any values lesser than 3.3V will be easily supported by the ESP8266 analog pin (A0).
We have to first step down the upper voltage level for taking the analog value. The maximum battery voltage is 4.2V and there is a pair of 100KΩ resistors in series with the battery. This will give an output of 2.1V. Similarly, the minimum voltage is 2.8V as a cut-off voltage which steps down to 1.4V using the same voltage divider network. Now, both the upper and lower voltage is supported by the ESP8266 Analog pin.
Vout=Vin×[R2 / (R1+R2)]
Setting Up Arduino IoT Dashboard for Battery Status Monitoring System
In order to display the battery status on the Arduino IoT server, we need to set up the Arduino IoT dashboard. First, visit Arduino IoT SSO. Create an account or sign in if you created it already. Then click on “Create Thing” and give it a title.
Now go to the Thing tab and create two global variables that are “bat_percentage” as an integer type and “voltage” as a floating-point number respectively by clicking “Add Variables”.
Now, we need to add our ESP8266 NodeMCU as a third-party device. To do that click on “Select Device” and then click on add third party device. Choose ESP8266 and select NodeMCU 1.0 ( ESP8266 12E). Click on continue and provide our device name.
Finally, click on the “Download PDF” button and save it securely for future use of the Device ID and Secret key.
Now we click on “Add network” and provide the WiFi SSID, Password, and Device Secret key. Then click on save to link our WiFi network. We need to build a computer dashboard as well as a mobile dashboard to visualize the real-time data. So, let’s move to the dashboard tab and create a new dashboard and named it “Battery Monitoring System”. Now click on “Add” to add widgets to your dashboard. In this tutorial, we will use one gauge, one percentage, and two chart widgets to display our data on the dashboard.
The First widget (Gauge is for monitoring the battery voltage level. So we will name it “Battery Voltage” and link the “voltage” float-point variable from the Things tab that we created earlier. Similarly, for “Battery Percentage”, we will link a “bat_percentage” integer variable from the Things tab.
Finally, we need to configure one chart widget for “Battery Voltage” and another for “Battery Percentage” and link those with “voltage” and “bat_percentage” variables respectively. Now we have created our final dashboard. Now let’s go to program our NodeMCU ESP8266 to send the data to the Arduino IoT Cloud server.
Upload The ESP8266 Code Using Arduino IoT Website
The advantage of using Arduino IoT Cloud is, that you can program your ESP8266 board from the Arduino IoT website directly. You don’t need any separate software like Arduino IDE. Simply install and run “Arduino Create Agent” and upload your code through this. That’s it.
After that, return to the Things tab and click on the sketch option. Simply, copy and paste the below code and compile it. Then upload the code to your NodeMCU board accordingly.
Calibrate Analog Voltage Value
The circuit is designed to fix 100KΩ pair of resistors. But most of the resistors have a tolerance of ±5%. Because of this resistor values may be between 95KΩ to 105KΩ. As a result, the output voltage and the Analog signal output both will be affected.
In order to fix this, we can compare the voltage difference between the serial monitor reading and the multimeter reading. Test the actual output voltage at the TP4056 output terminal using a multimeter.
Subtract the multimeter voltage value from the value obtained on the serial monitor.
In the following line of the code add this calibration factor.
This will fix any error in the voltage reading. So this is how we can design an IoT-Based Battery Status Monitoring System using ESP8266 and get the reading on Arduino IoT Cloud Server. In case you want a 99% accurate battery status monitoring system, you can use MAX17043 fuel gauge IC which removes all the limitations of measuring battery percentage by this method.
How to Install ESP8266 Board Manager?
Click on the “File” from the Arduino IDE. Then click on “Preference”. From the additional board manager, you have to paste this URL: https://arduino.esp8266.com/stable/package_esp8266com_index.json. After that click on “Tools→Board→Boards Manager→Type ESP8266 and install.
ESP8266 NodeMCU Code
String apiKey = "**************";
const char* ssid = "**************"; // Enter your WiFi Network's SSID
const char* pass = "**************"; // Enter your WiFi Network's Password
const char* server = "api.thingspeak.com";
int analogInPin = A0; // Analog input pin
int sensorValue; // Analog Output of Sensor
float calibration = 0.36; // Check Battery voltage using multimeter & add/subtract the value
Serial.println("Connecting to ");
while (WiFi.status() != WL_CONNECTED)
sensorValue = analogRead(analogInPin);
float voltage = (((sensorValue * 3.3) / 1024) * 2 + calibration); //multiply by two as voltage divider network is 100K & 100K Resistor
bat_percentage = mapfloat(voltage, 2.8, 4.2, 0, 100); //2.8V as Battery Cut off Voltage & 4.2V as Maximum Voltage
if (bat_percentage >= 100)
bat_percentage = 100;
if (bat_percentage <= 0)
bat_percentage = 1;
Serial.print("Analog Value = ");
Serial.print("\t Output Voltage = ");
Serial.print("\t Battery Percentage = ");
if (client.connect(server, 80))
String postStr = apiKey;
postStr += "&field1=";
postStr += String(voltage);
postStr += "&field2=";
postStr += String(bat_percentage);
postStr += "\r\n\r\n";
client.print("POST /update HTTP/1.1\n");
client.print("X-THINGSPEAKAPIKEY: " + apiKey + "\n");
float mapfloat(float x, float in_min, float in_max, float out_min, float out_max)
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;