# PIC voltage measurement

I am running a battery powered device, and would like my microcontroller (PIC) to report on the supply voltage.

This is so I can determine how many batteries is used in the supply, and also the state of charge of the battery pack.

Supply voltage ranges from 4xD (either 1.2V nominal NiMH or 1.5V Alkalines making about 6V) to 12xAA (1.2V NiMH for 14.4V or 1.5V Alkaline for 18V).

My PIC is running off a regulated 5V supply.

I plan to use one of the ADC ports to measure the voltage, and therefore believe I should map the 18V f.s.d. to the range 0-5V, so roughly I'd need to divide the input voltage by 3.

I've heard of using a resistor voltage divider, but know that it can be wasteful in terms of energy consumed.

Wondering if there is a better way to achieve this voltage division without too much energy loss/wastage?

Thanks.

-

you want a voltage divider. if you care about power dissipation, there are a couple of things you can do:

• use a fairly high impedance voltage divider, with a capacitor on the output terminal, and buffer it with a low-power op-amp. Make sure you use an RC filter between op-amp and ADC, values of this RC filter are typically around 50-200 ohms, 1000pf. This serves to keep voltage stable when the ADC is switching between channels and charge is transferred between the ADC's internal capacitor and the outside pin. An op-amp alone cannot do this. If you don't buffer the high-impedance voltage divider, you will get errors due to ADC leakage current and charge transfer.

• switch the voltage divider, e.g. connect/disconnect it to the supply voltage, so you can do this only occasionally when you want. A PFET would work, just be careful with how you drive it.

-
This sounds great, I would just like to add that you need to load (pull current from) a battery to get a realistic measurement of the remaining battery life. You are much better off with a higher current divider with an off switch. –  Kortuk Dec 2 '09 at 14:39
I just posted a similar response, and voted to delete it. Apparently I had an old version of the page cached. I voted this up, it's the way to go. –  Lou Dec 2 '09 at 19:29
+1 for switching the voltage divider. A very useful trick. –  Clint Lawrence Dec 2 '09 at 20:15
@Kortuk: you have a very good point, but I'd decouple the voltage divider from the battery load. It's really easy to drive an N-channel FET directly from a microcontroller (well... there should be a small resistor between micro output + FET gate, I usually use 10-100 ohms) and have a resistor from FET drain to power supply. Much easier than trying to combine these two functions and finagle a switchable voltage divider. Besides, there are lots of times when you want to measure a battery voltage at no-load or near no load. Actually if I had to pick one or the other, I'd measure it at no-load. –  Jason S Dec 3 '09 at 4:49