Tutorial: Cheap Battery Monitor Using Resistive Voltage Divider

1. jes1510 Johnny-5
Category
How To, DIY
Views
83,586
Replies
3

Cheap Battery Monitor Using Resistive Voltage Divider

Difficulty
Easy
Estimated Time
1 hour
Skills Required
Simple Soldering Skills
Understanding of Ohms Law
Basic Algebra
Parts Required
QTY 1 Resistor kit- Radio Shack Catalog # 271-312
QTY 1 Arduino USB Board- Trossen Robotics

If the student wants to transfer the circuit to a stand-alone board, then these parts would be helpful:
QTY 1 PC Board- Radio Shack Catalog #276-148
QTY 2 PC Board Terminals- Radio Shack Catalog #276-1388

Tools Required
Soldering Iron
Small Flat Head Screwdriver
Desoldering Braid
Voltmeter
Calculator

Introduction
The purpose of this tutorial is to show how a simple battery monitor can be designed for microcontroller circuits by simply using two resistors and a little knowledge of Ohms Law.

Before working through this tutorial the student should have a firm understanding of Ohms Law and how it pertains to current and voltage.

The Circuit
The circuit used in this tutorial is a simple two-resistor voltage divider. The circuit will take the input voltage from the battery pack and convert it to a lower voltage that can be read by a microcontroller, in this case an Arduino.

This is a schematic of the circuit used: Circuit Operation
This circuit is called a voltage divider. This is because the total voltage applied to the circuit is divided between two resistors. The amount of voltage that each resistor sees is determined by the resistor value. The voltage across each resistor is called voltage drop and is determined by the following formula:

VR2 = (R2/(R1+R2)) * Vin
Where:

VR2 is the voltage "dropped" across R2 from the node labeled "V_OUT" to ground
R2 is the value of R2 is Ohms
R1 is the value of R1 in Ohms
Vin is the battery voltage

Notice from the above formula that the amount of voltage across R2 is determined by it's value as a ratio of R1 and R2 combined. If both of the resistors are the same value then the voltage drop on R2 will be the same as the voltage drop on R1. As the battery voltage varies, the output voltage will vary proportionately. This is the property of the voltage divider that will be used to safely read a battery voltage.

Designing The Circuit
The first step in designing the circuit is to decide what battery pack will be used. For the sake of this tutorial a 9v battery will be used. Be aware that a rechargeable battery will actually charge to a voltage higher than the rated voltage. For example, a 7.2v rechargeable battery can charge to around 8.4v. When calculating the resistor values always use the absolute maximum voltage of the battery pack and allow a buffer for noise. The values calculated here will work fine for a 7.2v pack.

Next, determine how much voltage will be sent into the Arduino. It needs to be well beneath the maximum allowed for an Arduino analog pin which is 5v, so 4v will be used.

Finally, pick how much current the circuit will consume. This circuit will be drawing current from the battery pack whenever it is connected, so it needs to be kept fairly low. 100uA is chosen as it is fairly low and will not drastically affect battery life but is high enough to cut back on any noise.

It is known from Ohm's Law
that a resistor value in Ohms is equal to the input voltage divided by the current flowing through the resistor. The current will be the same in both of the resistors since this circuit divides voltage, not current. The following formula describes the circuit:

RT = R1 + R2:
RT = Battery Voltage / Current
Where:

RT = 9v / 100uA
RT = 90k Ohms
or
R1 + R2 = 90k

Since the voltage across R2 is determined by its relationship to R1, the voltage out will have the same relationship with the battery voltage. This is described by this formula:

R2 = (Vout/Battery Voltage) * RT

Where:

R2 = (4v/9v) * 90k
R2 = 40k

Now that the value for R2 is known, R1 can be calculated. This formula describes the total resistance of the circuit:

RT = R1 + R2

The formula must be rearranged to calculate R1:

R1 = RT - R2

Substitute known values:

R1 = 90k - 40k
R1 = 50k

Finally! It is now known that the circuit has the following properties:

R1 = 50k
R2 = 40k
Current = 100uA
V_OUT = 4v

now check the math using the very first formula:

VR2 = (R2/(R1+R2)) * Vin

Substitute the values calculated in the previous steps:

VR2 = (40k/(50k+40k)) * 9v
VR2 = 4v

