Measuring battery percentage

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
Post Reply
alexmerchant
Posts: 3
Joined: Fri Oct 19, 2018 11:46 am

Measuring battery percentage

Post by alexmerchant » Fri Mar 22, 2019 3:13 pm

Hi everyone!
I am currently working with a Pycom device, Sipy more precisely and am trying to read some battery level and display its percentage. Concerning the cabling, the black wire is linked to the ground and the red one (which usually corresponds to the supply I gues) is connected to the pin I'd like to measure the values on. I am using a 12 bits ADC to read the voltage then I use some mathematics to convert it into percentage. However, the results I get don't seem right which is why I am wondering if my code is correct. Does somebody have any clue ? You can find below the code programmed, which is actually very simple. I apologize in advance for my poor knowledge on electronics. I put below the Python code I wrote, which is very simple.
Thanks a lot for your help

def battery_level():
adc=machine.ADC()
apin=adc.channel(pin='P20')
b=(apin()/4095)*100
return(b)

nekomatic
Posts: 37
Joined: Thu May 08, 2014 9:31 pm

Re: Measuring battery percentage

Post by nekomatic » Fri Apr 26, 2019 2:02 pm

Are you trying to measure the SiPy's own supply voltage, or the voltage of a separate battery? What voltage range do you need to measure?

I'm not very familiar with the SiPy / ESP32 but based on the ESP32 MicroPython documentation, the full-scale range of the ADC inputs is about 0 - 1 V unless you use ADC.atten to scale that down - are you doing that?

Your next issue is that the relationship between battery voltage and state of charge is not simple, and definitely not as simple as (reading / 4095) * 100, but you can move on to that once you've got the basic voltage measurement working...

Edit: I see you've asked this question on the Pycom site and got some answers there.

Post Reply