Step 4: Finally....a Charlieplex Matrix
As mentioned in the introduction, charliplexing is a handy way of driving lots of LEDs with only a few pins on a microcontroller. However in the previous pages we've not really saved any pins,driving two LEDs with two pins....big whoop!
Well we can extend the idea of complentary drive into a charlieplex matrix. The diagram below shows the minimum charlieplex matrix consisting of three resistors and six LEDs and using only three microcontroller pins. Now do you see how handy this method is? If you wanted to drive six LEDs in the normal way....you'd need six microcontroller pins.
In fact with N pins of a microcontroller you can potentially drive N * (N - 1) LEDs.
For 3 pins this is 3 * (3-1) = 3 * 2 = 6 LEDs.
Things stack up quickly with more pins. With 6 pins you can drive 6 * (6 - 1) = 6 * 5 = 30 LEDs....wow!
Now to the charlieplexing bit.
Look at the diagram below. We have three complementary pairs, one pair between a each combination of micro output pins. One pair between A-B, one pair between B-C and one pair between A-C.
If you disconnected pin C for now we'd have the same situation as before. With 5V on pin A and 0V on pin B, LED1 will glow, LED2 is reverse biased and will not conduct current. With 5V on pin B and 0V on pin A LED2 will glow and LED1 is reverse biased.
This follows for the other micro pins.
If we disconnected pin B and set pin A to 5V and pin C to 0V then LED5 would glow. Reversing so that pin A is 0V and pin C is 5V then LED6 would glow.
Same for the complementary pair between pins B-C.
Hang on, I hear you say. Lets look at the second case a bit more closely.
We have 5V on pin A and 0V on pin C. We've disconnected pin B (the middle one).
OK, so a current flows through LED5, current isn't flowing through LED6 because it is reverse biased (and so are LED2 and LED4)....but there is also a path for the current to take from pin A, through LED1 and LED3 isn't there? Why are these LEDs not glowing as well.
Here is the heart of the charlieplexing scheme. Indeed there is a current flowing both LED1 and LED3, however the voltage across the both of these combined is only going to be equal to the voltage across LED5. Typically they would have half the voltage across them that LED5 has. So if we have 1.9V across LED5, then only 0.95V will be across LED1 and 0.95V across LED3.
From the If/Vf curve mentioned at the beginning of this article we can see that the current at this half voltage is much much lower than 20mA.....and those LEDs will not glow visibly.
This is known as current stealing.
Thus most of the current will flow though the LED we want, the most direct path through the least number of LEDs (ie one LED), rather than any series combination of LEDs.
If you looked at the current flow for any combination of putting 5V and 0V on any two drive pins of the charlieplex matrix, you'll see the same thing. Only one LED will glow at a time.
As an exercise, look at the first situation. 5V on pin A and 0V on pin B, disconnect pin C. LED1 is the shortest route for the current to take, and LED 1 will glow. A small current will also pass through LED5, then back up LED4 to pin B.....but again, these two LEDs in series will not be able to syphon enough current compared to LED 1 to glow brightly.
Thus the power of charlieplexing is realised. See the second diagram which is the schematic for my Microdot watch.....30 LEDs, with only 6 pins. My Minidot 2 clock is basically an expanded version of the Microdot....same 30 LEDs arranged in an array.
To make a pattern in the array, each LED to be illuminated is briefly switched on, then the micro moves to the next. If it is scheduled to be illuminated it is switched on again for a brief time. By quickly scanning through the LEDs fast enough a principle called 'persistance of vision' will allow an array of LEDs to show a static pattern. The Minidot 2 article has a bit of an explanation on this principle.
But wait.....I've seemingly glossed over a bit in the description above. What's this 'disconnect pin B', 'disconnect pin C' business. Next section please.