9
\$\begingroup\$

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?

\$\endgroup\$

2 Answers 2

14
\$\begingroup\$

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
  2. Select this ADC input if multiple ADC inputs
  3. Start ADC conversion
  4. Wait for end of conversion flag
  5. read ADC register
  6. calculate Vbat = Vcc x (ADC reading)/(2^ADC resolution)
\$\endgroup\$
11
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Jul 4, 2012 at 15:14
  • 1
    \$\begingroup\$ It's not a bad question. The interviewer was probably looking for him to ask questions about the hardware and suggest various approaches. \$\endgroup\$ Commented Jul 4, 2012 at 15:15
  • \$\begingroup\$ @Toby - If I were the interviewer and that were my purpose I wouldn't ask for the pseudo code. \$\endgroup\$
    – stevenvh
    Commented Jul 4, 2012 at 15:17
  • \$\begingroup\$ @TobyJaffey: I think you are right \$\endgroup\$ Commented Jul 4, 2012 at 15:18
  • \$\begingroup\$ 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. \$\endgroup\$
    – jippie
    Commented Jul 4, 2012 at 15:46
9
\$\begingroup\$

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?).

\$\endgroup\$
7
  • \$\begingroup\$ 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. \$\endgroup\$
    – stevenvh
    Commented Jul 4, 2012 at 15:45
  • 1
    \$\begingroup\$ 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. \$\endgroup\$
    – jippie
    Commented Jul 4, 2012 at 15:54
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ Commented Jul 4, 2012 at 15:55
  • \$\begingroup\$ Yes that was my assumption too. More like a brown out circuit ... which of course doesn't require much pseudo code at all ... \$\endgroup\$
    – jippie
    Commented Jul 4, 2012 at 15:57
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Jul 4, 2012 at 15:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.