9 Volt Battery Checker

On one of my future projects, I needed to be able to monitor the current level of the 9 volt battery I'm planning on using for power.

I didn't want any digital readout or array of LEDs, just a simple RGB LED that will indicate the current level of the battery with a quick blink roughly every 10 seconds. The color of the blink will tell me how close I am to my battery being depleted. Green is above 90%. Below that, the green will fade and red will start to be more prominent the more the battery is drained. Simply enough.

The 10 second blink, as opposed to a constant light is to save battery power. The project I plan to use this on will be in a hard to reach area and I don't want to have to replace the battery very often.

For the RGB LED I used 3 100RΩ resisters

For the battery monitor I used 2 resisters, the first is a 1MΩ and the second is a 100kΩ.

The design is extremely easy to do. Simply put a jumper between the two resisters and attach it to the A2 port on the Arduino. Put the lead from the + side of the 9v battery to the 1MΩ resistor and ground out the other side of the 100KΩ resistor. Now you can take your readings.

I still have work to do on the code to clean it up but the general idea is there.

#define NUM_SAMPLES 10

int sum = 0; // sum of samples taken
unsigned char sample_count = 0; // current sample number
float voltage = 0.0; // calculated voltage
float R_LOAD = 5.015;
float numofchecks = 0;
float totalvoltage = 0;
float ledvalue = 0;

int ledRed = 6;
int ledGreen = 5;
int ledBlue = 3;

int green = 0;
int yellow = 0;
int red = 0;

int ledtimer = 0;

void setup()
{
Serial.begin(9600);
}

void loop()
{
ledtimer++;
numofchecks++;
// take a number of analog samples and add them up
while (sample_count < NUM_SAMPLES) {
sum += analogRead(A2);
sample_count++;
delay(10);
}
// calculate the voltage
// use 5.0 for a 5.0V ADC reference voltage
// 5.015V is the calibrated reference voltage
voltage = ((float)sum / (float)NUM_SAMPLES * R_LOAD) / 1024.0;
// send voltage for display on Serial Monitor
// voltage multiplied by 11 when using voltage divider that
// divides by 11. 11.132 is the calibrated voltage divide
// value

voltage = voltage * 11.132;
totalvoltage += voltage;

if(numofchecks > 10)
{
float tv = totalvoltage / numofchecks;
ledvalue = tv;
Serial.print("Avg V: ");
Serial.print(tv);
Serial.print("\t");
Serial.print("\t");
Serial.print("Battery Level: ");
Serial.println();

numofchecks = 0;
totalvoltage = 0;
}

sample_count = 0;
sum = 0;

if (ledtimer > 100)
{
ledtimer = 0;
ShowLED();
}
}

void ShowLED()
{
float batterycap = 9;
float batteryper = ledvalue / batterycap;

float greenval = 50 * batteryper;
float redval = 0;
if (batteryper >= 0.80 && batteryper < 0.90)
{
redval = 25;
}
else if (batteryper >= 0.70 && batteryper < 0.80)
{
redval = 50;
}
else if (batteryper >= 0.50 && batteryper < 0.70)
{
redval = 100;
}
else if (batteryper < 0.50)
{
redval = 255;
}

analogWrite(ledGreen, greenval);
analogWrite(ledRed, redval);
delay(500);
analogWrite(ledGreen, 0);
analogWrite(ledRed, 0);
}