How to monitor the battery level using microcontroller?

Thread Starter

goutham1995

Joined Feb 18, 2018
104
I want to know if I can use a simple voltage divider and feed it as input to the ADC pins of my microcontroller STM32L476RG in order to measure battery voltage level. Can I use the formula for voltage divider in such a case i.e. will the input impedance of the ADC pin of my microcontroller not affect the voltage level?
 

AlbertHall

Joined Jun 4, 2014
12,345
I don't know that particular uC but in general the answer is yes. The ADC can measure the battery voltage via a resistive divider. The ADC will have a pretty high input resistance - check the data sheet for this uC.
 

Thread Starter

goutham1995

Joined Feb 18, 2018
104
I don't know that particular uC but in general the answer is yes. The ADC can measure the battery voltage via a resistive divider. The ADC will have a pretty high input resistance - check the data sheet for this uC.
Hi @AlbertHall , according to the datasheet ( see the attachment), it says the external input impedance of ADC is 50 k ohms. Is that okay?
 

Attachments

AlbertHall

Joined Jun 4, 2014
12,345
I would take that to mean that the output impedance of the potential divider (the resistance if the two resistors were connected in parallel) should be no more than 50k.
 

Picbuster

Joined Dec 2, 2013
1,047
I would take that to mean that the output impedance of the potential divider (the resistance if the two resistors were connected in parallel) should be no more than 50k.
Hi AlbertHall,
To overcome that the input impedance is an issue measuring slow changing voltages you can do the following:
make a divider (assume input 12 volt DC and adc ref is 2.5V)
Take one 1M in serie with 200K and place 100nF over the 200K. This will act as your new voltage source.
Clamp to Vdd when needed.
Producing 2V when 12V is applied and correct this in software.
Disadvantage you have to wait ( after start-up) a number of mS until 100nF is charged but that is no problem for battery checking mechanism.
Advantage your power consumption is less than batteries self discharge.
I use that mechanism always.

Picbuster
 

BobaMosfet

Joined Jul 1, 2009
2,110
I want to know if I can use a simple voltage divider and feed it as input to the ADC pins of my microcontroller STM32L476RG in order to measure battery voltage level. Can I use the formula for voltage divider in such a case i.e. will the input impedance of the ADC pin of my microcontroller not affect the voltage level?
Having done this, I think some points are in mind you should be thinking about.

1. Your battery fully charged voltage may be higher than your digital logic. For example 9VDC. And your circuit is 5VDC. Which means you have to scale your battery voltage into the 5VDC range of your MCU, because that's what the ADC is expecting its input to fall within. A voltage divider is an excellent way to do this.
2. You should consider using an Opto-Isolator (4N35 is what I used) as a means to both control when you want to check the voltage, and it isolates you away from the battery in a safe way.

I use one pin on the MCU to fire the LED in the opto-isolator, which then causes the NPN transistor side of the opto-isolator to conduct voltage from the battery through my appropriately valued voltage divider. I've already turned the ADC on in preparation, it captures the voltage, I read the value, then I turn the opto-isolator off, and then the ADC. I do a calculation on the value from the ADC to convert it from an ADC value back into an actual voltage value and I know where my battery is at.

Just remember the .7VDC drop through the transistor in the opto-isolator, and add that back into the final voltage result.
 
Top