3 Simple Ways to
Share What You Make

With Instructables you can share what you make with the world — and tap into an ever-growing community of creative experts.

PhotosPhotos

Share one or more photos of a project, recipe, or whatever you've made, quickly and easily.

Step by StepStep-By-Step

Share your step-by-step photos with text instructions of what you made so others can do it too!

VideoVideo

Share your how-to video. You'll need your embed code from a video site such as YouTube.

Controlling 20 Led's from 5 Arduino pins using Charlieplexing

Controlling 20 Led\
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
 

Step 1The parts list

20 x LED's - These need to have the same forward voltage and current requirements to work properly. If you use different types, more than one will probably turn on at the same time. In my case I used 3mm green and red LED's.

5 Resistors - I used 100 Ohm resistors (Brown Black Brown). The value depends on your resistors, but will probably be somewhere between about 75 and 150 Ohm. If the value is too big, your LED's will be dim. If they are too small, more than one LED will turn on at the same time.

An Arduino. This is the brains of the project. Well, actually, I'm the brains, but you get the idea... I'm using a Diecimila for this project.

Some wire to hook it all together (Actually, LOTS of wire).

« Previous StepDownload PDFView All StepsNext Step »
11 comments
Mar 1, 2011. 5:22 PMardor says:
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 :-}
Mar 3, 2011. 11:27 AMardor says:
Thanks! I appreciate you finding time to help :-)
Feb 13, 2011. 6:07 AMardor says:
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?
Feb 13, 2011. 12:17 PMardor says:
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?
Feb 16, 2011. 4:54 PMardor says:
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. :-)
Feb 15, 2011. 6:40 PMshaggs31 says:
This is so cool. I have never understood Charlieplexing until now. Thanks for posting this. You got my vote!

Pro

Get More Out of Instructables

Already have an Account?

close

All Steps Viewing
View all steps of an Instructable on the same page when you're a Pro Member.

Upgrade to Pro today!
3
Followers
3
Author:riaancornelius