Page 1 of 1

Measure LiPol battery level

Posted: Thu Jan 05, 2017 7:09 pm
by olysachok
I bought a ESP32 Thing from SparkFun. It is powered by LiPol battery (3.7V 2500 mAh) and can't figure out how can I measure the battery level using voltage divider. Can anybody help me? Thanks.

Re: Measure LiPol battery level

Posted: Fri Jan 06, 2017 2:33 am
by onehorse
I use a 27 kOhm + 100 kOhm voltage divider to bring the 4.2 V maximum of a single cell LiPo battery down to 3.3 V, which is the reference voltage (AFAIK) of the ESP32 board I am using. I have the voltage divider connected to pin 34, and I read the pin as follows:

Code: Select all

    float VBAT = (127.0f/100.0f) * 3.30f * float(analogRead(34)) / 4096.0f;  // LiPo battery
    Serial.print("Battery Voltage = "); Serial.print(VBAT, 2); Serial.println(" V");   

The ADC is 12-bit which is why the 3.3/4096 (ref voltage/max counts) conversion factor is in there. When the analog read returns 0, the battery voltage is 0, and when it returns 4095, the battery voltage is 4.2 V. The response of the ESP32 ADCs is apparently non-linear so you will have to calibrate the response and correct for maximum accuracy.

Re: Measure LiPol battery level

Posted: Fri Jan 06, 2017 4:08 am
by rudi ;-)
onehorse wrote:

Code: Select all

    float VBAT = (127.0f/100.0f) * 3.30f * float(analogRead(34)) / 4096.0f;  // LiPo battery
    Serial.print("Battery Voltage = "); Serial.print(VBAT, 2); Serial.println(" V");   

The ADC is 12-bit which is why the 3.3/4096 (ref voltage/max counts) conversion factor is in there. When the analog read returns 0, the battery voltage is 0, and when it returns 4095, the battery voltage is 4.2 V. The response of the ESP32 ADCs is apparently non-linear so you will have to calibrate the response and correct for maximum accuracy.

hi

wounder me to use better

ADC 12 Bit = 0b 1111 1111 1111 = 4095

3.3 / 4095 = 8,0586080586080586080586080586081e-4
if return 4095 (max RAW) :
4095 * 8,0586080586080586080586080586081e-4 = 3.3

if return 0:
0 * 8,0586080586080586080586080586081e-4 = 0

if return 2:
2 * 8,0586080586080586080586080586081e-4 = 0,00161172161172161172161172161172

---

3.3 / 4096 = 0,0008056640625
if return 4095 (max RAW):
4095 * 0,0008056640625 = 3,2991943359375 ( missing one part )

if return 0:
0 * 0,0008056640625 = 0

if return 2:
2 * 0,0008056640625 = 0,001611328125


btw:

4.2 Cell
4.2 / 4096 = 0,001025390625

Max RAW Return 4095 :
4095 * 0,001025390625 = 4,198974609375 ( missing one part )


4.2 Cell
4.2 / 4095 = 0,00102564102564102564102564102564

Max RAW Return 4095 :
4095 * 0,001025390625 = 4,2

Code: Select all

    
    float VBAT = (127.0f/100.0f) * 3.30f * float(analogRead(34)) / 4095.0f;  // LiPo battery
    Serial.print("Battery Voltage = "); Serial.print(VBAT, 2); Serial.println(" V");   

best wishes
rudi ;-)

Re: Measure LiPol battery level

Posted: Tue Jul 02, 2019 7:36 am
by quangminh.le0701
onehorse wrote:
Fri Jan 06, 2017 2:33 am
I use a 27 kOhm + 100 kOhm voltage divider to bring the 4.2 V maximum of a single cell LiPo battery down to 3.3 V, which is the reference voltage (AFAIK) of the ESP32 board I am using. I have the voltage divider connected to pin 34, and I read the pin as follows:

Code: Select all

    float VBAT = (127.0f/100.0f) * 3.30f * float(analogRead(34)) / 4096.0f;  // LiPo battery
    Serial.print("Battery Voltage = "); Serial.print(VBAT, 2); Serial.println(" V");  

The ADC is 12-bit which is why the 3.3/4096 (ref voltage/max counts) conversion factor is in there. When the analog read returns 0, the battery voltage is 0, and when it returns 4095, the battery voltage is 4.2 V. The response of the ESP32 ADCs is apparently non-linear so you will have to calibrate the response and correct for maximum accuracy.
is it internal adc reference voltage?

Re: Measure LiPol battery level

Posted: Sun Jul 07, 2019 4:45 am
by wyattisawesome
You really wont be able to measure the voltage over time on a 0-100% basis similar to your iphone if this is what you are looking to do. The drain of lipo batteries is non-linear and requires a separate circuit usually called a fuel gauge to read its drain output like such. The closest you'll be able to get with using the internal ADC is knowing when you have enough voltage and perhaps right before the battery is about to die, ie. some kind of low batt indication (but from my experience and knowledge it will die quickly after depending on the overall power draw but usually in under 10-15mins for low level indication). A board like the TP4056 can help handle this basic voltage sensing as well as the ability to recharge the lipo.

Re: Measure LiPol battery level

Posted: Mon Aug 05, 2019 10:21 pm
by Tranzmetro
Your voltage divider calculation is wrong. If you have 2 resistors as a voltage divider, call the top resister R1 (the one that is connected to the voltage to be measured) and call the bottom resister R2. Use R1/(R1+R2). This will give you the correct divisor, or 1/divisor to give you the multiplier.

Re: Measure LiPol battery level

Posted: Fri Sep 20, 2019 10:57 am
by Zeni241
Hi
@wyattisawesome which fuel gauge chip/module can be used with ESP-IDF? Have you successfully used any fuel gauge with ESP32?