# Battery Indicator

Hi all,

Getting my feet wet in Arduino with a rough battery meter using the ATtiny85 and a RGB LED. I’ve been reading as much as I can find on similar projects, and have a first draft of some code. While I’m waiting on the voltage regulator to come in, I was hoping to get some feedback on connecting the ATtiny and my code. Am I on the right track, or do I need to go back to square one? Thanks very much in advance!

#define R1 680
#define R2 110

void setup() {

pinMode(2, OUTPUT);
pinMode(1, OUTPUT);
pinMode(0, OUTPUT);

}

void loop() {

float Vout = analogRead(3) * (1.1/1023.0);

float Vin = Vout * (R1+R2)/R2;

//Blue

if(Vin > 7.5)
digitalWrite(0, HIGH);
else
digitalWrite(0, LOW);

//Green

if(Vin == 7.5 && Vin > 5)
digitalWrite(1, HIGH);
else
digitalWrite(1, LOW);

//Red

if(Vin <= 6)
digitalWrite(2, HIGH);
else
digitalWrite(2, LOW);

}

I think the resistors for the leds are too small.

The regulator is missing some capacitors.

You are computing based on a reference voltage of 1.1V but don't select it.

The resistors for the voltage divider are too small. You've got 7.4V / (680R + 110R) = 9ma draining from the battery at all times. Multiply those resistor values by 10 or more to keep that wasted current down.

The ratio of the resistors is off as well. 7.4V will read as 1.03V, or analogRead would report (1.03 * 1024 / 5 =) 210 for a full battery. If the 110R resistor was 800R you would have a 4.54V for a fully charged two cell LiPo (which is actually 8.4V). You want it to be close to 5V but not too close to give yourself a margin of safety.

33k to +batt (not to regulator in), and 4k7 to ground gives 1.047volt on the analogue input.

Don't forget caps on the regulator (see datasheet).
The regulator might oscillate without them, and that could destroy your ATtiny.
Leo..

My RGB has forward voltage of 2, 3.2, 3.2 at 20mA. Are my resistor calculations off?

I'll check the datasheet on the caps, the example I was following specifically left them out. Thanks for the heads up!

As for selecting the reference voltage, should I be using "int sensorValue" to set the reference?

Thank you for pointing out the current drain! I don't know how I didn't think of that.

As for the ratio. I read the voltage to the analogue input needs to be under 1.1V; is this incorrect then? Also I should be calculating for the 8.4V not the 7.4V?

Thanks for the help!!!

If you assume output pin resistance is ~40ohm...
(5-3.2) / (40+90) = ~13.8mA
(5-2) / (40+150) = ~15.6mA
20mA LED current is for max brightness. e.g. to illuminate something.
A LED used as indicator can be driven with much less current.
Try if 220ohm for red and 150ohm for green/blue is bright enough.

In the IDE: Learning > Reference > AnalogReference
analogReference(INTERNAL); // use 1.1volt Aref
Leo..

LiPo cells are 3.2V discharged, 4.2V charged, then multiply by two for a two cell LiPo. 3.7V is the "nominal" per-cell voltage similarly to how a car battery nominal voltage is 12V (but actually ~14.4V when fully charged).

Not using caps on the regulator is a great way to blow lots of time troubleshooting. You get lots of quick/imperceptible voltage sags that just cause everything to "not work". Always include them at the start then after you get everything working you can make a decision as to whether you need them or not.

Also see: De-coupling

``````#define R1 6800
#define R2 1000

void setup() {

pinMode(2, OUTPUT);
pinMode(1, OUTPUT);
pinMode(0, OUTPUT);

analogReference(INTERNAL); // 1.1v Aref

}

void loop() {

float Vout = analogRead(3) * (1.1/1024.0);

float Vin = Vout * (R1+R2)/R2;

//Blue

if(Vin > 8.4)
digitalWrite(0, HIGH);
else
digitalWrite(0, LOW);

//Green

if(Vin == 8.4 && Vin > 7)
digitalWrite(1, HIGH);
else
digitalWrite(1, LOW);

//Red

if(Vin <= 7.7)
digitalWrite(2, HIGH);
else
digitalWrite(2, LOW);

}
``````

Updated code and schematic.

analogReference(INTERNAL1V1) returned an error. Will analogReference(INTERNAL) default to 1.1V?

Resistor ratios for the divider look okay now? 1.08V too close to 1.1V or good enough?

And do the capacitors look okay? The de-coupling tutorial recommended 47uF, but the datasheet called for 0.33uF so I used that.

I think the resistors for the LEDs (blue and green at least) are still too low.
From my personal experiences I always ended with much higher values for blue and green than for red to get close to a combinatorial white. The red LEDs seem to be a lot less efficent, the different color-sensibility of the human eye adds to that effect. Best to try it on a breadboard.

ndknoy:
analogReference(INTERNAL1V1) returned an error. Will analogReference(INTERNAL) default to 1.1V?

That depends on the core you're using. But yes, it more than likely is equivalent.

ndknoy:
And do the capacitors look okay? The de-coupling tutorial recommended 47uF, but the datasheet called for 0.33uF so I used that.

You're mixing up the reason why capacitors are used with the linear regulator and why capacitors are added on a per-chip basis. The .33uF and .1uF with the regulator are there just to make the regulator stable (per the datasheet, and always try to follow any datasheet's recommendations). There should also be a third .1uF between the VCC and GND of the ATTiny85 itself - and it should be located as close as possible to the chip.

Every time you blink that LED white it's going to cause an instantaneous ~40ma power sag. You're adding the .1uF near the ATTiny to ensure that those power sags don't cause the chip to brownout or reset.

I'll test some different LED resistors Whandall. I plan to test the circuit with a friend's power supply before I make a PCB.

Thanks for clarifying Chagrin. I find myself having to remember quite a bit from my E&M classes in college on this project.

Big thank you to everyone! It would have taken me a lot longer to iron out this circuit without all the help.