Step 5: Tri-states (not tricycles)
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








































Visit Our Store »
Go Pro Today »




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;
}
}
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.