Friday 13 March 2015

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?



Answer




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)



No comments:

Post a Comment

arduino - Can I use TI's cc2541 BLE as micro controller to perform operations/ processing instead of ATmega328P AU to save cost?

I am using arduino pro mini (which contains Atmega328p AU ) along with cc2541(HM-10) to process and transfer data over BLE to smartphone. I...