9

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?

skrrgwasme
  • 840
  • 9
  • 33

2 Answers2

14

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)
stevenvh
  • 145,832
  • 21
  • 457
  • 668
  • 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. – Rasmi Ranjan Nayak Jul 04 '12 at 15:14
  • 1
    It's not a bad question. The interviewer was probably looking for him to ask questions about the hardware and suggest various approaches. – Toby Jaffey Jul 04 '12 at 15:15
  • @Toby - If I were the interviewer and that were my purpose I wouldn't ask for the pseudo code. – stevenvh Jul 04 '12 at 15:17
  • @TobyJaffey: I think you are right – Rasmi Ranjan Nayak Jul 04 '12 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. – jippie Jul 04 '12 at 15:46
  • Interviewer: "Tell me a bit more about band gap references." :-) – stevenvh Jul 04 '12 at 15:51
  • +1 for the "working through 300 page datasheet part". I'd ask for a laptop and an electronic version of the document ;o) – jippie Jul 04 '12 at 15:56
  • "battery voltage is less than the microcontroller supply voltage"... I think that's exactly the opposite of the assumption you should be making, so just use a voltage divider. – Ben Voigt Jul 04 '12 at 16:06
  • @Ben - If the interviewer doesn't specify that I can make any assumption I like. It's a perfectly reasonable assumption for NiMH, or Alkaline cells and a 3.3 V or 5 V microcontroller. If he wants to hear about the voltage divider he should respond "Oh, by the way, it's a 12 V car battery". – stevenvh Jul 04 '12 at 16:24
  • @stevenvh: I think the more likely reply is: "Your microcontroller is forever held in reset because the battery voltage isn't sufficient to turn it on; you failed to choose an appropriate microcontroller for use in this system." – Ben Voigt Jul 04 '12 at 16:44
  • I am a former CIO and IT manager before that. I would have expected not only some questions clarifying what I wanted and what the parameters were, but also some pseudo code. I would not expect perfect pseudo code but rather I want to see how the person being interviewed approached the problem. – rdivilbiss Jul 04 '12 at 16:45
9

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

Wouter van Ooijen
  • 48,572
  • 1
  • 63
  • 136
  • 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. – stevenvh Jul 04 '12 at 15:45
  • 1
    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. – jippie Jul 04 '12 at 15:54
  • 1
    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. – Wouter van Ooijen Jul 04 '12 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 ... – jippie Jul 04 '12 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. – Wouter van Ooijen Jul 04 '12 at 15:57
  • @stevenvh: But you usually care about resolution more as the battery runs down anyway. This answer is brilliant! – Ben Voigt Jul 04 '12 at 16:08
  • @Ben - Agreed, but for your report you'll have to calculate the absolute error (+ and -) again for every result. – stevenvh Jul 04 '12 at 16:30