/*
* Arduino 18650 Battery Gauge
*
* learnelectronics
* 13 JUL 2017
*
* [Link]/c/learnelectronics
*/
#include <LiquidCrystal.h> //we need the
library for the lcd display
LiquidCrystal lcd(4, 6, 10, 11, 12, 13); //we ned to tell
the arduino which pins to use with the lcd
float rawV = 0.0; //a variable
with a decimal point to hold battery voltage
int batC = 0; //a variable
with no decimal point to hold battery percentage
int graph = 0; //a variable
with no decimal point to hold the value for the graph
void setup() { //only runs when
the Arduino is powered on reset
[Link](16, 2); //start lcd with
16 columns & 2 rows
[Link](); //clear any
garbage from screen
pinMode(A0,INPUT); //we will use
analog pin 0 to read voltage from 18650
void loop() { //runs over and
over
rawV = (analogRead(A0)*4.98)/1024; //figure out the
battery voltage (4.98 is the actual reading of my 5V pin)
//some logic to
set values
if (rawV <3.6){ //battery @ 3.5V
or less
batC = 0; //% = 0
graph = 0; //# of graph
segments to light
}
if(rawV > 3.5 && rawV < 3.7){ //battery @ 3.6V
batC = 2;
graph = 1;
}
if (rawV > 3.6 && rawV < 3.8){ //battery @ 3.7V
1
batC = 12;
graph = 4;
}
if (rawV > 3.7 && rawV <3.9){ //battery @ 3.8V
batC = 42;
graph = 7;
}
if (rawV > 3.8 && rawV < 4.0){ //battery @ 3.9V
batC = 62;
graph = 10;
}
if (rawV > 3.9 && rawV < 4.1){ //battery @ 4.0V
batC = 79;
graph = 13;
}
if (rawV > 4.0 && rawV < 4.2){ //battery @ 4.1V
batC = 81;
graph = 15;
}
if (rawV > 4.19){ //battery @ 4.2V
batC = 100;
graph = 16;
}
//send all the
values to the lcd
[Link](0,0);
[Link]("V:");
[Link](3,0);
[Link](rawV);
[Link](10,0);
[Link]("%:");
[Link](13,0);
[Link](batC);
[Link](0,1);
for(int m = 0; m < graph; m++){ //draw the graph
(light up m number of segments)
[Link](255);
}
delay(1000); //wait one
second to stabilize
[Link](); //clear previous
data from lcd