Measuring battery percentage

  • This post is deleted!

  • @brossingo Well, if you're not too versed into electronics, you should probably read up a bit before connecting things. As @Martinnn pointed out, connecting things without paying to much attention can lead to frying up hardware.

    Also note that Lithium batteries can be very dangerous. If you handle them incorrectly they can catch fire and/or explode.

    Even if you didn't fry the ESP32, the voltage of a LiPo battery exceeds the maximum voltage with attenuation, and completely blows out the maximum voltage without, which explains why you would get the max reading all the time. You need to use a voltage divider and correctly set attenuation.

    Note that there is already a voltage divider on the expansion board, you should probably use that. There's been ample discussion on the topic in the forum.

  • @brossingo Wow... this is funny. You are connecting the battery directly to a port pin?! A fully charged Li ion cell has typically 4.2 V which together with its low impedance should fry the port input immediately (overcurrent in the input protection). It seems though that yours is still alive... the ESP32 is full of surprises!

  • @jcaron

    I said this because with the Li-ion battery, the measured values are always the same (which is 4.2V), which is the highest, even if I measure it after not having recharged for a long time. On the other hand, I was measuring the level of a battery that, I guess, is a alkaline battery and the values at least tend to fluctuate. This might have something to do with the kind of battery I am working with as you say later.

    • I am measuring a Li-ion 3.7V 1500 mAh 5.6 Wh battery.
    • It is quite hard to join a picture but the cabling is really minimalistic. I only have two cables, one of them connected to the ground and the other to the pin where I'll measure the values, which is P20 in my case.
    • I am not using a voltage divider. As I said, my knowledge on electronics are really bad but I'd be happy to know how useful it'd be to use one and especially why I should use one though.
    • As I said, I'm using a 12 bits ADC, so with values between 0-4095. Also, according to the documentation about the ADC of Pycom, the ADC pin iput range is 0-1.1V. It can be increased up to 3.3V using the highest attenuation of 11 dB but since I'm not using any, I figure I didn't change the range.
      I don't know if these answers are the ones you expected to your questions but I can provide you with further information if needed. I'll also give a look to the links I posted and have never seen before, so thannks for attaching them.

  • @brossingo I use the following code on GPY to get the battery VOLTAGE from a LiPo battery connected to the Expansion Board 3, as % value is not really much use with LiPo batteries..

    Wouldn't be hard to convert the voltage to a percentage.

    # Battery Voltage ADC
    battadc = ADC()
    bat_pin ='P16', attn=ADC.ATTN_11DB)
    batmult = 1.64
    def getBatt():
        pycom.rgbled(0x0A0F00)                        # Mid Orange
        bat_vdc = round((bat_pin()/1000) * (batmult), 2)
        if bat_vdc <= 3.40:
            battIS = 1
        else: battIS = 0
        return battIS, bat_vdc

    I find the voltage returned isn't particularly accurate - especially as the battery is charging - due to a dodgy ADC on the GPY, but is useful as a general guide. The external ADC you use should be better.

    Hope this helps

  • @brossingo "the results I get don't seem right"... What are you expecting, what are you getting?


    • What battery are you measuring?
    • Can you provide your precise cabling (diagram and/or picture)?
    • Did you use an appropriate voltage divider? The ADC only supports voltages up to about 3.5V (if correctly configured, otherwise it's even lower), so if you're measuring a LiPo battery voltage you can't do it directly.
    • Did you correctly set the range of the ADC? The default is 0-1V.

    If you are measuring the battery voltage, you most definitely won't get a charge % directly from that. This has been discussed repeatedly, and again quite recently.

    See and among many, many others.

Log in to reply

Pycom on Twitter