My latest project involves using an Arduino Pro Mini to animate 15 leds. These 15 leds consist of a 10-segment bargraph, and 5 individual leds. For simplicity's sake, I wanted to avoid using additional chips or transistors to drive my LEDs, so the Pro Mini's regulator would need to supply all the current. The voltage regulator on the Pro Mini is kinda wimpy though, and after deducting the current the microcontroller uses, you've only got around 100mA to work with. I allocated 50mA to the 5 individual LEDs, which left me with 50mA for my bargraph. But the leds in the bargraph aren't particularly bright, so when I run them at 5mA, they're kinda dim.
If only I could double the current, they'd shine brightly! But how do do that?
I knew that it was possible to run two LEDS at the same brightness without doubling the current by wiring them in series. Then, rather than wasting 3.3v of your 5v as heat, you can use a smaller resistor and turn most of that excess energy into light. I'd also read about Charlieplexing, which is using the tri-state logic of a microcontroller... high, low, and input, to drive a larger array of leds than you could normally using the same number of pins.
That is what led me to this solution. I'm not sure if it'll work since I'm still new to this stuff, but I think my logic is sound:
Here's the theory of how it should operate:
Each of the 8 pins can be put into a high, low, or input state. In input state (represented by the - symbol below), the pin is effectively disconnected from the circuit, as in charliplexing.
To light the first led, you would set the pins like so: 10------
To light the second led, you would do this: -10-----
And to light the first and second leds, you would do this: 1-0-----
Now you may notice a problem. If all of the resistors have the same value, then if you're putting 10mA through the first two leds when they're lit simultaneously, then because the voltage drop of the second led isn't there when you light just one of them, you'll be putting much more than 10mA through the LED when you light only one of them.
Using a bit of algebra though, I found a solution to that issue as well.
Imagine the first three resistors in the schematic are labeled A B and C. Now, let's say you want to put 10mA through each LED, and each LED has a forward voltage of 2.1 volts. For a single LED with a 5v source, you need a 330 ohm resistor. And for two leds, you would need an 82 ohm resistor.
So, we know that:
A + B = 330
B + C = 330
A + C = 82
A + B = B + C
A = C
That being the case, if A + C = 82 and A = C, then A and C must be 41.
And now that we know the value of A, we can subsitute that into our first equation, A + B = 330, and we get B = 289.
In other words, if we calculate two resistor values X, and Y, where X is the vlaue needed for a single LED, and Y is the value needed for two leds, then we should use the following resistor values:
A = Y/2
C = Y/2
B = X-A
And this will give us the correct total resistance to provide 10mA to each LED regardless of whether we have one or two LEDs in a particular string.
Another potential pitfall you may have noticed is that when a pin is set to ground, you may have two pins to either side of it which are sinking current into it. Because of this, you'll need to do your calculations such that each LED isn't using more than 10mA. So for 8 leds, you could use 40mA total, where each led is getting 10mA.
The only other issue I see is that if one LED goes out, then the other LED in the pair will not light when you try to light both. But since the LED which is not burnt out will still light if lit alone, it wouldn't be difficult to figure out which one burnt out.
Anyway, let me know what you think. I'm not yet good enough with this stuff to be sure that this will work. I think it will. I think a little juice will flow from a high pin to an input pin, but not enough to actually light the led, which is why Charlieplexing works, I think.