Tuesday, May 21, 2024
HomeHow toHow to Set Battery Charge Limit in Ubuntu 22.04 & 24.04

How to Set Battery Charge Limit in Ubuntu 22.04 & 24.04

This is a step by step beginner’s guide shows how to configure your Ubuntu laptop to limit the maximum battery charge level.

For those who keep laptop plugged in for long term, it’s better to set battery charge limit to reduce the battery wear by constantly trickle charging.

Linux Kernel supports battery charge threshold, and there’s a merge request to provide graphical UI options in Gnome Control Center. Until GNOME officially support this feature, you can follow this tutorial to do the job step by step.

Step 1: Check if your laptop supports charge threshold

Most modern laptops support charge limiting, but old machines may NOT. In my case, the cheap HP laptop does not support it, but ThinkPad does.

First, press Ctrl+Alt+T on keyboard to open up a terminal window. Then run command:

ls /sys/class/power_supply/

It will list the device names including your battery, usually BAT0, BAT1, BATC, BATT, etc.

In my case, its BAT0. To tell if it supports battery charge limit, run:

ls /sys/class/power_supply/BAT0

Replace BAT0 in last command accordingly. And, it will output following 2 files telling that its support the feature!

  • charge_control_start_threshold
  • charge_control_end_threshold

While, charge_start_threshold and charge_stop_threshold are legacy API (usually for ThinkPad) that automatically synced to the previous 2.

Step 2: Set Battery Charge Start & Maximum Limit Level

To set the charge limit, simply set the value number (from 0 to 100) of the 2 key files listed above.

For example, set the battery start charging level to 80. So, when power supply is plugged in, it only starts charging when battery is lower than 80%.

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

Here you need to replace BAT0 according to last step, and change value 80 as you want.

To set maximum battery charging limit, run command:

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

Here I set to stop battery charging when reaching level 88%. Also, change the number and BAT0 accordingly.

The changes apply immediately after running the commands above. You can verify by checking the battery icon status in system tray, or run command below to tell if it’s charging:

cat /sys/class/power_supply/BAT0/status

Step 3: Create Battery Charge Threshold Service (Optional)

The previous steps works in my ThinkPad even after reboot. If NOT for you, then create a systemd service to make it permanent.

To create a service to automatically set maximum battery charge limit on startup, do:

1. First, press Ctrl+Alt+T on keyboard to open terminal. When it opens, run command:

sudo nano /etc/systemd/system/battery-charge-end-threshold.service

This command creates a service file and edit via the nano command line text editor. You can replace nano with gedit for Ubuntu 22.04, or gnome-text-editor for Ubuntu 24.04. Though, nano works in most desktop environments.

When the file opens, paste following lines:

[Unit]
Description=Set Battery Charge Maximum Limit
After=multi-user.target
StartLimitBurst=0

[Service]
Type=oneshot
Restart=on-failure
ExecStart=/bin/bash -c 'echo 90 > /sys/class/power_supply/BAT0/charge_control_end_threshold'

[Install]
WantedBy=multi-user.target

Here replace BAT0 according to step 1, and change number 90 as you want. Finally, press ctrs+s to save file, and ctrl+x to exit.

2. After created the service file, run the following commands one by one to enable & start it.

systemctl enable battery-charge-end-threshold.service
systemctl daemon-reload
systemctl start battery-charge-end-threshold.service

If you also want to create a service to set battery start charging level, re-do the last steps, but replace the service name, as well as level number and charge_control_end_threshold with charge_control_start_threshold in file content.

Set Battery Charge Limit via TLP (Another Way)

The popular TLP power saving tool also has the feature to set charge threshold for laptop users. Though, you still need the battery support. Meaning the charge_control_start_threshold and charge_control_end_threshold files must exist under /sys/class/power_supply/BAT*.

1. First, press Ctrl+Alt+T on keyboard to open terminal. Then, run command to install TLP:

sudo apt install tlp

For the latest version, see the official Docs.

2. Then, edit the config file by running command:

sudo nano /etc/tlp.conf

Also, you may replace nano text editor according to your desktop environment. When file opens, find out the following 2 lines (use Ctrl+W in nano), un-comment (remove # at the beginning), and change the number as you want.

START_CHARGE_THRESH_BAT0=80
STOP_CHARGE_THRESH_BAT0=90

Finally, press Ctrl+S to save file and Ctrl+X to exit editing.

3. To verify if the setting applied, run sudo tlp-stat -b to check.

Finally, make sure TLP service is running by running commands:

systemctl enable tlp.service
systemctl start tlp.service

And use systemctl status tlp.service to verify the service status. For more about the TLP power saving tool, see its website.

Undo

To uninstall TLP, open terminal (Ctrl+Alt+T) and run command:

sudo apt remove tlp tlp-*

To remove the manually created startup service, run commands below one by one to stop, disable and remove the service file.

systemctl stop battery-charge-end-threshold.service
systemctl disable battery-charge-end-threshold.service
sudo rm /etc/systemd/system/battery-charge-end-threshold.service

Finally, you may reset the start and end charging threshold via commands (also replace BAT0 accordingly):

sudo sh -c "echo 0 > /sys/class/power_supply/BAT0/charge_control_start_threshold"
sudo sh -c "echo 100 > /sys/class/power_supply/BAT0/charge_control_end_threshold"



RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here


Most Popular