Introduction: Charlieplexing LEDs- the Theory

About: update later
This instructable is less a build you're own project and more a description of the theory of charlieplexing. It's suitable for people with the basics of electronics, but not complete beginners. I've written it in response to the many questions I've gotten in my previously published Instructables.

What is 'Charlieplexing'? It is driving lots of LEDs with only a few pins. In case you're wondering Charlieplexing is named after Charles Allen at Maxim who developed the technique.

This can be useful for lots of things. You may need to display status information on a small microcontroller, but only have a few pins spare. You may want to show a fancy dot matrix or clock display but don't want to use lots of components.

Some other projects demonstrating charlieplexing you may want to look at are:
How to drive a lot of LEDs from a few microcontroller pins.
by Westfw :-
And a couple of my own projects,
The Microdot watch:-
The Minidot 2 clock:-
Another cool example of the use of charlieplexing is at:

The Minidot 2 clock introduces an advanced charlieplexing scheme for fading/dimming which won't be discussed here.

UPDATE 19 August 2008 : I've added a zip file with a circuit that may be able to exploit the matrix charliplexing for high power LEDs discussed (at length :) ) in the comments section. It has a pushbutton + position encoder to do a user interface, plus circuitry for either USB or RS232 computer control. Each of the high side voltage rails can be set to one of two voltages, say 2.2V for RED LEDs and 3.4V for green/blue/white. The voltage for the high side rails can be set by trimpot. I'd envisage that a 20wire IDC ribbon cable be plugged into the board, and 20pin IDC connectors added along the length of the ribbon, each LED board having links to whatever wires in the matrix are desired. The circuit is in Eagle Cad and rendered in the sub image below. The high side circuit is implemented by using optocouplers which I think might be suitable.

I haven't actually tested this circuit nor written any software because of lack of time, but have put it up for comment, I'm particularly interested in the optocoupler implementation. Anyone brave enough to give it a go...please post your results.

UPDATE 27th August 2008: For those not using EagleCad....added below is a pdf of the schematic

Step 1: Some LED Theory

Charlieplexing relies on a number of useful aspects of LEDs and modern microcontrollers.

Firstly what happens when you connect an LED to electricity.

The main diagram below shows what is called the If v Vf curve of a typical 5mm low power LED.
If stands for 'forward current'
Vf stands for 'forward voltage'
The vertical axis in otherwords shows the current that will flow through an LED if you put the horizontal axis voltage across it's terminals. It works the other way around as well, if you measure that the current is of some value, you can look across to the horizontal axis and see the voltage the LED will present across it's terminals.
The second diagram shows a schematic representation of an LED with If and Vf labelled.

From the main diagram I've also labelled areas of the graph that are of interest.
- The first area is where the LED is 'off'. More accurately the LED is emitting light so dimly you won't be able to see it unless you had some sort of super-duper image intensifier.
- The second area has the LED just slightly emitting a dim glow.
- The third area is where an LED is usually operated and is emitting light at the manufacturers rating.
- The forth area is where an LED is operated beyond it's operating limits, is probably glowing very brightly but alas for only a short time before the magic smoke inside escapes and it won't operate in this area it burns out because too much current flows through it.

Note that the If/Vf curve or operating curve of the LED is a 'non-linear' curve. That is, it is not a straight has a bend or kink in it.

Lastly this diagram is for a typical 5mm red LED designed to operate at 20mA. Different LEDs from different manufacturers have different operating curves. For example in this diagram at 20mA the forward voltage of the LED will be approximately 1.9V. For a blue 5mm LED at 20mA the forward voltage might be 3.4V. For a high power white luxeon LED at 350mA the forward voltage might be around 3.2V. Some LEDs packages might be several LEDs in series or in parallel, changing the Vf/If curve again.

Typically a manufactuer will specify an operating current which is safe to use the LED at, and the forward voltage at that current. Usually (but not always) you get a graph similiar to below in the datasheet. You need to look at the datasheet for the LED to determine what the forward voltage is at different operating currents.

