# Charlieplexing LEDs- The theory

7 Steps

## 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.
Remove these ads by Signing Up
Vick Jr says: Aug 4, 2010. 3:44 PM
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.
rgbphil (author) in reply to Vick JrAug 4, 2010. 4:11 PM
Heres how I did mine; http://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
Vick Jr in reply to rgbphilAug 4, 2010. 5:56 PM
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 "proper charlieplex driving scheme" 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.
rgbphil (author) in reply to Vick JrAug 5, 2010. 5:04 PM
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
Vick Jr in reply to rgbphilAug 6, 2010. 7:58 AM
I like the hex idea-sortof like an led ball or sphere. As for the string idea, check out this project for charlieplexed string of 12.

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.

Is it possible that there's some sort of interference or something between the pins that makes them not behave as expected?

Here's my code (sorry if formatting is undone.)

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).  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.

/*
This program controls a 4x4x4 semi-charlieplexed LED cube using 18 io pins
Alternate rows of the cube have leds with opposite poles, and are complimentary drives.
pins are refereed to by row, column, and layer (r,c,l), going front-to-back,left-to-right, and bottom-to-top.
The rows and columns are connected to poles that go through the layers.
*/

//array mapping led rows and columns to arduino pins
int gridPinMap[4][4]=
{
{0,1,2,3},
{4,5,6,7},
{11,10,9,8},
{14,15,16,12}
};

//ints mapping cube levels to arduino pins
int levels01pin = 17;
int levels23pin=18;

void setup(){
Serial.begin(9600);// initialize serial communications at 9600 bps:
allOff();
pinMode(13, INPUT);//we don't use 13 b/c it has a built-in resistor but just in case, disconnect it
}

void loop(){//main loop
//this simple program turns each pin on for a second successivly
for (int r=0;r<4;r++){
for (int c=0;c<4;c++){
for (int l=0;l<4;l++){
LEDON(r,c,l);
delay(1000);
allOff();
delay(1000);
}
}
}
}

//this function disconnects all the io pins we are using, so nothing should be on at all.
void allOff(){
Serial.print("\nOFF" );
//set everything to output by default
for (int r=0;r<4;r++){
for (int c=0;c<4;c++){
pinMode(gridPinMap[r][c], INPUT);
}
}
pinMode(levels01pin, INPUT);
pinMode(levels23pin, INPUT);
}

//this function turns on the led at (r,c,l)
void LEDON(int r, int c, int l){
Serial.print("\n(");
Serial.print(r);
Serial.print(c);
Serial.print(l);
Serial.print(")");

//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).
//Vice-versa if on levels 1 or 3

switch (l) {
case 0://bottom level
//disconnect the other two columns so no other pins go on
//pinMode(levels23pin, INPUT);
//send ground to column of pin
pinMode(gridPinMap[r][c], OUTPUT);
digitalWrite(gridPinMap[r][c],LOW);
//send voltage to row of pin
pinMode(levels01pin, OUTPUT);
digitalWrite(levels01pin,HIGH);
break;
case 1://first level up
//pinMode(levels23pin, INPUT);
pinMode(gridPinMap[r][c], OUTPUT);
digitalWrite(gridPinMap[r][c],HIGH);
pinMode(levels01pin, OUTPUT);
digitalWrite(levels01pin,LOW);
break;
case 2://second level up
//pinMode(levels01pin, INPUT);
pinMode(gridPinMap[r][c], OUTPUT);
digitalWrite(gridPinMap[r][c],LOW);
pinMode(levels23pin, OUTPUT);
digitalWrite(levels23pin,HIGH);
break;
case 3://top level
//pinMode(levels01pin, INPUT);
pinMode(gridPinMap[r][c], OUTPUT);
digitalWrite(gridPinMap[r][c],HIGH);
pinMode(levels23pin, OUTPUT);
digitalWrite(levels23pin,LOW);
break;
}
}
rgbphil (author) in reply to Vick JrAug 6, 2010. 5:42 PM
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
Vick Jr in reply to rgbphilAug 7, 2010. 6:35 AM
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.

The regular arduino language is a high level C-like library with functions for pin manipulation, but I did find this page on directly manipulating pins . It should be helpfull. I'll try it in a bit.
lavert31 says: Jul 10, 2010. 1:24 PM
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
lavert31 says: Jul 10, 2010. 1:20 PM
:-) o.k now you actually answered my question about disconnect pins. but still, instead of disconnect you can put it on HIGH no?