This weekend’s programme consisted of several new projects (battery tester, switching mains lights using a microcontroller, an ATTiny programmer and a touch-based instrument), but this post is just about the battery tester. Having worked with schools over the summer using the littleBits modules, it became clear that we needed a simple device to check the state of the 9V batteries used to power the littleBits, as it was difficult to determine which ones were still useful and which should be binned. The problem is that batteries which would still illuminate the LED on the power module were not necessarily usable, as they might not have enough energy left in them to drive a motor or servo. Hence, we need a device to check the state of the charge, with a simple indicator that would show whether the battery was new, usable or dead.
Overview
The device constructed over the weekend is based upon an ATTiny85, and uses a simple (minded, as I’m not an engineer) circuit that puts the battery under load and checks the voltage across the load – the ‘load’ being a 100Ω resistor. A voltage divider is used to bring the voltage down from 9V to below 5V which the ADC on the ATTiny can read without killing it, and a diode is placed inline with the positive terminal so that accidental reverse polarity won’t wreck the microcontroller. Output is via three LEDs to show the battery state. Obviously, as battery is not required to power it!
Schematic
Here’s the schematic for the circuit. I drew it up in about 5 minutes, which is why it is not the most optimal (some components are upside down) … but I can understand it, and it works. I’m sure some of you clever engineers out there can make this a lot neater, but hey ho.

Here’s the component list:
R1 : 100Ω
R2, R3: 10kΩ
R4, R5, R6: 220Ω
R7: 10k trimmer (a 1k will work also)
D1: 1N4001
C1, C2: 10μF, 16V
U1: L7805CV
LEDs: 5mm, red, green, amber
IC1: Atmel ATTiny85
25 x 9 stripboard or equivalent
Notes: All resistors are 1//4W. The 7805 is overkill, but all I had.
Stripboard
Construction is fairly easy … unless you are prone to cockups like me. I had spent most of the day soldering, and was tired. So accidentally soldered the Voltage reg into the wrong place and had to cut it free and resolder.
Here’s the breadboard and a picture of the completed stripboard. Note that I had to change the location of the load resistor on the stripboard as I didn’t leave enough space on the board beneath the IC when I soldered the other components into place. Did I say that I was prone to cockups?

Photos of the completed board (LED on wires, out of shot):
Code
The code for the tester is equally as simple minded as the circuit, and uses clunky Arduino-esque C. Here it is. Use it at your peril.
// Kingarthursdog 06-12-2015
// Simple battery tester based upon an Atmel ATTiny85. Circuit is a simple voltage divider and load for the
// battery (100 ohm). When the battery measures more than a threshold value it is considered good, inbetween an
// upper and lower limit it is considered usable but nearing the end of its life, and below the threshold, it needs
// replacing. Uses red, amber and green LEDs for output. A trimmer is added to slide the scale to adjust the lower
// and middle limits.
#define UPPERLIMIT 78
#define LOWERLIMIT 68
#define VINPUT A2
#define TINPUT A3
#define ROUT 0
#define YOUT 1
#define GOUT 2
void setup() {
// Set up inputs for battery input, and for the trimmer
pinMode(VINPUT, INPUT);
pinMode(TINPUT, INPUT);
// LED outputs
pinMode(ROUT, OUTPUT);
pinMode(YOUT, OUTPUT);
pinMode(GOUT, OUTPUT);
// Set the LEDs off to begin with
digitalWrite(ROUT, LOW);
digitalWrite(YOUT, LOW);
digitalWrite(GOUT, LOW);
// Serial port for test output
//Serial.begin(9600);
}
void loop() {
// Scale the input from the battery pin to a range between 0 and 99 (just for simplicity)
double val = map(analogRead(VINPUT), 0, 1023, 0, 99);
int trimmer = map(analogRead(TINPUT), 0, 1023, -50, 50);
int lower = LOWERLIMIT + trimmer;
if (val > 0) {
if (val >= UPPERLIMIT) {
// Display green
digitalWrite(GOUT, HIGH);
digitalWrite(ROUT, LOW);
digitalWrite(YOUT, LOW);
} else if (val < UPPERLIMIT && val >= lower) {
// Display amber
digitalWrite(YOUT, HIGH);
digitalWrite(ROUT, LOW);
digitalWrite(GOUT, LOW);
} else {
// Display red
digitalWrite(ROUT, HIGH);
digitalWrite(YOUT, LOW);
digitalWrite(GOUT, LOW);
}
} else {
digitalWrite(ROUT, LOW);
digitalWrite(YOUT, LOW);
digitalWrite(GOUT, LOW);
}
// TEST OUTPUT
/*Serial.print(val);
Serial.print(", ");
Serial.println(trimmer);*/
delay(200);
}
The Box
The tester works as it is, but needs a container. I’ll post that when I have (laser) cut it out and assembled it.