Why is this graph so important? Because it shows that when a voltage is across the LED, the current that will flow will be according to the graph. Lower the voltage and less current will flow.....and the LED will be 'off'. This is part of the theory of charlieplexing, which we'll get to in the next step.

Step 2: The Laws (of Electronics)

Still not yet at the magic of charlieplexing yet....we need to go to some basics of electronics laws.

The first law of interest states that the total voltage across any series of connected components in an electrical circuit is equal to the sum of the individual voltages across the components. This is shown in the main diagram below.

This is useful when using LEDs because your average battery or microcontroller output pin will never be exactly the right voltage to run your LED at the recommended current. For example a microcontroller will typically run at 5V and it's output pins will be at 5V when on. If you just connect an LED to the output pin of the micro, you'll see from the operating curve in the previous page too much current will flow in the LED and it will get hot and burn out (probably damaging the micro as well).

However if we introduce a second component in series with the LED we can subtract some of the 5V so that the voltage left is just right to run the LED at the proper operating current.

This is typically a resistor, and when used in this way is called a current limiting resistor. This method is used very commonly and leads to what is called 'ohms law' named after Mr Ohm.

Ohms law follows the equation V = I * R where V is the voltage that will appear across a resistance R when a current I is flowing through the resistor. V is in volts, I is in amps and R is in ohms.

So if we have 5V to spend, and we want 1.9V across the LED to get it to run at 20mA then we want the resistor to have 5-1.9=3.1V across it. We can see this in the second diagram.

Because the resistor is in series with the LED, the same current will flow through the resistor as the LED, ie 20mA. So rearranging the equation we can find the resistance we need to make this work.

V = I * R
R = V / I
substituting the values in our example we get:
R = 3.1 / 0.02 = 155ohms
(note 20mA = 0.02Amps)

Still with me so Now look at diagram 3. It has the LED sandwiched between two resistors. According to the first law mentioned above, we have the same situation at the second diagram. We have 1.9V across the LED so it is running according to it's spec sheet. We also have each resistor subtracting 1.55V each (for a total of 3.1). Adding the voltages together we have
5V (the microcontroller pin) = 1.55V (R1) + 1.9V (the LED) + 1.55V (R2) and everything balances out.
Using ohms law we find the resistors need to be 77.5 ohms each, which is half the amount calculated from the second diagram.

Of course in practice you'd be hard pressed to find a 77.5ohm resistor, so you'd just substitute the nearest available value, say 75ohms and end up with a little more current in the LED or 82ohms to be safe and have a little less.

Why on earth should we be doing this resistor sandwhich to drive a simple LED.....well if you have one LED it's all a bit silly, but this is an instructable on charlieplexing and it comes in handy for the next step.

Step 3: Introducing 'complementary Drive'

Another name that is more accurate to describe 'charlieplexing' is 'complementary drive'.

In your average microcontroller you can in firmware tell the micro to set an output pin to be either a '0' or a '1', or to present a 0V voltage at the output or a 5V voltage at the output.

The diagram below now shows the sandwiched LED with a reversed partner....or a complement LED, hence complementary drive.

In the first half of the diagram, the micro is outputting 5V to pin A, and 0V to pin B. The current will thus flow from A to B. Because LED2 is oriented backwards to LED1 no current will flow through it and it will not glow. It's what is called reverse biased. We have the equivalent of the situation in the previous page. We can basically ignore LED2. Arrows show the current flow.

An LED is essentially a diode (hence Light Emitting Diode). A diode is a device that allows current to flow in one direction, but not in the other. The schematic of an LED sort of shows this, current will flow in the direction of the arrow......but is blocked the other way.

If we instruct the micro to now output 5V to pin B and 0V on pin A we have the opposite. Now LED1 is reverse biased, LED2 is forward biased and will allow current flow. LED2 will glow and LED1 will be dark.

