ESP32 – Getting Battery charging level

Voltage Divider

Some of the ESP32 development boards provide a 3.7 Ion-Li battery charger what is an advantage when we want to get a device with the minimum number of components.

One of these boards is Wemos Lolin 32 (with battery charger) which costs about 7$ in eBay.

Wemos Lolin 32 with battery charger
Wemos Lolin 32

When a battery is plugged in and the board is connected to a power source via the USB connector, the battery starts being charged.

As the ESP32 board counts with several ADC pins, we can use one of them to check the voltage in between the two battery terminals. The only issue with this is that ADC pins expect voltages between 0 and 3.3 volts and our Ion-Li battery voltage range may reach 4.2 volts.

18650 Ion-Li Battery
18650 Ion-Li Battery

The solution to that is connecting a voltage divider to the battery, so we can divide the volts by 2 and the maximum value will be about 2.1 volts.

Voltage Divider
Voltage Divider Schema
Voltage Divider
Voltage Divider

Our voltage divider is built of two 47KΩ resistors. The total impedance between positive and negative terminals will be 94KΩ and that means a current of less than 5 50μA (microamperes, not milliamperes). (Thanks Jonathan)

Voltage Divider mounted on Lolin 32
Voltage Divider mounted on Lolin 32

With this, we can measure the voltage applied in GPIO34 (or any other ADC pins of our ESP32) and then, based on a conversion table, calculate the charge level of the battery.

First, we will get the value of ADC pin. This value may vary from 0 to 4096 depending on the voltage applied to it from 0V to 3.3V. So we can establish a constant to calculate the voltage applied to the pin based on its value. This constant, theoretically, will be 3300 / 4096 = 0.8056.

As we are applying a voltage divider and the voltage applied to the pin is half the voltage of the battery, our constant should be 0.8056 x 2 = 1.6113.

This means, for each unit in ADC pin we have 1.6113 mVolts applied to it.

For instance, if we read the value of the ADC pin and get 2,543, then the voltage applied to the pin should be 2,453 x 1.6113 = 3,952V = 3.95V

ADC pins are not that precise, so the value of our constant should be adjusted to a level we consider it is valid for our components. In my case, after doing some testings I have concluded that the best value for the conversion factor is 1.7.

As I mentioned before, calculating the charge level is a direct translation from the voltage we obtained to a charge level by using a table.

All the code to make these calculations is contained in a library I have created for that purpose. You can find it in Github at Pangodream_18650CL

All you have to do is downloading the .zip file and add it to Arduino IDE.

There is an example of using the library:


#include <Pangodream_18650_CL.h>

//#define ADC_PIN 34
//#define CONV_FACTOR 1.7
//#define READS 20

Pangodream_18650_CL BL;
 * If you need to change default values you can use it as
 * Pangodream_18650_CL BL(ADC_PIN, CONV_FACTOR, READS);

void setup() {

void loop() {
  Serial.print("Value from pin: ");
  Serial.print("Average value from pin: ");
  Serial.print("Volts: ");
  Serial.print("Charge level: ");

And if everything works, it should display something like this on your serial terminal:

Reading battery charge level
Reading battery charge level


16 thoughts on “ESP32 – Getting Battery charging level”

  1. Hi

    Useful article, I just happened to spot that your current consumption calculation for the voltage divider is out by a factor of ten – you should say under 50 microamps, not under 5.
    (1V / 1M = 1 microamp, so ~ 100k will be 10 times that.

    1. Thanks a lot Jonathan!!! You are right!!

      I’ve already modified the post.


  2. Nice post. The higher the resistance, the longer the battery last: do you know if trying with a much higher resistor values (let’s say two 470K resistors) will still make reliable measurements? I’m looking for ESP32 ADC input impedance value, but it seems that this information is not available.

    1. Thanks Gabriele!!! 🙂

      I guess it is not a good idea to increase the resistors value so much. I remember measuring a lot of noise in measurements even when using 47K resistors.

      Please, if you try and achieve good results let me know.


  3. Hi, I was wondering if the charger of that board stops charging when the battery reaches it maximum capacity.
    Thank you

  4. Hi, I got this working. But now I want to see the batt value on my phone or computer when I connect the bluetooth device.

    Any idea?


  5. Newb here. Thanks for putting this piece together as well as the library. Can you explain to me how your battery, which is clearly labeled as 3.7v, might reach as high as 4.2v as you suggest at the top of your article?


  6. Hi ,
    Great job, Thank you.
    Only a sugestion : could you introduce a custom factor that the user can use to set is own conversion factor ?
    This can be usefull because i think that your library will be used when the power supply is diconnected , to send by mqtt a battery level.
    Your calculation seems to be done by assuming the fact the power supply is allways connected.
    I am wrong?

  7. Hi, I got three esp32 , but every esp32 must set diffrent value (conversion factor). Is it my problem ? or esp32 is NOT good for DAC?

Leave a Reply

Your email address will not be published. Required fields are marked *