Charlieplexing the Arduino

45,192

128

21

Introduction: Charlieplexing the Arduino

About: Mech Eng

This Instructable shows how to implement Charlieplexing, a technique that will allow you to control many more LEDs than you have pins. This shows how to configure 6 LEDs and use only 3 pins. You can extend this to many more LEDs. The exact number of LEDs that you can control with a given number of pins is given by n^2 - n where n is the number of pins used (i.e.,  3^2 - 3 = 9 - 3 = 6 LEDs using only n = 3 pins).

Step 1: Charlieplexing the Arduino

Charlieplexing not only takes advantage of the two states that we normally change, HIGH and LOW, but also uses a third state by changing between OUTPUT and INPUT modes, which affects internal resistors on the Arduino.

For Charlieplexing 6 LEDs, we will setup six LEDs (L1 thru L6) and need to configure three pins (I chose Pins 13, 12, and 11) for each LED.

For each LED (L1, L2,....,L6), we need to set the pinMode (INPUT or OUTPUT) and the pin state (HIGH or LOW)

H: pinMode = OUTPUT; state HIGH
L: pinMode = OUTPUT; state LOW
Z: pinMode = INPUT; state LOW

For example, to configure LED L2, set pin 13 to H, pin 12 to L, and pin 11 to Z:

Pin13    
pinMode(LED_1, OUTPUT);  //pinMode
digitalWrite(LED_1, HIGH);   // state

Pin12    
pinMode(LED_2, OUTPUT);  // pinMode
digitalWrite(LED_2, LOW);   //state

Pin11    
pinMode(LED_3, INPUT);  // pinMode
digitalWrite(LED_3, LOW);  // state

Step 2: Configure Hardware

Connect two LEDs to each Pin; be sure to flip the LED so the Cathode (+) and Anode (-) are on the correct Pins. The cathode (+) has the longer leg and is shown in the schematic as the leg with the bend in it.  

Step 3: Configure the Software

1 Person Made This Project!

Recommendations

  • Home Cooked Speed Challenge

    Home Cooked Speed Challenge
  • Robots Contest

    Robots Contest
  • Audio Challenge

    Audio Challenge

21 Comments

0
IskraT
IskraT

Question 2 years ago on Step 2

I have basic 5mm blue leds and I want to use charlieplexing for connecting 6 of them on 3 pins. What resistors should I use?

0
tsant
tsant

Answer 2 years ago

Approximately 100 ohm.

0
timmins.jack595
timmins.jack595

Question 3 years ago

I'm pretty sure the image is incorrect. I may be though. I think you need to add a wire from R3 to 8(B-D)

0
ZentOx
ZentOx

4 years ago

You really helped me understand charlieplex better. Thanks!!

0
superqi7
superqi7

6 years ago on Introduction

How do you make multiple LEDs light up at the same time?

0
muscipula
muscipula

Reply 4 years ago

Just reduce the delay time, even remove it then you will see the LEDs are all lit. You then figure out the code that cycles the lights either switching on or off and so on.

Andrew

0
neilzzoferrari
neilzzoferrari

Reply 5 years ago

You can use a short delay and light up LEDs one by one. If the delay is right, you can use persistence of vision to make those LEDs glow. Mae separate function for each LED, and call those functions in a loop.

0
tsant
tsant

Reply 6 years ago on Introduction

Actually, I am not really sure. I found another Instructable that goes through the process. I would have to set this up again at home and go through this other tutorial line by line to figure it out. Hope this gets you on the right track. If not, let me know.

0
superqi7
superqi7

Reply 6 years ago on Introduction

Thank you very much, I will look at it and try it.

0
Artstep
Artstep

4 years ago

You have an incomplete image of the wiring - the connection between the cathode of L4 and anode of L6 is missing.

Other than that, all good it seems, thank you for the tutorial.

0
shutterbun
shutterbun

6 years ago on Introduction

Everything worked great, thanks for the tutorial, and especially for the breadboard image. Very helpful!

0
tsant
tsant

Reply 6 years ago on Introduction

Glad to hear it. Thanks!

0
IMFoolishTim
IMFoolishTim

7 years ago on Introduction

I couldn't get L6 to work, looking at the sketch you have:

//turn on LED L6 pinMode
(LED_1, OUTPUT);
digitalWrite(LED_1, HIGH);
pinMode(LED_2, INPUT);
digitalWrite(LED_2, LOW);
pinMode(LED_3, OUTPUT);
digitalWrite(LED_3, HIGH);

I needed to change LED_3 to LOW:
digitalWrite(LED_3, LOW);

Then it worked.

Tim

0
tsant
tsant

Reply 7 years ago on Introduction

Updated sketch on github.

thanks.

0
tsant
tsant

Reply 7 years ago

I knew that a pin can source current but I didn't know until now that a pin other than ground pin can sink current. so in deed the connections to ground aren't required. I'll have to update the schematic. oops.

Question for you, I am a complete beginner. So this is probably a stupid question, in your schematic, following pin 13, I noticed there is no resistor for the LED, how will row 1 not fry? Any info would be much appreciated

0
rfisher11
rfisher11

8 years ago on Introduction

The schematic is correct but the wiring is wrong. L5 and L6 are reversed. Also, seems to make more sense to run the wires from pins 13, 12, and 11 through the resistors first (just like the schematic) and dispense with the ground wires which don't make sense in this set up anyway. Finally, if it is wired correctly according to the schematic, then the code is incorrect. This is what the loop should be:
void loop()
{
//turn on LED L1
pinMode(LED_1, OUTPUT); //row 1
digitalWrite(LED_1, LOW);
pinMode(LED_2, OUTPUT); //row 2
digitalWrite(LED_2, HIGH);
pinMode(LED_3, INPUT); //row 3
digitalWrite(LED_3, LOW);

delay(1000);

//turn on LED L2
pinMode(LED_1, OUTPUT); //row 1
digitalWrite(LED_1, HIGH);
pinMode(LED_2, OUTPUT); //row 2
digitalWrite(LED_2, LOW);
pinMode(LED_3, INPUT); //row 3
digitalWrite(LED_3, LOW);

delay(1000);

//turn on LED L3
pinMode(LED_1, INPUT); //row 1
digitalWrite(LED_1, LOW);
pinMode(LED_2, OUTPUT); //row 2
digitalWrite(LED_2, LOW);
pinMode(LED_3, OUTPUT); //row 3
digitalWrite(LED_3, HIGH);

delay(1000);

//turn on LED L4
pinMode(LED_1, INPUT); //row 1
digitalWrite(LED_1, LOW);
pinMode(LED_2, OUTPUT); //row 2
digitalWrite(LED_2, HIGH);
pinMode(LED_3, OUTPUT); //row 3
digitalWrite(LED_3, LOW);

delay(1000);

//turn on LED L5
pinMode(LED_1, OUTPUT); //row 1
digitalWrite(LED_1, LOW);
pinMode(LED_2, INPUT); //row 2
digitalWrite(LED_2, LOW);
pinMode(LED_3, OUTPUT); //row3
digitalWrite(LED_3, HIGH);

delay(1000);

//turn on LED L6
pinMode(LED_1, OUTPUT);
digitalWrite(LED_1, HIGH);
pinMode(LED_2, INPUT);
digitalWrite(LED_2, LOW);
pinMode(LED_3, OUTPUT);
digitalWrite(LED_3, LOW);

delay(1000);
}

0
tsant
tsant

8 years ago on Step 2

Image is now correct.

0
tsant
tsant

8 years ago

The GND isn't connected to the breadboard bus correctly; I'll upload new image shortly! Should be over one to connect all black GND Jumpers to Arduino gnd