Intro: Charlieplexing the Arduino
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:
pinMode(LED_1, OUTPUT); //pinMode
digitalWrite(LED_1, HIGH); // state
pinMode(LED_2, OUTPUT); // pinMode
digitalWrite(LED_2, LOW); //state
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
Upload Charlieplexing Sketch