# How to measure battery voltage from a microcontroller

Today I had an interview, there they asked me, if given a microcontroller and Battery then how do you measure the voltage level. Write the steps from scratch with pseudo code.

How can this be done?

If they were after the pseudo code then that was a bad question; it all depends on the microcontroller. I probably would have given the candidate the microcontroller's datasheet and observe how he works his way through that 300 pages document.

Anyway, you connect the battery to an ADC input (assumptions: microcontroller has ADC on board and battery voltage is less than microcontroller supply voltage).

1. Set pin mode for that pin to ADC
4. Wait for end of conversion flag
• This is a great piece of help. Could you please suggest some web-links where I can study hardware as well as software parallely on microcontroller. Jul 4, 2012 at 15:14
• It's not a bad question. The interviewer was probably looking for him to ask questions about the hardware and suggest various approaches. Jul 4, 2012 at 15:15
• @Toby - If I were the interviewer and that were my purpose I wouldn't ask for the pseudo code. Jul 4, 2012 at 15:17
• @TobyJaffey: I think you are right Jul 4, 2012 at 15:18
• Select internal band gap reference when available ;o) If battery and supply voltage are one and the same, then you need to connect through a voltage divider. Jul 4, 2012 at 15:46

I think the catch here is that Microcontrollers need an extrenal reference for their A/D. The default reference is the power supply. You can use an external reference, and divide the battery voltage to something below that reference, but that puts a constant drain on the battery which might not be a good idea.

My answer would be: get a shunt-type reference for a voltage that is lower than the battery range of interest. Let's assume a ~5V battery, and a TL431 or the like as 2.5V reference. Now measure the TL431 voltage, using the battery as reference. Assume a 10-bit A/D (most common), that reads N. Now you know that

N * (Battery / 1023) = 2.5


hence

Battery = 2.5 * 1023 / N


If the current drawn by the reference is a problem I would switch its power from an output pin: no power drawn unless we want to do the measurement (which will be a very short period).

=========================================

Now some homework as a preparation for the next interview: You have a USB powered 5V gadet, that has a microcontroller and an LM35 temperature sensor. How would you determine the temperature? Hints: what does the LM35 output, and check the voltage range of a USB outlet. (There is an alternate answer: why the \$&^*^& don't you use a digital temperature sensor in the first place?).

• And then you ask if there are reasons for not doing it this way (battery as Vref)! Like the resolution isn't constant because the function is hyperbolic instead of linear. At 5 V one LSB = 10 mV, at 3.33 V one LSB = 4.3 mV. Jul 4, 2012 at 15:45
• Very smart indeed. Is a band gap reference different from your 'zener-type reference', or are we talking about the same sort of divice here? Didn't realize a reference would actually consume less power than an high impedance voltage divider. Jul 4, 2012 at 15:54
• That depends a lot on the context. I assumed a low-cost system, where a knowing the battery voltage is only interesting to maybe 5 or 10%. A high-accuracy reading would probably not use the uC A/D at all. Jul 4, 2012 at 15:55
• Yes that was my assumption too. More like a brown out circuit ... which of course doesn't require much pseudo code at all ... Jul 4, 2012 at 15:57
• Zener-type was the wrong wording, I meant shunt regulator (in contrast with series regulator). I'll edit my text. The reference likely consumes the same current, but it can be switched on and off at will. Note that the divider can in general not be high-impedance: uC A/D inputs are not high-impedance. Typically they require a source impedance in the 1..10k range. Jul 4, 2012 at 15:57