These are the calculated values for the circuit.

Constructing and Testing the Circuit
Open the resistor kit and remove the piece of paper packaged with the resistors. This paper lists all of the values inside the kit and how many of each value are in the kit. Be sure to put the resistors inside a container so that they can't be spilled, and they can be accessed easily. Keep the paper as an inventory and reference aid. A sandwich bag works fine.

Here are the values calculated in the last section:
R1 = 50k
R2 = 40k

The kit does not contain these values, and this is where the real world conflicts with the math. The values of the resistors will need to be adjusted to fit what is in the kit. Find the closest resistor values to the calculations, which are 39k and 51k. Reuse the voltage divider formula using those values to see how close the circuit is to the desired properties:

VR2 = (R2/(R1+R2)) * Vin
where
VR2 = (39k/(39k + 51k)) * 9v
VR2 = 3.9v

That's pretty close and will work fine for this design.

Build the circuit and check all of the connections and wiring. See the tutorial here for a circuit construction technique that uses this circuit as an example.

Connect a 9v battery to the input side of the circuit and place a voltmeter on the output. Verify that the voltage is close to the expected value. It can vary as much as 10% due to the tolerances of the resistors. The sample circuit measured 4.15 volts out with 9.61 volts in, exactly as expected. Make sure to label the input, output, and grounds on the circuit board. A permanent marker is typically used to do this.

Now, calculate the ratio of the divider to use in the software. Use this formula:

Ratio = Vin / Vout

For the readings taken on the example circuit the ratio would be calculated as follows:

Ratio = 9.62 / 4.15
Ratio = 2.318

Connect the Vout to analog pin 0 of the Arduino and the ground to the ground on the Arduino board. See the picture below for reference.

Connect the input side of the circuit to a 9v battery and connect the USB cable from the Arduino to the computer and load the source code shown below. Make sure to change the ratio variable to the one calculated for your circuit.

Code:

/* Read voltage divider
* Reads the voltage divider to calculate a battery voltage
* This software has no warranty, real or implied and is free to distribute and modify
*/

int batMonPin = 0;    // input pin for the divider
int val = 0;       // variable for the A/D value
float pinVoltage = 0; // variable to hold the calculated voltage
float batteryVoltage = 0;
float ratio = 2.316;  // Change this to match the MEASURED ration of the circuit

void setup() {

Serial.begin(9600);      // open the serial port at 9600 baud
}

void loop() {
val = analogRead(batMonPin);    // read the voltage on the divider

pinVoltage = val * 0.00488;       //  Calculate the voltage on the A/D pin
//  A reading of 1 for the A/D = 0.0048mV
//  if we multiply the A/D reading by 0.00488 then
//  we get the voltage on the pin.

batteryVoltage = pinVoltage * ratio;    //  Use the ratio calculated for the voltage divider
//  to calculate the battery voltage
Serial.print("Voltage: ");
Serial.println(batteryVoltage);

delay(1000);                  //  Slow it down
}
The Arduino should start sending the battery voltage to the serial port at 9600 bps.

Final Notes

The measured battery voltage should be fairly close to the actual voltage but may be off by a few hundred mV. This circuit is intended to provide an extremely cheap way to monitor a battery's voltage and should not be used where a precise measurement is required. If there are motors attached to the battery then it can cause a fair amount of noise on the readings. These can be filtered with either additional electronics, software filtering, or both. Also note that if this noise exceeds 5v at the Arduino pin then it can damage the Arduino.

Replies to Tutorial: Cheap Battery Monitor Using Resistive Voltage Divider

Re: Cheap Battery Monitor Using Resistive Voltage Divider

I've just found this really useful while starting a battery monitor for my small bot - thanks!

Does anyone know where the 0.00488 number in the sketch comes from though please? Re: Cheap Battery Monitor Using Resistive Voltage Divider

The number 0.00488 is the volts per division for the analog conversion.

5V / 1024 divisions = 0.00488 volts per division 3. fcarlo Guest

Re: Cheap Battery Monitor Using Resistive Voltage Divider

Thanks! Great tutorial! First tutorial on this subject, which ever made sense to me. I'm building a go kart with three batteries and need to monitor all of them. This is a great solution!  Closed Tutorial