Page 1 of 1

Measuring battery percentage

Posted: Fri Mar 22, 2019 3:13 pm
by alexmerchant
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)

Re: Measuring battery percentage

Posted: Fri Apr 26, 2019 2:02 pm
by nekomatic
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.