Measuring the battery with a blue pill
Posted: Mon Jul 27, 2020 8:20 pm
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:
And I get this output:
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).
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);
}
Code: Select all
ADC: 173
ADC: 49
ADC: 14
--
ADC: 177
ADC: 51
ADC: 13
--
ADC: 181
ADC: 52
ADC: 15
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).