Charlieplexing with the Raspberry Pi

I’ve always been interested in Charlieplexing, but never had any means to get into it.  When I got my Raspberry Pi, I figured it would make a great platform to learn how to Charlieplex.
There are already a ton of Instructables on Charlieplexing out there, so I won’t go into the theory behind it, or how it works.  This Instructable will instead focus on how I wired it up on my breadboard using 3 pairs of LEDs and 3 leads, then expanding it to 10 pairs of LEDs with 5 leads.

Charlieplexing with the Raspberry Pi
From a learning perspective, my objective was to learn about Charlieplexing.  How does it work, and how do you code it.

Step 1: A Pair of LEDs

Charlieplexing is all about wiring up LEDs in pairs between sets of two leads.  Each LED in the pair lights in a “different direction,” if you will.  This lets you run current from lead 1 to lead 2, and light one LED, then run current from lead 2 to lead 1 and light the other LED.

Wiring was going to be a mess, I figured, so I wanted my pairs of LEDs to be pretty compact.  I put the LEDs in my breadboard facing opposite directions, in the same columns, practically touching, with a resister wired right next to them.

Step 2: Wiring up the first three pairs

This image from wikipeida is a really good schematic of 3 pairs of LEDs Charlieplexed over 3 leads.
My wiring was very basic here, and I used short leads to keep it somewhat neat.  I tried to label the picture with all the wires, but it was a bit too much, and didn’t make things clear.  So here’s the wiring by color:
Orange: Lead 1 to Pair 1
Yellow: Pair 1 to Lead 2
Left Blue: Lead 1 to Pair 2
Straight Green: Pair 2 to Lead 3
Right Blue: Lead 2 to Pair 3
Wiggly Green: Pair 3 to Lead 3
Then I connected the Leads to the GPIO pins on my Raspberry Pi.  I just chose to start at Pin 1 and work my way down, taking the first three GPIO pins.  No other rhyme or reason for it.  This instructable has a nice diagram of the order of the GPIO pins, and what each is used for.

Step 3: The code and a video

Attached to this step is the Python code I created for the Charlieplexing.  It’s very commented, so take a look at it and it should be easy to follow.  One thing I do want to stress about it is the way I created the list of LEDs.  In most of the code I’ve seen, people manually list out each LED using something like this:

LEDS=[[1,2], [2,1], [1,3], [3,1], [2,3], [3,2]]

This works great, but is very static.  To add a fourth lead, you need to go in and extend the list manually.  This works well for a small number of leads, when you get to 10 leads, it’s 90 LEDs. I wanted a way to automatically generate the list of LEDs. (See my comments in the final step for more on this.)

So, I created a list of leads, based on their GPIO pin number on the Raspberry Pi, then ran the leads through two for loops to generate the list automatically:

# define an array of pins used as leads
charliePins=[7,11,12]
Charlieplexing with the Raspberry Pi Schemetic
# Define the array of LEDs.  This is normally done
# by defining each pair separately, but I wanted the code
# to be easy to expand, so I went with this method of
# cycling through the pins and creating the pairs.  It
# has the disadvantage of not making them in order for larger
# sets of pairs, but is easier to maintain, IMO.
charlieLEDS=[]
for i in range(0,len(charliePins)-1):
for j in range(i+1,len(charliePins)):
charlieLEDS.append([charliePins[i],charliePins[j]])
charlieLEDS.append([charliePins[j],charliePins[i]])

cp_play_3lead.py2 KB

Step 4: Overview

So far it’s all been very simple–almost too simple.  But Charlieplexing really is pretty simple once you get the hang of it.  Let’s take a minute, before we move on, to look back at where we’ve been.

We setup 3 pairs of LEDs, giving us a total of 6 LEDs.

We wired them up to three leads on the breadboard.

We connected the leads to the Raspberry Pi.

We wrote code to light the LEDs.

That’s all there really is to do.  You could modify the code to light the LEDs in a particular pattern if you wanted. My code does show three things to do–Light them one by one in order, light the top row, then the bottom row, and light them at random.  Maybe you could have it do a wave, or a light chase or something like that.  Pretty straight forward.

So what’s left to do?  Add more leads, and more LEDs, of course.

For more detail: Charlieplexing with the Raspberry Pi

Scroll to Top
Read previous post:
Raspberry pi arcade project

Hello their ''young'' follower, in this projects of mine I've will be telling you about one of my little inventions...

Close