Any way to get or estimate the battery percentage

some devices like the meowbit have batteries,
is it possible to the battery percentage through cpu speed or something (since when batteries get lower they admit less volts instead of just turning off causing the cpu to probably run slower and the screen to dim)
this would be good for games to tell you to save your game if it detect low a percentage

I know on the Adafruit PyGamer and PyBadges, the battery is connected to A6 (an analog input) through a voltage divider. I’m not sure about other devices like the PyGamer, check their documentation about their hardware/pinouts.

  • Pin A6 is connected to a voltage divider which gives half the current battery voltage. You can read the battery voltage by using the Arcada library function readBatterySensor() (it multiplies by two to give the actual voltage), by using analogRead(A6) in Arduino, or by using analogio.AnalogIn(board.A6) in CircuitPython.

Learn guide for Adafruit PyGamer

The problem is that the voltage of a lipo is non-linear, which requires extra math/logic. Plus the voltage of a lipo decreases based on the amount of current used, which makes it seem like the percentage is lower than it actually is.

If you want an accurate percentage, search up “fuel gauge / batter monitor” chips / breakouts, (Adafruit has some) but adding this into may be difficult. (I would need to look at it more in depth to see how complicated this is)

Fortunately, if you only want to know the battery is low, you can just check the voltage. Lipos have a range of 4.2v to 3.2v, (the chart goes lower, but discharging past that can be unhealthy or even unsafe. Thankfully, the power management chips on the boards show automatically cut the battery at 3.2v) and you can check if the voltage is close to 3.2v

I believe MakeCode Arcade has blocks to read analog input pins, but this obviously won’t work on the simulator.

The screen may dim a teensy bit, but the CPU will probably not run slower - it’s either the CPU has enough current (and therefore voltage) or not enough current when the battery is low. When there is not enough current, voltage will drop, causing the CPU to either reset / glitch. (Although some CPUs have “brownout” detection - that will quickly halt the chip if the voltage dips too low, preventing the chip from glitching. It will reset the chip once the voltage rises to a suitable level again. I forget if the Arcade dev boards have this) Although it is true that when a CPU runs at lower speed, it requires less current. An automatically changing clock speed is uncommon for chips, “this cheap.” (I believe the Teensy 4.1 has it, but those are expensive.)

Sorry for the wall of text!

4 Likes

The power can be sourced from anything… I use a powerbank to run my PyGamer. A common trick smartphones and such devices use is checking the voltage of the battery current. I don’t know if that’s possible with MC devices though

1 Like

i will try to do this with micropython when i have the chance
im pretty sure it wont work on the meowbit though

1 Like

thank you

1 Like