# Application to know how long my batteries will last?

I am new in the raspberry world and learning to program it with C# on Windows IOT Core.

Right now I am working with it connected to the power supply.

However I would like to develop an application to know how long the battery will last, a simple battery icon showing percentage.

Is out there a sample code to implement this independently of whatever battery I connect to the raspberry?

• Don't forget that Pi cannot accurately measure battery status at all due to lack of an ADC. You will need extra hardware or at least a "smart" digital battery if such things exist. Jun 7, 2016 at 11:24

## 3 Answers

Determination of the charge of a battery seems to be more of an art than a science. It also highly depends on the type of the battery and numerous other factors, see e.g. here: How can charge be determined?. Independently of whatever battery I connect is therefore probably wishful thinking only. Select a battery type and do in-depth research of applicable techniques to determine the charge of this type.

The Pi however might not be the best choice for a mobile application running of batteries, see e.g. here.

• good comment, but I still need to develop an app that can show me the battery percentage just like a smartphone does Jun 7, 2016 at 1:43
• I think what's being stressed here is that the techniques most commonly used don't allow you to instantaneously measure the remaining battery amp-hours of a random battery. Consider your smartphone - it always uses an identical model of battery, if not actually the same one, and it's normally responsible for charging that battery. The phone has access to data on how long, at what voltage, etc. that particular battery model was charged and can infer remaining capacity from that. That system doesn't work if you throw random AAs at it. Jun 7, 2016 at 10:10
• more to the point, smartphone batteries (laptop batteries too) have a dedicated battery-control microcontroller whose primary purpose is to make sure the power supply is feeding the right stuff to the battery, but in the process records battery usage information (that's how your laptop can know how much life the battery has). Jun 8, 2016 at 7:02

The best way I can see this happening is taking a low end UPS that has a modifiable API that you can call up or design your own interface to.

You may be able to get away with stripping it down, re-casing it to a smaller form factor and using different batteries depending on the tolerance of the unit for different batteries.

Down sides to this are most UPS batteries are 6volts or more. You would need a regulator circuit between the pi and the UPS to step it down. Also it would require another cord to the pi (ethernet or serial).

Edit: To clarify, you would connect the regulator directly to the batteries, not to the UPS circuitry. The UPS circuitry would be entirely for the recharging and the API only. Further, to save battery life, you would also add a switch to disable the circuit that powers the 120volt outputs while in battery mode.

You would not need to plug it into the wall and act as a true UPS as the batteries also power the back-end for the API. But it would also provide a means to correctly charge it.

Then once you accomplish that, write a web based C# widget to call up specific parts of that API.

I've built a little circuit using an INA219 with an ESP8266 that continuously measures current and voltage, and outputs it in a variety of ways (LCD, serial, raw tcp stream, etc). It's a work-in-progress, but if you are curious, here it is: https://github.com/jayeye/jardino/tree/master/esp8266/ammeter