Battery Charge Level / Gauge

Is there way to get the % of charge left in the battery?

No there’s no easy way to do that, you can use the ADC, but you need to lower the voltage to 0-VREF (~3.3v), this might be useful:
https://vivonomicon.com/2019/10/15/reading-battery-voltage-with-the-stm32s-adc/

This is also a good read.

Hardware side:

https://docs.openmv.io/library/pyb.ADC.html


Software side:

If you can get the ADC vref, you can get the base voltage via the voltage divider (100/100) above by using the following formula will give you a decently accurate

(adc reading of battery/4095)*2(for the voltage divider) * 3.3(the 0-3.3v range) * ADC Reference Voltage(varies, but 1100 mv means use 1.1 sicne we are using volts; not millivolts))


From there, if you are using a random battery, you need to graph & monitor the discharge over and over. 4.2v is the “fully charged” voltage; and 3.2v is “fully discharged”. However, it’s not a linear equation since the battery will sit at 3.8v-3.7 for a majority of it’s charge. the amount of current discharged impacts this as well. after its 3.7v it’ll crash to dead very quickly.

Be aware, you aren’t calculating the actual percentage, you are predicting how much is left via a mostly educated guess. for comparison, just because a cell phone says it has 50% battery left doesn’t mean it’s actually half left. phones do the same thing and adjust based on the battery’s recent usage history based on some fairly standardized numbers from the battery itself. It’s why your brand new phone has an amazing battery and after a few months it’s horrible. nothing’s really changed that much, other than the device learning and calculating it on it’s own to give it a much more accurate view of the battery percentage “left”.