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

**URL:** <https://forum.clockworkpi.com/t/how-to-read-battery-voltage-percentage-from-command-line-python/11848>\
**Category:** DevTerm\
**Created:** [December 6, 2023, 8:39pm UTC](https://forum.clockworkpi.com/t/how-to-read-battery-voltage-percentage-from-command-line-python/11848 "2023-12-06T20:39:44Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mischko](https://avatars.discourse-cdn.com/v4/letter/m/ed655f/32.png) [@mischko](https://forum.clockworkpi.com/u/mischko)\
**Post date:** [December 6, 2023, 8:39pm UTC](https://forum.clockworkpi.com/t/how-to-read-battery-voltage-percentage-from-command-line-python/11848/1 "2023-12-06T20:39:45Z")

</div>

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.

Thanks,  
Scott

---

<div class="post-metadata">

**Author:** ![exelotl](https://yyz1.discourse-cdn.com/flex029/user_avatar/forum.clockworkpi.com/exelotl/32/6654_2.png) [@exelotl](https://forum.clockworkpi.com/u/exelotl)\
**Post date:** [December 7, 2023, 7:07pm UTC](https://forum.clockworkpi.com/t/how-to-read-battery-voltage-percentage-from-command-line-python/11848/2 "2023-12-07T19:07:41Z")

</div>

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

```sh
cat /sys/class/power_supply/axp20x-battery/capacity

```

---

<div class="post-metadata">

**Author:** ![mischko](https://avatars.discourse-cdn.com/v4/letter/m/ed655f/32.png) [@mischko](https://forum.clockworkpi.com/u/mischko)\
**Post date:** [December 7, 2023, 8:27pm UTC](https://forum.clockworkpi.com/t/how-to-read-battery-voltage-percentage-from-command-line-python/11848/3 "2023-12-07T20:27:42Z")

</div>

Thanks! I’ll tinker with it.

---

<div class="post-metadata">

**Author:** ![exelotl](https://yyz1.discourse-cdn.com/flex029/user_avatar/forum.clockworkpi.com/exelotl/32/6654_2.png) [@exelotl](https://forum.clockworkpi.com/u/exelotl)\
**Post date:** [December 7, 2023, 9:48pm UTC](https://forum.clockworkpi.com/t/how-to-read-battery-voltage-percentage-from-command-line-python/11848/4 "2023-12-07T21:48:48Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![1337p337](https://yyz1.discourse-cdn.com/flex029/user_avatar/forum.clockworkpi.com/1337p337/32/8689_2.png) [@1337p337](https://forum.clockworkpi.com/u/1337p337)\
**Post date:** [December 10, 2023, 6:25am UTC](https://forum.clockworkpi.com/t/how-to-read-battery-voltage-percentage-from-command-line-python/11848/5 "2023-12-10T06:25:26Z")

</div>

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

```auto
#!/bin/sh
awk -F'=' '
  {a[$1]=$2}
  END{
    printf "%3d%% %s (%s)\n",
      a["POWER_SUPPLY_CAPACITY"], a["POWER_SUPPLY_STATUS"], a["POWER_SUPPLY_HEALTH"]
  }' '/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:

```auto
$ find /sys/devices/platform -name uevent | grep axp20x-battery

```

---

<div class="post-metadata">

**Author:** ![oliof](https://yyz1.discourse-cdn.com/flex029/user_avatar/forum.clockworkpi.com/oliof/32/9717_2.png) [@oliof](https://forum.clockworkpi.com/u/oliof)\
**Post date:** [June 25, 2024, 5:43pm UTC](https://forum.clockworkpi.com/t/how-to-read-battery-voltage-percentage-from-command-line-python/11848/6 "2024-06-25T17:43:03Z")

</div>

This works great, thanks for the awk script. On the R-01, the following file path works for me: `/sys/devices/platform/soc@3000000/2502000.twi/i2c-0/0-0034/axp20x-battery-power-supply/power_supply/axp20x-battery/uevent`
