Monday, 25 February 2013

Maximising battery lifetime

Every type of battery has its own charging and discharging regime that it prefers. Car batteries are happiest when they're cycling between full charge and full discharge all the time. This is why you can buy a "battery jogger" for your motorcycle battery, to keep it healthy during winter, which does just that: charges the battery to full capacity and then discharges it, and then charges it again.

Li-Ion batteries in our laptops however, are just the opposite. They don't like to be charged to the full or discharged completely flat, and are happiest if kept between approximately 40% to 80% capacity. The most harm you can do to a Li-Ion battery is to keep charging it to full capacity - all the time. And this is exactly what we're doing with our laptop batteries when we have the power plugged in most of the time. 

If you're a road warrior, there's nothing much you can do. You need the full charge. But most of us spend quite some time at the desk not using the battery at all. This is where we can make some optimisations to extend the lifetime of the battery, so it won't have to be replaced before its second birthday. 

This is easy on Windows, because the battery charge manager in Windows is implemented and it works. On Linux, there is no battery charge manager, or I haven't seen one. There are a few implementations of battery managers that reduce screen brightness and change CPU frequency and whatnot to extend battery life on a SINGLE charge. That's not what I'm talking about. I'm talking about extending battery lifetime as a whole. In years.

The concept is simple. If you know you won't need a full charge, don't charge full. Instead, keep the battery in its comfort zone. On most ThinkPads this is easily done by installing tp_smapi and writing the charge thresholds to appropriate locations:

$ echo 40 > /sys/devices/platform/smapi/BAT0/start_charge_thresh
$ echo 80 > /sys/devices/platform/smapi/BAT0/stop_charge_thresh

More info about tp_smapi, how to install it and set it up, and other stuff that it can do, on ArchWiki.

On some ThinkPads, for instance the T420, the lower charge threshold isn't supported by tp_smapi. A workaround is to forget about the lower threshold and only change the upper threshold, depending on the current battery charge. Here is an implementation of this concept along with a few other features, version 0.1:

tp_battery (on GitHub).

This is a bash executable script. To use it, put it into your $PATH and make it executable. It depends on tp_smapi so you'll need to install that, too. Refer to ArchWiki for details. You'll also have to grant the threshold file write access for your user, perhaps best if you claim ownership straight away:

$ chown igor /sys/devices/platform/smapi/BAT0/stop_charge_thresh

Put this line into your /etc/rc.local or similar location, so it will be there at every boot. If you want the script to govern your battery charge automatically (I assume you do), set up a cron job to run $ battery -c every minute. When you need full battery charge, simply say $ battery -f or $ battery --full. When you don't need full charge anymore, put the battery on vacation saying $ battery -u.

There is also a config file batteryrc that contains the lower and upper charge thresholds. I keep my battery script in $HOME/bin, and the config file in $HOME/bin/rc.d. Maybe not the best of practices, but who cares. The important bit is that the path to the config file is hardcoded in line 7 of the script. Check it and change it if necessary.

I found 70% as the lower threshold and 80% as the upper threshold values as most appropriate for my use case. Most of the time I'm on the power cable, and when I need the battery, it's only for an hour or two. I rarely even charge to full. And, I set the full battery threshold to 96% so I don't push my battery too hard when charging full, because those last few percent do a lot of damage to the chemistry and don't really contribute much autonomy time.

Long live the battery!

No comments:

Post a Comment