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:
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.