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 again......ie 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 line...it 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.

<p>Excellent tutorial on Charlieplexing. You explained it so well that even a guy like me could fully understand it. I appreciate you taking the time to publish this Instructable, not to show off your project, but further the understanding of others. Great work.</p>
Is it possible to use diodes instead of LEDs, and get logic signals from a charliplexed matrix?
I really don't know why someone would build a Manual switched 20 LED charlieplexed box, But I did!
that looks really cool!
hi<br>I would like to make a chain of LED lights for the x-mas tree out of it, pulsing and blinking in different modes. I also want to use 6 pins to be able to use 30 LED's in a total lenght of 3 meters...<br>Will this work? or will the LED's are too dim?
Thank you so much for posting this! I am working on my electrical engineering senior design project which is going to be a bicycle with a lighting system along with other features. I am looking into Charlieplexing the taillight which is made up of 3 premade LED matrices.<br><br>My main question is, what is the actual time that each LED is lit up? I understand that the human eye takes in a new image 25 time per second, so I'm wondering how long should each LED stay lit for.<br><br>I'm also concerned that with the LED matrix I'm using, I won't actually be able to wire all of them up in the way that you've shown. This is the part I'm using: http://www.futurlec.com/LED/LEDM88RGCA.shtml<br><br>Check out http://litebike.info for more information about our project!<br><br>Thanks again.
Simply an excellent article. Can you confirm that we cannot turn on all LED's in the matrix at the same time using this method? Thanks.
You should only keep one LED on at a time if you have no buffering on your micro, or else the current draw will exceed the micro design tolerances.<br><br>However if you read the front page, there is quite a thread on lighting several LEDs at once with a charlie matrix. It is possible, but you should really buffer the control lines.<br><br>Check out the comments on this and follow the links back to the bloke who put me right in this regard.....he's got some great examples of more advanced charliplexing on his pages and probably a better explanation than I could have done.
Excellent explanation, very easy to follow and very helpful.
If you were to set pin A to high and pin C to low, led 5 would light up. But wouldnt leds 1 and 3 also light up (dimly) This is what happened to me when i tried the circuit
Yes...check the comments in the text. The amount would depend on the type of LEDs, their forward voltages, minimum operating current and the voltage/current you are putting across the grid.<br>I suspect either you have some really cool low current LEDs, the voltage is too high or the current limit resistors being used are too small.<br>
hey, if instead if disconnecting pin B you will put it in HIGH you'll get the same effect. current wont go through LED 1 due to zero voltage difference. i say that because i wonder how can you disconnect and reconnect pins in the microcontroller? you can either put it on 1 or 0 no?
Good question. That is addressed in the next &quot;step&quot;, &quot;Tri-states&quot;.
Typo: In the 6th paragraph, last sentence,<br><br> // With 5V on pin B and 0V on pin A LED1 will glow. <br><br>should be &quot;LED2&quot;, right?<br><br> - rob
Yep, thanks, should be updated now.
A most excellent article. Couldn't be more clear.
OK. I understand the theory completely, even the math for computing how many leds can be controlled with x many io pins. If that schematic for a charlieplex matrix were expanded, it would be fractal. It's pretty cool actually. Now to turn theory into practice. I'm trying to make a charlieplexed led cube that's 4x4x4 (64 leds) which could theoretically be controlled with 9 io pins, but I can't wrap my head around mapping a 2d fractal charlieplex schematic into a 3d circuit. I already have all the wows done, with all the cathodes on a row connected and the anodes unconnected for now. How do i complete it? Any help would be appreciated. I plan to control it with an arduino, and may make it into an instructible. (if people aren't tired of led cubes, that is) My backup plan is to just have a pin corresponding to each of the 4 levels and each of the 16 columns. Thats 20 io pins needed. The arduino has 13 regular digital pins, 6 analogue pins that can also be used as digital io, so it's 1 pin short. I'd have to leave something off.
Heres how I did mine; https://www.instructables.com/id/3D-LED-Charlieplex-Cube-from-Chrismas-Tree-Lights/ I had grids or meshs forming the charlieplex array. Note, my array was very sparse, so I was using more signal lines than I really needed. This was mostly for mechanical convenience than anything else. Phil
Ahhhh, I see what you did. I actually got the idea to use xmas tree leds from you, but I was constructing it according to this and didn't alternate the polarity of the leds with the layers, which I think is the key to your method. No matter, I have plenty of xmas lights and plenty of time. I can make 2 cubes (or one giant stack!) There might also be a way to charlieplex one or two places in my configuration. I only need to reduce the number of io pins by 1 to work with the ardiuno. Thanks for your help, and how's your next cube with the &quot;proper charlieplex driving scheme&quot; coming along? Try making a template to put your leds in, then overlay the chicken wire or whatever (I used paperclips) and solder it in. That would be a good way to get the leds and their support structure as exact as possible.
Glad to be any help. I honestly haven't had time for any more charlieplexing cubes....although I have been thinking of alternatives....why not have a hex structure, each LED is in the vertex of a 3d hex shape rather than a cube so they are all equidistant.....a bit different (more net interest), plus higher LED density. Another alternative may be to do away with the lattice (cube or hex) altogether. Instead have a charliplexed string driven from below without any cross connections at all. Eg three pins, gives 6 LEDs, 4 gives 12 LEDs high. Then just arrange the strings vertically into whatever shape you want, cube, cylinder, sphere etc. You could also make a charlieplex chandelier if they hang freely. You can be flexible, test as you go and build as you go as well. This would probably require more io pins, I've been wracking my brain how to share io between strings on this setup. Note charlieplexing with fewer io pins means lower individual brightness because only one LED can be easily lit at a time (scrummage through the comments below, there is a solution for making several lit at a time...albeit at an increase in wiring complexity). More io pins used on a sparse charlieplex matrix increases the brightness. Good luck, please experiment and if you have time write up an instructable (linking to any reference sources of course) for others to enjoy and learn from. I've gotten quite a buzz from all the happy campers who've read this instructable and learnt something. phil
I like the hex idea-sortof like an led ball or sphere. As for the string idea, check out <a href="https://www.instructables.com/id/CharliePlexed-LED-string-for-the-Arduino/" rel="nofollow">this </a>project for charlieplexed string of 12.<br> <br> If and when I make an instructable, I'll be sure to refer to yours. I'm having problems getting my cube to work with my arduino though. When I test it with a battery, connecting positive and negative to the correct wires, the correct led lights up, but when I use the ardiuno, some leds work fine, but others will not light up, or have other leds light up with them. If the cube works, then it must be a problem with my arduino or with my program.<br> <br> Is it possible that there's some sort of interference or something between the pins that makes them not behave as expected?<br> <br> Here's my code (sorry if formatting is undone.)<br> <br> It's pretty well commented. The cube is set up like yours, with alternate rows as complimentary drives. I use all the io pins except 13 and 19 (analogue 5).&nbsp; T light up an led first disconnect all pins (set as input), then set it's poll and level as input and apply voltage and ground (digital out HIGH/digital out LOW) appropriately.<br> <br> /*<br> This program controls a 4x4x4 semi-charlieplexed LED cube using 18 io pins<br> Alternate rows of the cube have leds with opposite poles, and are complimentary drives.<br> pins are refereed to by row, column, and layer (r,c,l), going front-to-back,left-to-right, and bottom-to-top.<br> The rows and columns are connected to poles that go through the layers.<br> */<br> <br> //array mapping led rows and columns to arduino pins<br> int gridPinMap[4][4]=<br> {<br> {0,1,2,3},<br> {4,5,6,7},<br> {11,10,9,8},<br> {14,15,16,12}<br> };<br> <br> //ints mapping cube levels to arduino pins<br> int levels01pin = 17;<br> int levels23pin=18;<br> <br> void setup(){<br> Serial.begin(9600);// initialize serial communications at 9600 bps:<br> allOff();<br> pinMode(13, INPUT);//we don't use 13 b/c it has a built-in resistor but just in case, disconnect it<br> }<br> <br> void loop(){//main loop<br> //this simple program turns each pin on for a second successivly<br> for (int r=0;r&lt;4;r++){<br> for (int c=0;c&lt;4;c++){<br> for (int l=0;l&lt;4;l++){<br> LEDON(r,c,l);<br> delay(1000);<br> allOff();<br> delay(1000);<br> }<br> }<br> }<br> }<br> <br> //this function disconnects all the io pins we are using, so nothing should be on at all.<br> void allOff(){<br> Serial.print(&quot;\nOFF&quot; );<br> //set everything to output by default<br> for (int r=0;r&lt;4;r++){<br> for (int c=0;c&lt;4;c++){<br> pinMode(gridPinMap[r][c], INPUT);<br> }<br> }<br> pinMode(levels01pin, INPUT);<br> pinMode(levels23pin, INPUT);<br> }<br> <br> //this function turns on the led at (r,c,l)<br> void LEDON(int r, int c, int l){<br> Serial.print(&quot;\n(&quot;);<br> Serial.print(r);<br> Serial.print(c);<br> Serial.print(l);<br> Serial.print(&quot;)&quot;);<br> <br> //if the led to light is in row 0 or 2, it's column needs a ground (digital out LOW), and the level needs a positive voltage (digital out HIGH).<br> //Vice-versa if on levels 1 or 3<br> <br> switch (l) {<br> case 0://bottom level<br> //disconnect the other two columns so no other pins go on<br> //pinMode(levels23pin, INPUT);<br> //send ground to column of pin<br> pinMode(gridPinMap[r][c], OUTPUT);<br> digitalWrite(gridPinMap[r][c],LOW);<br> //send voltage to row of pin<br> pinMode(levels01pin, OUTPUT);<br> digitalWrite(levels01pin,HIGH);<br> break;<br> case 1://first level up<br> //pinMode(levels23pin, INPUT);<br> pinMode(gridPinMap[r][c], OUTPUT);<br> digitalWrite(gridPinMap[r][c],HIGH);<br> pinMode(levels01pin, OUTPUT);<br> digitalWrite(levels01pin,LOW);<br> break;<br> case 2://second level up<br> //pinMode(levels01pin, INPUT);<br> pinMode(gridPinMap[r][c], OUTPUT);<br> digitalWrite(gridPinMap[r][c],LOW);<br> pinMode(levels23pin, OUTPUT);<br> digitalWrite(levels23pin,HIGH);<br> break;<br> case 3://top level<br> //pinMode(levels01pin, INPUT);<br> pinMode(gridPinMap[r][c], OUTPUT);<br> digitalWrite(gridPinMap[r][c],HIGH);<br> pinMode(levels23pin, OUTPUT);<br> digitalWrite(levels23pin,LOW);<br> break;<br> }<br> }<br>
Hi, I've got a few things on my plate today...so I haven't had a thorough read of the code sorry. So based on this: Some possibilities: - I'm guessing all the LEDs are the same colour, if not see the instructable about the issue where the forward voltage difference between colours can result in a problem. - check your digitalWrite and PinMode routines don't have the read/modify/write issue common to PICs (I'm not familiar with Arduino code ). In PICs a bit change on a port can sometimes be unreliable for various reasons, it's usually better to use a shadow port register and write the whole port register at once from this or use a r/c/l coordinate to locate a bit mask as in my code. Instead of setting each r/c/l to INPUT individually, just set the whole port register or registers to INPUT at once in the allOff routine. It'll be faster as well without an array sweep. If you check my code I stored the port register input/output bit masks as well as the output register bit masks and outputted those. Try changing the code to simply run through a couple of LEDs then stop. Then use a multimeter to see if multiple I/O pins are being set to low or high. You should only have two lines active either low or high, the rest should be tri-state or input. You can use a large pull up/pull down resistor sitting on the multimeter probe to check if the line is tri-state. If you can pull it up and down then it's set to an input. If it sticks at high or low, then it is an output. Phil
The LEDs are the same type, but they are ripped from an old string of xmas lights. They are bleached from sunlight, some are rusty, but they seem to work.<br> <br> The regular arduino language is a high level C-like library with functions for pin manipulation, but I did find <a href="http://arduino.cc/en/Reference/PortManipulation" rel="nofollow">this </a>page on directly manipulating pins . It should be helpfull. I'll try it in a bit.<br>
Ohhhh o.k! now i answered myself! putting pin B in an HIGH state will cause LED 3 to glow... the only solution is to disconnect pin B . thanks for this very comprehensive tutorial! Etay
:-) o.k now you actually answered my question about disconnect pins. but still, instead of disconnect you can put it on HIGH no?
I made a 12 led Charlieplexed with softwarebased 8bit PWM<br> <a href="http://www.youtube.com/watch?v=8apRaZQbv5w " rel="nofollow">http://www.youtube.com/watch?v=8apRaZQbv5w </a>
nice work, it's pretty hard getting good PWM on charlieplex unless you're throwing lots of cycles at it so hats off. I look forward to a fancied up instructable of your own detailing the innards of the software.
man.... that explained it so well. thanks!<br />
I&nbsp;just put this thing together!! Thanks a bunch. I&nbsp;was looking for a simple two digit display for a GPS speedometer.<br />
Excellent explanation. I'll definitely have to give this a try. <br />
i first i was slightly intimidated by ur explanation but after reading it, i feel like i can teach my electronics teacher this concept. thanks -drew
Thanks a lot!
It's such an easy to understand explanation, i totally graped the concept with one read :)
I have never understood this "charlieplexing" thing, but after reading this I understood it completely. 5 *'s
Thanks for the compliment...and to all the other people the instructable has helped and taken the time to comment.
I've got it, and I'm a complete new to to microcontrollers and electronics! Thanks for this great explanations! Alex
Thanks, about the easiest to understand explanation I have seen. R
In step two, don't you want 3.2-3.8 volts across an LED?
No, if you read closely you find I looked at my forward operating region for the LED from my graph and it was 1.9V, I wanted 3.1V shared between the two resistors. For combined red/green/blue LED matrices I made a note in step six to be careful about mixing different colours as they might have different forward voltages. In practice I found with the LEDs I had for my clock the green/blue LEDs lit up nicely at 1.9V, presumably because they were low power SMD devices or I was just luck ...so I didn't have to worry, however you do need to check this first. Other LEDs may well have higher forward voltages and in that case green/blue LEDs will need a separate matrix.
thank you, that was helpful
Thanks, this was really helpful.
Thanks for posting this!
Wonderful Work! Congratulations!
NICE!!! i havent been able to get an account 4 a while, but i hav been reading instructables for monthes. urs really helped me out with a problem i had 2 solve. AWSOME!!!
Please forgive the typo' in the previous post. Obviously I meant to say; <strong>This improves overall brightness by increasing duty cycle.</strong><br/><br/>I suspect there's a few chaps scratching their heads trying to figure out how to drive that 6 pin 30 LED matrix example in my previous post so I've attached a driver example in C.<br/><br/>Basically, it uses a 6 element array, one element for each column, with the least significant five bits in each element corresponding to the five LEDs in each column. Your <em>main</em> program simply sets or clears those bits to turn the corresponding LEDs on or off. The ISR driver uses periodic timer 2 interrupts and lights the LEDs in a single column during each interrupt interval so it takes six interrupt intervals to refresh the entire matrix.<br/><br/>The driver may not seem very intuitive at first but if you look at the LEDs on the RB5 row in the drawing (link) in my previous post and think of RB5 as a &quot;floating&quot; row, the method may make more sense.<br/><br/>Have fun. Regards, Mike<br/><br/><a rel="nofollow" href="http://www.wowway.com/~mmclaren/Charlie.txt">K8LH Charlieplexed Driver Example</a><br/><br/>
Hi Mike, I've had a very quick look at the code, can you answer a couple of questions though. If you are only using 6 interrupt intervals, then how can you provide an arbitrary pattern for 30 LEDs without illuminating several LEDs at once? This might be your purpose however, so please excuse me if that is the case. From what I understand you are doing a bargraph/bardot display. In that case, this is a reasonable method for such displays. For the minidot/microdot clocks however I wanted an arbitrary pattern for the 30LEDs (see pics on these instructables).....not sure that this would help in that case. If you have a look at the minidot/microdot code, I've had to have a lookup table for each LED and put the tris/port values out for each possible combination. I suppose the 'all possible combination' method is the generic method for pur charlieplex arrays, and this method is better suited as you've noted for seven segment displays or bar graphs where either only one LED is illuminated, or an ordered number of LEDs (ie a bar) is illuminated. I might be missing something, if you can do another more efficient algorithm for arbitrary patterns please post it. Again, I mentioned I've only had a quick look. On any particular iteration through the 6 interrupt cycles, do you have several LEDs on at once? If so, then you might have a problem with varying brightness if say on cycle one you want one LED, cycle two four LEDs etc because the cummulative forward voltage of several LEDs being illuminated will change the current. Thanks for adding to the instructable though, I think it illustrates the different cases for charlieplex arrays. Phil
&gt;&gt;&gt;&gt;&gt;RETRACTION&lt;&lt;&lt;&lt;&lt;&lt;&lt;<br/><br/>Sorry!!!!! I must do a bit of back paddling here. I didn't look at the solution you've come up with properly.<br/><br/>I didn't refer to the below circuit diagram when making the above comments.<br/>I see now how you can have several LEDs on at once and still have an arbitrary pattern.<br/><br/>Lets see if I can explain properly.<br/>First, because the diagram has no LEDs numbers lets assume a numbering convention where L11 means LED at row 1, column 1 , top left hand corner and L61 means LED at row 6, column 1 (bottom right hand corner. Of course LED11, LED22, LED33 etc don't actually exist in the matrix.<br/><br/>You can set an arbitrary pattern for column 1, eg L21, L31, L41 by setting tris/port values tris=xx110000 and port=xxx10001 (where x means don't care).<br/>This sets RB0 to high to turn on the column driver, and RB1,2,3 to LOW to turn on LEDs L21,L31 and L41. RB4 and RB5 are tri-state. <br/>This would be the first interrupt cycle.<br/><br/>Next pattern for column 2 eg L21, L23, L26 by setting<br/>tris=xx010100 and port=xx0x0x10<br/><br/>and so on. You can see that for each column in your arrangement there will be the same number of zeros in the port setting as the number of LEDs in that column you want to illuminate, plus a 'one' to set the output column driver. In the tris, there will be the number of desired LEDs+1 zeros to enable the output lines (all others to tri-state).<br/><br/>Apologies for jumping the gun before....this is a great method for reducing the number of interrupt cycles to control the array. With the extra time saved, PWM can be used to do brightness control.<br/><br/>My original concern was that the current sinking capability of the micro wasn't enough to handle several LEDs at one, however I can see now that each current sink line, only sinks one LED worth of current, and the transistors source the needed current.<br/><br/>Hats off....thanks for helping the charliplex community out. I can see the validity of this method now. You do need extra components....but that could be handled by a single transistor array chip (eg ULN 2800 for PNP drivers, ULN2803 of NPNs if you arrange it the other way).<br/><br/>When I extract my finger, I'll be using this method on my little LED cube. I was worried about getting PWM brightness control for a 5x5x6=125 LED array.<br/><br/>Phil<br/><br/>
You've almost got it Phil. The PORT reg' can only have a single '1' bit at any time (xx100000, xx010000, xx001000, xx000100, xx000010, or xx000001) to turn on a single column driver transistor. That's the purpose of the <strong>colpos</strong> ring-counter variable in the driver. We also use this '1' bit in <strong>colpos</strong> to determine when an individual led bit in our display array variable matches one of those invalid col/row positions (rb0/rb0, rb1/rb1, etc.) and needs to be &quot;floated&quot; onto the RB5 &quot;float&quot; row. If you physically slide those RB5 &quot;float&quot; row LEDs into the holes in the matrix above then your display array will match your LED numbering scheme.<br/><br/>Yes, you can still PWM the LEDs with this method (with improved duty cycle).<br/><br/>Yes, you could use a UDN2981 sourcing driver IC for CA columns or a ULN2803/2804 sinking driver IC for CC columns but adding an IC almost defeats the purpose of Charlieplexing (grin). I guess for some reason I don't think adding 5 transistors is 'cheating' (grin).<br/><br/>BTW, during my Charlieplexing experiments several years ago I observed higher brightness when the PIC pins sink current to LED cathodes rather than source current to LED anodes and so that's why you'll notice common anode column drivers in most of my projects.<br/><br/>Have fun. <br/><br/>Mike, K8LH (Michigan, USA)<br/><br/>
what simulator are the product pictures from?
Those drawings are Microsoft Excel spreadsheets (using the drawing tools). Mike

About This Instructable




Bio: update later
More by rgbphil:3D LED Charlieplex Cube from Chrismas Tree Lights Charlieplexing LEDs- The theory Minidot 2 - The holoclock 
Add instructable to: