This Instructable is to provide a way of displaying the life of a battery on a LCD. This can be useful if you have a project that is powered from batteries. It'll provide an indication whether you need to change out your batteries. The code may also be adjusted to include an alarm or warning LED. The possibilities are up to you!
This first code displays battery life using a byte image on an LCD. It displays battery life at 100%, 75%, 50%, 25%, and blinks when 10% or less.
This sketch allows you to display as a percentage on the LCD. The range is from 0% to 100%.
The last way is to actually display the exact voltage on the LCD.
If you look at line 16 in the image above, you'll notice the line of code as:
float volts = (voltReading/204.6);
To break this part down is crucial for using different voltage sources:
(1) float volts will display the voltage value in a decimal fashion. If you want a whole number, replace float with int.
(2) Depending on the voltage your reading, the number you use to divide is decided by the maximum of the actual voltage source you are measuring. As you can see the number is 204.6. This number was created by taking 1023 (the maximum reading of a fully charged battery) and divided by the maximum voltage of the battery you are using. In this case, I used a 5V battery so I used 1023/5 = 204.6. You then use 204.6 to divide by. If you use a 3V source, then 1023/3 = 341. Then you would divide by 341 to display battery voltage ranging from 0V to 3V vice 0V to 5V.