Now might be a good idea to look at the schematics of the various projects mentioned in the introduction. You should see a whole lot of these complementary pairs in a matrix. Of course in the example below we are driving two LEDs with two microcontroller could say why bother.

Well the next section is where we get to the guts of charlieplexing and how it makes an efficient use of a microcontrollers output pins.

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'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!

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.

Step 5: Tri-states (not Tricycles)

In the previous step we mentioned a microcontroller can be programmed to output a 5V voltage or a 0V voltage. To make the charlieplex matrix work, we select two pins in the matrix, and disconnect any other pins.

Of course manually disconnecting the pins is a bit difficult to do, particularly if we are scanning things very quickly to use the persistance of vision effect to show a pattern. However a microcontroller output pins can also be programmed to be input pins as well.

When a micro pin is programmed to be an input, it goes into what is called 'high-impedence' or 'tri-state'. That is, it presents a very high resistance (of the order of megaohms, or millions of ohms) to the pin.

If there is a very high resistance (see diagram) then we can essentially regard the pin as being disconnected, and so the charliplex scheme works.

The second diagram shows the matrix pins for each combination possible to illuminate each of the 6 LEDs in our example. Typically a tri-state is denoted by an 'X', 5V is shown as a '1' (for logical 1) and 0V as a '0'. In the micro firmware for a '0' or '1' you'd program the pins to be an output and it's state is well defined. For tri-state you program it to be an input, and because it's an input we don't actually know what the state may be....hence the 'X' for unknown.

Although we might allocate a pin to be tri-state or an input, we don't need to read it. We just take advantage of the fact an input pin on a microcontroller is high impedence.

Step 6: Some Practical Matters

The magic of charlieplexing relies on the fact the individual voltage presented across multiple LEDs in series will always be less than that across one single LED when the single LED is in parrallel with the series combination. If the voltage is less, then the current is less, and hopefully the current in the series combination will be so low that the LED will not light.

This isn't always the case however.

Lets say you had two red LEDs with a typical forward voltage of 1.9V in your matrix and a blue LED with a forward voltage of 3.5V (say LED1=red, LED3=red, LED5=blue in our 6 LED example). If you lit up the blue LED, you would end up with 3.5/2 = 1.75V for each of the red LEDs. This may be very close to the dim operating area of the LED. You might find the red LEDs will glow dimly when the blue is illuminated.

It is a good idea therefore to make sure the forward voltage of any different coloured LEDs in your matrix are roughly the same at the operating current, or else use the same coloured LEDs in a matrix.
In my Microdot/Minidot projects I didnt have to worry about this, I used high efficiency blue/green SMD LEDs which fortunately have much the same forward voltage as the reds/yellows. However if I implemented the same thing with 5mm LEDs the result would have more problematical. In this case I would have implemented a blue/green charlieplex matrix and a red/yellow matix seperately. I'd have needed to use more pins....but there you go.

Another issue is to look at your current draw from the micro and how bright you want the LED. If you have a big matrix, and are rapidally scanning it, then each LED is on for only a brief time. This it will appear relatively dim compared to a static display. You can cheat by increasing the current through the LED by reducing the current limiting resistors, but only to a point. If you draw too much current from the micro for too long you'll damage the output pins.
If you have a slowly moving matrix, say a status or cyclon display, you could keep the current down to a safe level but still have a bright LED display because each LED is on for a longer time, possibly static (in the case of a status indicator).

Some advantages of charlieplexing:
- uses only a few pins on a microcontroller to control many LEDs
- reduces component count as you don't need lots of driver chips/resistors etc
Some disadvantages:
- your micro firmware will need to handle setting both voltage state and input/output state of the pins
- need to be careful with mixing different colours
- PCB layout is difficult, because the LED matrix is more complex.

Step 7: References

There are lots of references about charlieplexing on the web.
In addition to the links at the front of the article, some of them are:

The original article from Maxim, this has a lot to say about driving 7 segment displays which is also possible.

A wiki entry

The Instructables Book Contest

Participated in the
The Instructables Book Contest