Microcontroller Interfacing Part 15

Measuring Battery Voltage

  

Goals

A common application for the A/D section of portable microcontroller based systems is to measure the battery voltage. Often the user will want to get some idea of how long it will be before the batteries need to be changed or recharged.

Part 13 covered the basics of measuring voltages and included a simple Arduino program and an example of measuring the voltage of a battery.  If the battery in question is also powering the microcontroller there are a few other things that must be considered. This section covers these additional factors.  

Systems with a voltage regulator 

Figure 15-1 shows a battery supplying power to a microcontroller. Two options are shown, one with the battery going to a regulator, and one where the battery powers the micro directly. We will cover the case with a voltage regulator first.  There are two types of regulators, linear and switching.  See the side bar on voltage regulators.  

The first example in this section has a 9V battery and a regulator that drops this down to 5V (Vcc) to supply the microcontroller and other circuitry.  The first consideration is that the battery voltage (Vbat) we want to measure is greater than the supply voltage and connecting it directly to a pin (ADIN) on the micro is likely to destroy the chip. 

 

The way around this is to use a voltage divider to bring the 9V down to a level where it can be measured.  Part 2 discusses voltage dividers in some detail.  In this example R1 and R2 form a voltage divider to bring the 9V down to an acceptable level.  For now assume that the lower end of R2 goes to ground instead of pin P0.

The first step is to select resistors for the divider.  The equation for a voltage divider is:

Vdiv = Vbat* R2/(R1+ R2)

We can rewrite this to come up with a value we want to divide the battery voltage by:

Vdiv / Vbat =  R2/(R1+ R2)

V must never go higher than the supply voltage, 5V in our example.  A voltage divider offers no protection if the battery voltage is a bit high, so for this calculation we will bump up Vbat as a safety margin.  Let’s use 10V instead of 9V, so

Vdiv / Vbat = 5V/10V = .5

  

Measuring Battery Voltage

Figure 15-1

  

Plugging this back in to the equation gives  

.5 = R2/(R1+ R2)  

The math turns out really simple when the divider ratio is 1/2.  R1 and R2 are the same value. 

Current flows from the battery through R1 and R2, so we want to pick high values for the resistors. This will minimize the amount of power used. We might want to pick 47K for R1, which will mean that R2 will also be 47K.

The divisor factor may not work out so cleanly with different batteries or operating voltage.  Let’s assume that we were running the micro at 3.3V volts with a 9V battery.  Using 10V as the maximum battery voltage we get  

Vdiv / Vbat = 3.3V/10V = .33  

.33 = R2/(R1+ R2)

Solving for a divisor of .33 and picking R1 = 100K, we get a value for R2 of 49.25K.  You are not going to find a 49.25K, at least not one you can afford.  You will need to select a standard value resistor.   First, use 1% resistors. Regular 5% resistors will not give you very accurate readings. The nearest standard 1% resistors are 48.7K and 49.9K ohms. We want the lower value because a higher value resistor would deliver more than 3.3V with a 10V battery.  

The voltage divider with 100K and 48.7K resistors will give a divider ratio of 3.275.  You might try other combinations of standard resistor values to see if you can get closer to the desired divisor ratio, but it is not usually that important.   Whatever divisor ratio you end up using, you will need to factor that in when calculating the actual battery voltage. For example, if the A/D converter reads 4 volts and the divisor was .5, we have 4/.5 or 8V.  

So far we had assumed for simplicity that the bottom of R2 was grounded. We would not want to do that in practice because  current would also be flowing through the resistors all the time. This will reduce battery life. To prevent this we connect the resistor to a digital I/O pin, P0 in Figure 15-1.  

Most of the time this pin is set to be an input.  This presents a high impedance at the bottom end of R2 and very little current will flow. When we want to measure the battery voltage we first change the pin to an output set low. This will be very close to ground and we can then take our measurement. After the reading is complete, the pin is changed back to an input.

There will be a bit of voltage drop from the transistor to ground inside the micro. This will cause some loss of accuracy of the voltage reading. This will probably not be too important since battery life estimates are just that, estimates.  If you need higher accuracy you can use a FET transistor to ground and turn that on and off. Pick a FET with a low Rds-on specification.  

A different version of the battery and regulator is when you use a boost switching regulator. You could for example use a single 1.5V battery with a regulator that bumps the battery voltage up to what the micro can work at, say 3.3V.  In this case you could just connect the battery to the A/D input pin. One word of caution though.  Switching regulators often generate a fair amount of noise.  To get accurate readings you might need a bit of filtering to battery voltage signal to the micro. Part 10 discusses simple RC filtering.

Direct Battery power

Often it is desirable to power the micro directly to the battery with no regulator. This reduces the part cost and does not waste power in the regulator. Of course you need a battery and micro combination where the micro operates properly in the range of voltages the battery will present during its useful life.  

We don’t have to worry about putting too much voltage directly to the A/D pin since it will never be higher than the supply voltage. Can we just connect the battery to the A/D pin without the voltage divider?  

Unfortunately this will not work. We will always read that the battery is at full charge! The reason for this is that the A/D converter is referencing the  input voltage with the micro’s supply voltage. They will always be the same regardless of the actual battery voltage.  We need to use something else for an A/D reference. 

Microprocessors will often give you an alternative reference. They often have 1.25 or other internal voltage reference that can be selected. Otherwise you can add an external reference source. Either way, the reference will be less than the supply voltage, and you will need to use a voltage divider to reduce the battery voltage to be no more than the reference voltage.  

Summary  

Keeping tabs on the battery voltage is pretty easy, but it requires a few special considerations not needed with other A/D applications.

  

Voltage Regulators

There are two main types of regulators, linear and switching.  Linear regulators are simpler to implement. The 78xx series are popular examples. You can convert 9V to 5V with a 7805 and a couple of capacitors. The down side of linear regulators is that they are not very efficient. They waste a lot of power as heat.  

Consider a linear regulator that converts our 9V battery to 3.3V, a common voltage for newer microprocessors.  Suppose the circuit draws 50 ma (.050A).  The power consumed by the circuit is

.050A X 9V = .45 Watts.

The power consumed by the main circuit is

.050A X 3.3V = .165W

Most of the power (.285W) is consumed by the regulator and not the main part of the circuit!  

Switching regulators are more efficient, usually 80% or more.  They are also much more complex to implement.  They use a regulator IC, one or more inductors and usually several capacitors and resistors, and maybe some diodes and a transistor. They also require care in component selection and circuit board layout.  

Switching regulators come in buck and boost types. A buck regulator provides a regulated voltage less than the input voltage. Boost regulators produce a voltage higher than the input voltage. Buck-boost regulators can operate with the input voltage in a range that is below or above the output voltage.  

Because of the demand for long battery life and power efficiency overall, switching regulators are found in most new designs of commercial electronics.

  

Gotcha List

1. Don’t allow the voltage to the A/D pin be greater than the reference voltage or the micro supply voltage (whichever is less)

2. Use 1% resistors in the divider for accuracy

  

I sell on Tindie

© 2009 - 2022 Gary C. Sutcliffe

  

Created with the QTH.com SiteBuilder.