Ways to save battery for your device

Fang Jin
4 min readOct 11, 2023

If you are working with computer, you might not care about the electricity. However right after you switch to a laptop, a pad, or a phone, you start to notice battery is kinda important to you. Since the battery capacity is normally capped for the device, the power consumption of your device becomes a key factor. This applies to my custom-made device as well, a device for scrolling the computer screen remotely.

It’s a silly idea, as my wife tells me: Do you know there’s something called a wireless mouse? Yeah I heard about it, but I made it mine this time. I use it to scroll the screen while sitting away from my keyboard or desktop and thinking about a problem to relax. I found it handy and small enough to fit in my pocket. FYI, the material cost is about $20. Very kool IMHO.

After I prototyped it, I run into the battery problem quickly. For instance taking it outside of the house and to the office etc. where I have to solely rely on the battery. Based on some usages, I found it can last about 400/70=6 hours total. The battery capacity is 400mAh, and my device current draw is about 70mA.

This is when I start to look for ways to save battery, yeah 6 hours of usage is a bit short considering the casual usage of it.

A power switch

A physical power switch is the first to come in mind. If I can turn off the device entirely when I finish using it, I’m sure I can save some battery. Quickly I soldered a switch to it so that I can switch the power on and off. The battery is hidden behind the chip. And I kept the old battery socket (right) where you can turn on the power directly. With the new battery socket (left) and a switch installed on the left, I can plug in the battery while be able to turn it on manually.

This approach is quite effective considering my device takes only 0.1 second to re-connect to the bluetooth and reboot the firmware, so there isn’t much downtime. This way the lifetime of the battery depends on the real usage now. If you don’t switch it on, it won’t cost you a dime. Sweet.

Of course you still need to remember to turn it off. Practically this could be cumbersome and easy to forget. So I want to find out if there’s a way to save the battery when the device is running.

Sleep mode

I know my device takes 70mA when running, but most time it’s just sitting in my pocket in an idle state. So maybe I should make it hibernate while idle and later wake it up when I need to use it again. Quite intuitive idea, I would say.

Luckily the microcontroller ESP32Dev1 I’m using has a sleep mode built-in, according to the documentation and some online tutorial, it can lower the consumption to 0.1mA under this deep sleep mode. This means if you don’t wake the device up, it can last 400/0.1=4000 hours in my case, more than half a year. This is a bit crazy long.

Before switching to the deep sleep, you have to let the device know when it will wake up in the future. For instance, either setup a wakeup time, or setup a switch to wake it up.

  if (millis() - lastActivity > 10 * mS_TO_S_FACTOR)
{
Serial.println("Idle and sleep");
rgbLed.flash(RGBLed::RED, 20);
esp_sleep_enable_timer_wakeup(10 * uS_TO_S_FACTOR);
delay(1000);
esp_deep_sleep_start();
return;
}

Let’s try the wakeup time first, I uploaded the above code to the firmware: basically if I see no activity in 10 seconds of my device, I’ll ask it to sleep, and wait there until 10 seconds later to wake it up.

If I roughly calculate the battery usage, I believe I saved half. For the time I’m not using the device, it wake up for 10 seconds and then sleep for 10 seconds. So I used half of the running time. Ok, you might wonder how did I come up 10 seconds of sleep time. I don’t know.

So maybe it’s better that I don’t predict this sleep time for the future, instead, I should try to use a hard switch to wake it up manually.

  if (millis() - lastActivity > IDLE_TIME * mS_TO_S_FACTOR)
{
Serial.println("Idle and sleep");
rgbLed.flash(RGBLed::RED, 20);
touchAttachInterrupt(T3, callback, TOUCH_THRESHOLD);
esp_sleep_enable_touchpad_wakeup();
delay(1000);
esp_deep_sleep_start();
return;
}

This time I replaced the firmware with a touch pin approach above with a handy function to wake up when a pin (ex. T3) is physically touched. After I uploaded the above firmware, the device goes to sleep after 10 seconds, and it will stay asleep until touched. Now the battery is consumed by demand. This is really awesome! In the end I actually kept the 10 second idle time settings since the bluetooth reconnection is just so efficient.

Conclusion

What I have learned here is that, in order to make the battery last long, you want to aim for a low consumption device. This can reduce the number of trips of battery recharge. In case the energy consumption and battery capacity are both capped, we can try to work with an idle/hibernation approach, so that when there’s no user activity, the device can switch to a low energy consumption mode and stay there until the wakeup.

--

--

Fang Jin
Fang Jin

Written by Fang Jin

Front-end Engineer, book author of “Designing React Hooks the Right Way” and "Think in Recursion"

No responses yet