Monitor de tensão da bateria com módulo WiFi NodeMCU Esp8266-12E

Battery Voltage Monitor with NodeMCU Esp8266-12E WiFi Module

Measuring the voltage of your solar panel, UPS and other everyday batteries is time-consuming. Taking the multimeter, opening the battery cases and touching the two multimeter leads to the battery terminals requires some effort. How about developing an efficient IoT system through which you can see your battery status on desktop browsers and mobile devices?
Let's get started and do this DIY project as we will develop an IoT system that can monitor the battery status and update us in our browser. We will need some circuit/device/controller that can measure voltage effectively.
A WiFi device that can connect to local home WiFi reads the voltage from the controller and updates the end user on the current battery level. To this end, I decided to use the NodeMCU WiFi module in the project. Not only can it work as a controller, but it can also connect to a WiFi network as a server or client. The battery monitoring circuit is a traditional voltage divider circuit. I will measure 12 volt batteries. The circuit can be modified to measure 24 volt batteries and even more parallel 48 volt battery packs.

Voltage divider operation and calculations

NodeMCU is a tiny device; works at 3.3 volts. Since it is running on 3.3 volts, its pins can only supply and drain 3.3 volts. A voltage greater than 5 volts could blow the pin or fry the NodeMCU. In our case, we want to measure a 12 volt battery, and the NodeMCU ADC (analog to digital converter) can only accept 3.3 volts. We need to play smart here. We will divide the voltage between two resistors and measure the single voltage across a resistor, and the remaining voltage of the resistor will be calculated mathematically. A typical voltage divider circuit and formula are given below.

Voltage divider with battery circuit

Voltage divider with battery circuit

Now let's calculate the values ​​of Rtop and Rbottom. Here we need some important considerations to be taken seriously.

  • Low ohm resistors can draw a lot of current and the wires can heat up instantly. Consequently, the threads can melt in seconds. Therefore, always use a sufficient amount of resistors for larger amp-hour batteries. I selected a 10k ohm resistor Rbottom.
  • During charging, the battery voltage may increase to 18 volts. For example, a 150-watt solar panel produces 17 volts at 6 amps during full sun. The output voltage can even reach above 18 volts . The solar charge controller also outputs voltage approximately equal to 15 volts to charge the batteries.

Formula Calculations

I will measure the voltage at Rbottom and randomly decided that its value will be 10k ohm. We know that Vout can have a maximum of 3.3 volts, since the NodeMCU works and accepts a maximum of 3.3 volts on its I/O pins. Vin is 18 volts when the battery is charging. Now we can find Rtop.

Calculation of voltage divider resistance value

Calculation of voltage divider resistance value
If 18 volts are on the battery side, it will be divided between the resistors, 3.3 volts will fall on the 10k resistor, and the remaining 14.7 volts will fall on the 44.54k resistor. The 44.54 k ohm resistor is not commercially available. I will use the one above this rating and it can be easily found in any electronics store. 47 k ohm resistor. If the battery is not charging and providing 12 volts, what will be the voltage drop across the resistors? Let's calculate

Voltage drop across voltage divider resistors

The voltage drop across the resistance of the voltage divider circuit
It is obvious from the above discussion that the voltage across Rbottom will not exceed 3.3 volts now. I hope this makes sense to readers about the calculations. The question now is how the 3.33 volts are converted into 12 volts by the NodeMCU or how from 3.33 volts we can predict that on the battery side the voltage is 12 volts. Well, a little more math is involved here. Since the resistor values ​​are fixed, we can calculate the voltage ratio between the resistors relative to the source and use this in the code for the actual voltage at the source. The way the ratio is calculated is below.

Calculation of voltage divider ration

Voltage Divider Ratio Calculation
Two cases are given above when the source is at 18 volts and when the source is at 12 volts in both cases the relationship turns out to be a constant value. This relationship is used in the code to predict the actual voltage of the source/battery. The ratio is multiplied by the voltage at Rbottom to obtain the actual voltage value.
The circuit diagram of the project is given below. I'm using the NodeMCU's ADC0 channel to measure battery voltage. Both the battery and the NodeMCU power supply must be grounded to complete the circuit. Your most common mistake will be measuring the voltage where both grounds are not grounded together. If the NodeMCU ground is not connected to the battery ground, the adc0 pin will become a floating pin and begin reading floating values.

Battery voltage monitoring with nodemcu

Battery voltage monitoring with NodeMCU
Once you've completed the circuit, it's time to move on to the code. The code is written in the Arduino IDE. I used the ESP8266WiFi.h library in the code, so first, make sure to install this library in the Arduino library folder. If it is not present, download it from GitHub and install it first. Then enter the SSID and password of the WiFi network you want your NodeMCU to be connected to. Most likely, it will be your home WiFi. So enter the SSID and password. Now load the code into NodeMCU. Before uploading, make sure you select the correct board from Arduino boards. If NodeMCU does not appear in your board's drop-down menu, import the manager link and install the necessary files. You can find many tutorials on the internet on how to install NodeMCU support for Arduino IDE.
After submitting the code, you will have to do a little complex and unique work. Open the Arduino serial monitor. Once you open it, you will see the NodeMCU server starting and your WiFi assigning an IP to your NodeMCU. This IP is essential. You must enter this IP into your mobile or desktop browser to see the battery status. Suppose you don't see any messages on the serial monitor. Check the communication speed of the serial monitor. It should be 9600bps. Could you change it to 9600? If you still don't see anything, the code may not have loaded correctly or the board is disconnected. Check for possible errors. Once your router allows the IP for the NodeMCU, it will remain the same all the time. I hope so 😀
Once you hit the assigned IP in your browser, you will see the battery status page and a button. Press this button to get the updated temperature whenever you want.
Note : Nodemcu and your mobile client or desktop on which you want to view voltage must be connected to the same WiFi. If your nodemcu server and mobile client or computer, laptop, etc., are connected to different networks, you cannot see nothing after reaching the IP.

battery voltage monitor via WiFi

battery voltage monitor via WiFi
This is a basic project. Furthermore, developments can be made to the project. Similar data can be sent to the remote site to be displayed in real time on web pages. An alert can be generated when voltage is low. LEDs or LCD can be connected to the NodeMCU for on-site status viewing.
To learn about other ways to measure battery voltage, follow the tutorial below. The tutorial is about measuring batteries when they are connected in series and parallel combinations.

Battery Voltage Monitoring Methods

Download the project code. The folder contains the project's .ino file. Please provide us with your feedback on the project. If you have any queries or queries, please write them below in the comments section.
Battery voltage monitor with MCU.ino node

Related Content

Back to blog

Leave a comment

Please note, comments need to be approved before they are published.