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: 33
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: 465
Joined: Fri Nov 13, 2015 3:25 pm
Location: germany
Contact:

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.
-------------------------------------
問候飛出去的朋友遍全球魯迪

Who is online

Users browsing this forum: No registered users and 2 guests