Instructables

Controlling 20 Led's from 5 Arduino pins using Charlieplexing

Picture of Controlling 20 Led's from 5 Arduino pins using Charlieplexing
Today, I'll show you how to control 20 LED's from just 5 Arduino pins. I'm working on a project where I need to control 15 LED's, 3 buttons, 3 seven segment displays and an RGB orb from one Arduino. Now if you work it out, you would see that without some clever multiplexing, I would need 15 Digital outputs for the LED's, 3 inputs for the buttons, 21 digital outputs for the seven segment displays and 3 PWM outputs for the RGB orb.

My Arduino doesn't have 40(15+21+3) outputs and 3 inputs, so I either need to drop features (Which doesn't sound like the fun thing to do), buy an Arduino Mega (Where's the fun in that) or I need to find a clever way to make it work. While I'm usually lazy, doing the impossible sounds like a lot more fun (Ok, the improbable then...)

I've already managed to hook 5 buttons to one analog input and documented that here:
http://www.instructables.com/id/How-to-access-5-buttons-through-1-Arduino-input/

By using Charlieplexing, you can hook up n*(n-1) LED's to n microcontroller pins. I'll go through the theory in the next step, but from this you can see that I can hook up 20 LED's on 5 pins or 12 LED's on 4 pins, which would be a great start for actually making this all work on 1 Arduino.

If you search Instructables (and Google for that matter), You'll find lots of theoretical explanations of how Charlieplexing work, and you'll even find some examples of Charlieplexed LED's running on Arduino's. The problem is that the code on these examples are generally not explained very well, and are usually very tightly tied to the exact hardware setup that the author used. This makes it an exercise in futility and frustration to try and make it work on your own projects.

I will show you exactly how this works, how to set it up with as many LED's as you would like in as painless a manner as possible (Although it gets horribly complicated if you go to more than about 30 LED's), how to code it so it works for you, and which problems I had to overcome in the process.

 
Remove these adsRemove these ads by Signing Up
AlexLPD9 months ago

Hey hey... this is a nice trick!!! thanks for sharing it !!!! ... know leds do something awesome! =)

sephirothx66610 months ago
This is pretty cool, i successfully hooked it up using a simple two pin and then a three pin. I'd say i can confidently handle this on a larger scale now. You are right though this does take up a lot of space, but at least it uses less pins. Thanks!
Great and clear I'ble! Thanks for sharing.

Could I use Charlieplexing as you described it, with "AnalogWrite" (instead of DigitalWrite).? I would like to change the intensity of a large numer of LED's and Arduino has a limited amount of pins to do this (pins 3, 5, 6, 9, 10 , 11).
sonicase1 year ago
this helped some, thanks
ardor3 years ago
I'm struggling to understand your code. Would you mind going through Display_Frames.pde and commenting so that we could figure out what each section a little easier?

I'm new to coding and it would REALLY help.

Please :-}
riaancornelius (author)  ardor3 years ago
I'm a bit swamped at the moment, but I'll try to help you out with this on the weekend.
Thanks! I appreciate you finding time to help :-)
ardor3 years ago
I'm very confused about something. In the image above you have a red and a black wire coming from what I assume are the +5 power and ground pins. In neither of the images below do you show anything about a power and a ground. Did I miss something?

To make things easier for me when I set up my Charileplexed matrix I did it exactly as you see the first image.

Into a piece of wood I drilled two sets of five holes about 5 inches apart. Then I strung wire between those holes and soldered the LEDs across them as you showed in the first example. I soldered resistors to the end of the wires. I checked the matrix by running power across each wire. The LEDs lit up correctly and worked fine. Then I plugged them into pins 1, 2 , 3 ,4 and 5.

However when I loaded your program, which I have to admit I have trouble following, nothing happened... Where did I go wrong?
layout.jpgLayout2.jpg
riaancornelius (author)  ardor3 years ago
The power from the Arduino to the breadboard is just a habit I have... It's not actually hooked up to something in the Charlieplex circuit.

The second problem you have is that the pins in the software doesn't correspond to the way you've wired them up to the Arduino. This is a bit confusing, and I don't specifically mention it... I'll fix that in the Instrucatable.

If you look at the code, you'll see this at the top:
#define A 8
#define B 9
#define C 10
#define D 11
#define E 12

That defines what pins connect the Arduino to the Charlieplex. So just change your connections to connect to pins 8-12 and it should work fine.
Duh, I looked at the code and understood that much. I don't know why that didn't occur to me. If it was a snake it would have bit me...

http://www.youtube.com/watch?v=YBJwUTHt8Do

I had to change this part:

int c[5][4][2] =
{
{ {A, B}, {A, C}, {A, D}, {A, E} },
{ {B, A}, {B, C}, {B, D}, {B, E} },
{ {C, A}, {C, B}, {C, D}, {C, E} },
{ {D, A}, {D, B}, {D, C}, {D, E} },
{ {E, A}, {E, B}, {E, C}, {E, D} }
};

To this:

{
{ {A, B}, {B, A}, {B, C}, {C, B} },
{ {C, D}, {D, C}, {D, E}, {E, D} },
{ {A, C}, {C, A}, {B, D}, {D, B} },
{ {C, E}, {E, C}, {A, D}, {D, A} },
{ {B, E}, {E, B}, {A, E}, {E, A} }
}

So that my LEDs would light in order.

I know this may be asking a bit much but if I wanted to create different patterns instead of just cycling through each LED what might be the best way?
riaancornelius (author)  ardor3 years ago
The sketch in step 7 lets you set up custom displays. and display them. It's not the easiest code to customize, but I haven't come up with anything better yet.

The 'int frames[][5] = {..}' array defines the patterns to display, and you can display the pattern using the 'void display( int frame[5], int duration ){...}' method.
riaancornelius (author)  riaancornelius3 years ago
Ahh, crap... I had the wrong sketch attached to step 7... The new sketch I've attached to step 7 is what's running in the video.
Sadly that code is way above my abilities at the moment. I have however figured out how to make three different types of light animations the hard way. Hey I'm just learning :-)

Below is the first "frame" of animation code that would start out in the middle of a strip of 20 charlieplexed LEDs then go out to both ends and back into the middle again.

int ledPin1 = 8;
int ledPin2 = 9;
int ledPin3 = 10;
int ledPin4 = 11;
int ledPin5 = 12;

void setup() {
}

void loop() {
int a;
for( a = 0; a < 50; a++ )
{
pinMode(ledPin1, INPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, INPUT);
pinMode(ledPin4, OUTPUT);
pinMode(ledPin5, INPUT);
digitalWrite(ledPin2, HIGH);
digitalWrite(ledPin4, LOW);
delay(.5);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, INPUT);
pinMode(ledPin3, OUTPUT);
pinMode(ledPin4, INPUT);
pinMode(ledPin5, INPUT);
digitalWrite(ledPin3, HIGH);
digitalWrite(ledPin1, LOW);
delay(.5);
}


Now I just have to figure out how to add a potentiometer to vary the speed of the animation and either a rotary switch or a series of switches that would enable me to change to the different patterns...

Thanks for the head start. :-)
shaggs313 years ago
This is so cool. I have never understood Charlieplexing until now. Thanks for posting this. You got my vote!
riaancornelius (author)  shaggs313 years ago
Thanks for the thumbs up. I'm glad I could help.