How to read battery voltage/percentage from command line/Python?

I’m on a CM4 using the latest Raspberry Pi OS.
I’d like to know how to get the battery status from the command line, or in Python.


1 Like

The following works for me in the command line (outputs a number between 0 and 100)

cat /sys/class/power_supply/axp20x-battery/capacity
1 Like

Thanks! I’ll tinker with it.

1 Like

Oops sorry, I missed that you were looking for voltage too. That also seems to be there under the same directory as voltage_now - I’m guessing the unit is µV, maybe someone more knowledgeable than me can confirm x)

Since these are exposed as files you can ought to be able to just read them from the filesystem in Python.

1 Like

I use an awk script (whitespace added for readability):

awk -F'=' '
    printf "%3d%% %s (%s)\n",
  }'  '/sys/devices/platform/ff140000.i2c/i2c-5/5-0034/axp20x-battery-power-supply/power_supply/axp20x-battery/uevent'

You can get about as much information as you want out of that interface, and they’re all $KEY=$VALUE, so that little script just builds a little table out.

For my system, it’s /sys/devices/platform/ff140000.i2c/i2c-5/5-0034/axp20x-battery-power-supply/power_supply/axp20x-battery/uevent but the filename will vary between CM4, A06, R01, etc. You can find the correct file by looking around like this:

$ find /sys/devices/platform -name uevent | grep axp20x-battery
1 Like