Introduction: Arduino Battery Checker
This will show you how to setup your arduino to check to see how charged your battery is.
Step 1: The Setup
This is the basic setup of the battery checker.
List of Items Needed.
3 LED's prefer green, yellow, and red.
3 resistors 330 OHMS
6 breadboard wires
2 alligator clips
Arduino
Breadboard
Step 2: The Code
This is the basic code. If you have troubles with the led's lighting up then make sure you do not assign the led output number to a variable but a number. Example: pinMode(13,OUTPUT); --- digitalWrite(13,HIGH);. if you have a different battery then a 5V then change the 5.00 in the algorithm (voltage = analog * (5.00/1023)) to the max Voltage on your battery.
THE CODE:
int analog = 0; // To read the value of the battery.
float voltage = 0; // To take analog and turn it into Voltage
int ledDelay = 1000;
void setup() { // put your setup code here, to run once:
pinMode(13,OUTPUT); //Green led
pinMode(3,OUTPUT); //Yellow led
pinMode(2,OUTPUT); //Red led
pinMode(0,INPUT); //Battery input
Serial.begin(9600);
}
void loop() { // put your main code here, to run repeatedly:
analog = analogRead(0);
voltage = analog * (5.00/1023); // Algorithm for the code to be the right voltage.
Serial.println(voltage);
if(voltage>=1.6)
{
digitalWrite(13,HIGH);
}
else if(voltage >1.2 && voltage <1.6)
{
digitalWrite(3,HIGH);
}
else if(voltage<=1.2)
{
digitalWrite(2,HIGH);
}
digitalWrite(13,LOW);
digitalWrite(3,LOW);
digitalWrite(2,LOW);
}