(A)Tiny battery monitor

A few months ago I was looking for a cheap and simple way to keep check on the lead-acid battery of my motorcycle. Some indication on whether the electrical system is still charging or draining it, and whether the battery still holds a charge. This is very useful, since if the charging circuit or the battery starts to malfunction, any kind of early warning is better than getting stranded...On ebay, I stumbled upon a popular type of battery monitor LEDs, from a company called Gammatronix:

This got me interested. How does it work? It is small enough to fit partly into a 10mm LED holder. Since it is kind of programmable (jumper for mode selection, other versions have a button to switch between ranges), some simple logic is out of question. Looking for image on google, I found this:

So, here we see a pushbutton, regulator, capacitor on the bottom and probably some ATtiny microcontroller. Pretty neat that it fits inside this LED-holder. I can make this too :)

The first version I had in mind would use a switching regulator for the power supply, but SMD components would be required and it would still be a lot bigger due to the necessary inductance. Since an ATtiny does not consume much, I opted for a simple 7805 regulator.

The ADC2 input of the AtTiny measures the battery voltage through a 1/4 divider, pictured on the left. So there's really not much to it. I decided on ordering ATtiny85's, because they have more program space. The "leftovers" are then useful for other projects...

To program the ATtiny, I'm using an Arduino Uno that is programmed with the ISP sketch. But to keep the PCB small, I will program the ATtiny outside the board, and then plug it into an IC-socket on the final PCB.

Code can be developed in the Arduino IDE, after adding support to it (I followed the info from here, but an alternative could be this).

Eventually a protoboard version is put together. I tried leaving out the capacitor, but then the 5V supply became a total mess. Decoupling is unavoidable :P

After some trimming, the pcb fits neatly inside the LED holder, just like the "original" :)

The code is very straightforward as well, and is available on github. There is an interrupt loop that periodically reads the ADC, and stores the resulting sample in an array. The content of the array with the measured values is averaged in the main loop and used to decide on the behavior of the LED.

Also, after blowing up the regulator due to wrong battery polarity, a diode was added to the input. Due to the construction of the board, this diode is also present to the ADC, so the measured voltage is 0.7V lower. This has to be compensated with an offset.

There are 5 distinct voltage regions:

During startup, the LED is disabled until the array is filled with measurements, to avoid confusing indications.