Measuring the battery with a blue pill

Post here first, or if you can't find a relevant section!
Post Reply
mjepronk
Posts: 3
Joined: Mon Jul 27, 2020 8:09 pm

Measuring the battery with a blue pill

Post by mjepronk »

Hi all,
I would like to measure the battery voltage of my data logger. I did some research and found that I need a voltage divider. I also learned that if you want to minimize the battery drain you need to use higher rated resistors. I found the blogpost https://jeelabs.org/2013/05/16/measurin ... aining-it/, which also adds a 0.1μF capacitor to the circuit.

So I wired up a voltage divider with two 1MΩ resistors, and a 104 capacitor to my battery and PA1 of my Blue Pill. I use the following code:

Code: Select all

analogReadResolution(ADC_RESOLUTION);
pinMode(PA1, INPUT_ANALOG);
for (byte i = 0; i < 3; ++i) {
    x = analogRead(PA1);
    Serial1.print("ADC: ");
    Serial1.println(x);
}
And I get this output:

Code: Select all

ADC: 173
ADC: 49
ADC: 14
--
ADC: 177
ADC: 51
ADC: 13
--
ADC: 181
ADC: 52
ADC: 15
My battery voltage is 4.04 V at the moment, and with the voltage divider (which divides voltage in half) I would expect ADC values around 2390. I checked the voltage output of the voltage divider and it shows 1.92 V (which implies the voltage radio actually is around 2.1, but OK...).
Can anybody give some tips on what I'm doing wrong? Are there better options? Other resistors? Other capacitor?
Thanks in advance!

(Note: I use STM32 Cores v1.9.0).
GonzoG
Posts: 403
Joined: Wed Jan 15, 2020 11:30 am
Answers: 27
Location: Prudnik, Poland

Re: Measuring the battery with a blue pill

Post by GonzoG »

You must have wired something wrong.
it works without any problems for me.

Check reading using 3.3V from board.

Also, if you use too high value resistors in voltage divider you'll get inaccurate reading (lower).
mjepronk
Posts: 3
Joined: Mon Jul 27, 2020 8:09 pm

Re: Measuring the battery with a blue pill

Post by mjepronk »

@GonzoG: Thanks... Would 1MΩ be too high?
GonzoG
Posts: 403
Joined: Wed Jan 15, 2020 11:30 am
Answers: 27
Location: Prudnik, Poland

Re: Measuring the battery with a blue pill

Post by GonzoG »

In datasheet they say that recommended max. input resistor is 50k but with 100k you should still get good reading.
I've tested this with 3V lithium coin battery (3.157 on my mulitimeter V) and got this:

Code: Select all

1M: 
1M: 
1995 = 3.294V
100k: 
1982 = 3.168V
47k: 
1971 = 3.157V
no divider: 
3941 = 3.156V
mjepronk
Posts: 3
Joined: Mon Jul 27, 2020 8:09 pm

Re: Measuring the battery with a blue pill

Post by mjepronk »

@GonzoG
I've soldered the headers on another Black Pill board I had lying around and now it actually works. So, I don't know what's going on, but there was definitely something wrong with my first board. Thanks for your help!

BTW, I've noticed the readings are actually HIGHER with larger resistors...
paynterf
Posts: 3
Joined: Sat Aug 01, 2020 3:50 pm

Re: Measuring the battery with a blue pill

Post by paynterf »

Remember too that the complete circuit also includes the input impedance (resistance and capacitance) of the analog input pin. With 1M resistors, the analog input impedance might be significant. You are probably better off with something like 100K resistors, which are pretty much guaranteed to be much smaller than the effective resistance of the analog input (meaning that the analog input's resistance value won't affect the division ratio). Also, If you have a 0.01 uF cap to ground at the intersection of your two 1M resistors, you have formed a very low pass filter with a time constant of about 0.01 seconds - did you mean to do that? With 100K resistors, the time constant with the same capacitor is now 0.001S (1 mSec) a much more comfortable number.

Just my $0.02 ;-)

Frank
Post Reply

Return to “General discussion”