Measure LiPol battery level

olysachok
Posts: 1
Joined: Thu Jan 05, 2017 6:50 pm

Measure LiPol battery level

Postby olysachok » Thu Jan 05, 2017 7:09 pm

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.

onehorse
Posts: 70
Joined: Mon Feb 15, 2016 1:35 am

Re: Measure LiPol battery level

Postby onehorse » 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.

User avatar
rudi ;-)
Posts: 1727
Joined: Fri Nov 13, 2015 3:25 pm

Re: Measure LiPol battery level

Postby rudi ;-) » Fri Jan 06, 2017 4:08 am

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 ;-)
-------------------------------------
love it, change it or leave it.
-------------------------------------
問候飛出去的朋友遍全球魯迪

quangminh.le0701
Posts: 6
Joined: Mon Jul 01, 2019 2:55 pm

Re: Measure LiPol battery level

Postby quangminh.le0701 » Tue Jul 02, 2019 7:36 am

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?

wyattisawesome
Posts: 2
Joined: Sun Jul 07, 2019 4:16 am

Re: Measure LiPol battery level

Postby wyattisawesome » Sun Jul 07, 2019 4:45 am

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.

Tranzmetro
Posts: 9
Joined: Fri Jan 05, 2018 1:38 am

Re: Measure LiPol battery level

Postby Tranzmetro » Mon Aug 05, 2019 10:21 pm

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.

Zeni241
Posts: 86
Joined: Tue Nov 20, 2018 4:28 am

Re: Measure LiPol battery level

Postby Zeni241 » Fri Sep 20, 2019 10:57 am

Hi
@wyattisawesome which fuel gauge chip/module can be used with ESP-IDF? Have you successfully used any fuel gauge with ESP32?

Who is online

Users browsing this forum: No registered users and 21 guests