Limiting Battery Charge on a Lenovo Carbon X1 (Shallow Thoughts)

Akkana's Musings on Open Source Computing and Technology, Science, and Nature.

Sat, 12 Nov 2022

Limiting Battery Charge on a Lenovo Carbon X1

Ever since I got my Lenovo Carbon X1, I've wished there was some way to limit the battery charge. I keep it plugged in to a USB hub and external monitor most of the time, which means that the battery is at 100% for weeks on end. That isn't particularly good for lithium ion batteries: it's better for battery life to stop charging at around 80%.

Lots of laptops, including Dells and Apples, have a charge limit feature in their BIOS, but I searched through the CX1's BIOS several times and never found anything, so I'd resigned myself.. But just this week I accidentally stumbled on a way to set this at runtime!

It involves the files /sys/class/power_supply/BAT0/charge_start_threshold and charge_stop_threshold. Initially, charge_start_threshold is 0 and charge_stop_threshold is 100. But you can change them.

The directory /sys/class/power_supply/BAT0 has lots of interesting entries, but one thing I noticed immediately was that it had both charge_control_start_threshold and charge_start_threshold, and both charge_control_end_threshold and charge_stop_threshold. The two pairs of entries mirror each other: in other words, if you change charge_stop_threshold, charge_control_end_threshold changes to match, and vice versa.

Here's the documentation for /sys/class/power_supply. It mentions the charge_control_ entries but not the charge_ entries. I'm guessing that the charge_ entries are older and no longer supported, but kept in place for backward compatibility? but I wish I could find something that says so explicitly.

Here's what the documentation says about the two charge_control properties:

charge_control_start_threshold
Represents a battery percentage level, below which charging will begin.
charge_control_end_threshold
Represents a battery percentage level, above which charging will stop. Not all hardware is capable of setting this to an arbitrary percentage. Drivers will round written values to the nearest supported value. Reading back the value will show the actual threshold set by the driver.

To limit charging so it won't charge above 80%:

# echo 80 >/sys/class/power_supply/BAT0/charge_control_end_threshold

If you prefer sudo, you have to wrap it in a subshell so the redirection is run as root:

sudo sh -c "echo 80 >/sys/class/power_supply/BAT0/charge_control_end_threshold"

I was going to add something about how to do this at boot time, but it turns out the change is persistent, so there's no need. Hurrah!

For now, I'm setting the end threshold to 80, and the start threshold to 60. That means that if I use the laptop unplugged and the charge dips below 60%, the next time I plug in it will charge back to 80%, then stop.

I just wish I'd learned about this when the battery was new!

Tags: , ,
[ 16:17 Nov 12, 2022    More linux/laptop | permalink to this entry | ]

Comments via Disqus:

blog comments powered by Disqus