Overview:
In this project, we will build an IoT-based 12V Battery Monitoring System using ESP8266 and INA226 DC Current Sensor. This system is specifically designed for monitoring lead-acid batteries, which are widely used in automotive, solar, and other high-capacity applications. The primary goal of this system is to ensure the optimal performance and longevity of the battery by preventing overcharging or excessive discharging, which can lead to battery damage or system failure.
In this setup, we are using the INA226 sensor, a high-precision current and power monitor, to provide accurate readings of the battery’s voltage, load voltage, current, and power. These readings are crucial for maintaining the health and efficiency of the battery. The ESP8266, a low-cost Wi-Fi microchip with full TCP/IP stack and microcontroller capability, is used to send this data to the ThingSpeak server.
The ThingSpeak server, an open-source Internet of Things (IoT) application and API, is used to collect and store sensor data in the cloud and develop IoT applications. This allows users to monitor the battery status remotely from anywhere in the world via their smartphones or computer dashboards. The server displays the battery voltage, load voltage, current, and power, providing a comprehensive overview of the battery’s condition in both charging and discharging states.
Check our previous similar project for 3.7V Lithium-Ion/Lithium-Polymer Batteries:
- IoT Based Battery Status Monitoring System using ESP8266
- IoT Battery Monitoring System with DIY LiPo Charger
Bill of Materials:
The following are the components required for building the IoT-Based 12V Battery Monitoring System Project.
| S.N. | Components | Quantity | Purchase Links |
|---|---|---|---|
| 2 | NodeMCU ESP8266 or Wemos D1 Mini Board | 1 | Amazon | AliExpress | SunFounder |
| 3 | INA226 Current Sensor | 1 | Amazon | AliExpress |
| 4 | Jumper Wires | 10 | Amazon | AliExpress | SunFounder |
| 5 | Breadboard | 1 | Amazon | AliExpress | SunFounder |
| 6 | Micro-USB Cable | 1 | Amazon | AliExpress |
| 7 | 12V Lead-Acid Battery | 1 | Amazon | AliExpress |
Circuit Design & Schematic:
Let us move to the project part. We could have used INA219 Current Sensor for this project, but INA226 has voltage limitations of 26V and the maximum current it can measure is ±3.2A.
The INA226 can measure the voltage up to 36V and the current up to 30A. The current setting is based on the shunt resistance that needs to be changed based on current requirements. Follow the INA226 Interfacing Guide for learning more about current settings.
Let us take a look at the schamtic of IoT Based 12V Battery Monitoring System with ESP8266.
The SDA and SCL pins of INA226 are connected to the D2 & D1 pins of the ESP8266 Board. The Vin+ pin should be connected to a power source and the Vin- to the load as shown in the design schematic. The INA226 Sensor has VBus Pin, which is used to measure the Load Voltage. Therefore we need to connect the VBus pin to Vin- pin.
You may use a breadboard for connection or design your own custom PCB.
Setting up Thingspeak
In order to Monitor the Battery Data on Thingspeak Server, you first need to Setup the Thingspeak. To set up the Thingspeak Server, visit https://thingspeak.com/. Create an account or simply sign in if you created the account earlier. Then create a new channel with following details.
The parameters that we are gonna measure is Battery Voltage, Load Voltage, Current and Power. Therefore, we need to create a 4 parameters.
Then go to the API section of the dashboard and copy the API Key. This API key is needed in the code part.
Now your Thingspeak account setup is complete.
Source Code/Program
Let us move to the programming part of IoT Based 12V Battery Monitoring System with ESP8266. The code requires INA226 Library for compilation. First add the library to the Arduino IDE.
From the following lines, change the WiFi SSID, Password & Thingspeak API Key.
|
1 2 3 |
String apiKey = "**********"; const char* ssid = "**********"; // Enter your WiFi Network's SSID const char* pass = "**********"; // Enter your WiFi Network's Password |
Here is a complete code for this project.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
#include <Wire.h> #include <ESP8266WiFi.h> #include <INA226_WE.h> #define I2C_ADDRESS 0x40 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"; INA226_WE ina226 = INA226_WE(I2C_ADDRESS); WiFiClient client; void setup() { Serial.begin(115200); while (!Serial); // wait until serial comes up on Arduino Leonardo or MKR WiFi 1010 Wire.begin(); ina226.init(); /* Set Resistor and Current Range if resistor is 5.0 mOhm, current range is up to 10.0 A default is 100 mOhm and about 1.3 A*/ ina226.setResistorRange(0.1, 1.3); // choose resistor 0.1 Ohm and gain range up to 1.3A /* If the current values delivered by the INA226 differ by a constant factor from values obtained with calibrated equipment you can define a correction factor. Correction factor = current delivered from calibrated equipment / current delivered by INA226*/ ina226.setCorrectionFactor(0.93); Serial.println("INA226 Current Sensor Example Sketch - Continuous"); ina226.waitUntilConversionCompleted(); //if you comment this line the first data might be zero Serial.println("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, pass); while (WiFi.status() != WL_CONNECTED) { delay(100); Serial.print("*"); } Serial.println(""); Serial.println("WiFi connected"); } void loop() { float shuntVoltage_mV = 0.0; float loadVoltage_V = 0.0; float batteryVoltage_V = 0.0; float current_mA = 0.0; float power_mW = 0.0; ina226.readAndClearFlags(); shuntVoltage_mV = ina226.getShuntVoltage_mV(); batteryVoltage_V = ina226.getBusVoltage_V(); current_mA = ina226.getCurrent_mA(); power_mW = ina226.getBusPower(); loadVoltage_V = batteryVoltage_V + (shuntVoltage_mV / 1000); Serial.print("Battery Voltage"); Serial.print(batteryVoltage_V); Serial.println("V"); Serial.print("Load Voltage: "); Serial.print(loadVoltage_V); Serial.println("V"); Serial.print("Current: "); Serial.print(current_mA); Serial.println("mA"); Serial.print("Power: "); Serial.print(power_mW); Serial.println("mW"); if (!ina226.overflow) { Serial.println("Values OK - no overflow"); } else { Serial.println("Overflow! Choose higher current range"); } if (client.connect(server, 80)) { String postStr = apiKey; postStr += "&field1="; postStr += String(batteryVoltage_V); postStr += "&field2="; postStr += String(loadVoltage_V); postStr += "&field3="; postStr += String(current_mA); postStr += "&field4="; postStr += String(power_mW); postStr += "\r\n\r\n\r\n\r\n"; client.print("POST /update HTTP/1.1\n"); delay(100); client.print("Host: api.thingspeak.com\n"); delay(100); client.print("Connection: close\n"); delay(100); client.print("X-THINGSPEAKAPIKEY: " + apiKey + "\n"); delay(100); client.print("Content-Type: application/x-www-form-urlencoded\n"); delay(100); client.print("Content-Length: "); delay(100); client.print(postStr.length()); delay(100); client.print("\n\n\n\n"); delay(100); client.print(postStr); delay(100); } client.stop(); Serial.println("Sent Successfully :)"); Serial.println(); delay(3000); } |
From the Board Manager, select the NodeMCU 1.0 Board and the COM port. Then hit the upload button to upload the code to the ESP8266 Board.
Monitoring 12V Lead-Acid Battery on Thingspeak:
Open the Serial Monitor after uploading the code. The ESP8266 will try connecting to the WiFi Network. Once it connects to the WiFi Network, it will display the Battery Voltage, Load Voltage, Current and Power.
If nothing is connected at the load, it will only display the Battery Voltage and else everything will appear zero.
In order to test the current and power consumption, connect a Load like Motor, 12V LED Lights or anything else at the Load Terminal.
Now go to the private view of Thingspeak Dashboard. The Dashboard will shows the values of Battery Voltage, Load Voltage, Current and Power in graphical format as per time.You may start charging the Battery using 12V Battery Charger and observe the change in Current and Voltage on the graph.
Conclusion:
In conclusion, we successfully designed and built an IoT-based 12V Battery Monitoring System that leverages the ESP8266 and INA226 DC Current Sensor for optimal monitoring of lead-acid batteries. This sophisticated system safeguards battery performance and longevity by preventing overcharging and excessive discharging, which are common culprits of battery damage and system failure. By accurately measuring vital parameters such as battery voltage, load voltage, current, and power, our system promotes effective battery health management.
Furthermore, with the integration of the ThingSpeak server, users can effortlessly monitor their battery status from anywhere around the globe, providing enhanced convenience and flexibility. This comprehensive solution therefore, not only enhances the understanding of the battery’s condition in various states but also opens the door to potential advancements in remote battery management and other IoT applications in the future.













3 Comments
Awesome project but would you please add Blynk legacy code. I still have my own Blynk legacy v1 server and find Blynks GUI so much more flexible.
“users can effortlessly monitor their battery status from anywhere around the globe”
and then absolutely nothing you can do about it because you are “anywhere around the globe” and not where the battery is.
How about to be able remotely charge or disconnect the battery if needed?
Huh? This just measures Current/Voltage. It doesn’t prevent anything.