Monday, April 6, 2015

65. Driving Charlieplexed LEDs with the Raspberry Pi

33,000 page views!!
Do you remember a couple of years ago, when I built a set of Charlieplexed LEDs? Here's the post:http://smokespark.blogspot.co.uk/2013/08/32-charlieplexing-4-x-3-led-array.html
I was driving them with an Arduino and I thought I would try it with the Pi, as I'm on an exploration of the Pi's GPIO capabilities.

Here's a video of the result:

It's interesting that nothing is connected to GND - the four wires run to the four LED terminals from GPIO18, GPIO23, GPIO24 and GPIO25.  Current flows in different directions, depending on whether the software has set the ports to INPUT or OUTPUT, and if set to OUTPUT, whether that is set to HIGH or LOW..

As I have recently been interested in Python Graphical User Interfaces (GUIs), I also wrote a GUI for this exercise.  I have a grid of buttons, labelled 1 to 12, each one controlling its own personal LED, and which turn red when the corresponding LED is on.  So any one of the LEDs can be individually energised.

This also allows loops to be created, with sequences of lighting up the LEDs.  I made two further buttons, Loop - no delay and Loop - 0.1s delay, which both light up each LED in turn, and then continue this loop for a pre-set number of iterations.

The iterations can have a delay after lighting each LED, and this delay can be made zero, so that the lighting sequence is carried out as fast as the Pi can manage it.  With a zero delay, all the LEDs appear to light up together, albeit dimmer than one lit on its own, with only a slight flicker.  This is how persistence of vision (POV) can be used to fool the brain into thinking that they are all illuminated at the same time. The image below shows all the buttons in my GUI, which include a Clear button, turning all the LEDs off, and a Quit button, which closes the program down properly:
  
and here's a photo showing LED no 4 lit up as the GUI above indicates.
Here's the code:

Here is a reminder of how Charlieplexing works with a very simple example of 2 LEDs:
Pairs of LEDs are connected in anti-parallel (as opposed to parallel) and when the current is driven in one direction (Pin 1 to Pin 2 in the diagram below), one diode allows current to pass (LED1 lights up) while LED2 does not.  Then when the current is reversed - ie from Pin 2 to Pin 1, LED1 does not allow current to pass, but LED2 does, and LED2 lights up this time.


File:Complementary Drive.png

So, by alternating the direction of relative polarity of Pin 1 and Pin 2, LED1 or LED2 can be made to glow.  

If 4 GPIO ports are available, then a 4 x 3 array of LEDs can be handled.  In general, if n ports are available, a Charlieplexed array of n x (n-1) LEDs can be driven.

This is the diagram I used to show the wiring of the 12 LEDs.  You will need to read GPIO18, 23, 24 & 25 for ArduinoPin2, 3,4 and 5.


Here's a reminder of the truth table for 4 x 3 Charlieplexed LEDs:


Pins:




LEDs:












GPIO:
18
23
24
25

1
2
3
4
5
6
7
8
9
10
11
12



















L
H
i
i

1
0
0
0
0
0
0
0
0
0
0
0

H
L
i
i

0
1
0
0
0
0
0
0
0
0
0
0

i
L
H
i

0
0
1
0
0
0
0
0
0
0
0
0

i
H
L
i

0
0
0
1
0
0
0
0
0
0
0
0

i
i
L
H

0
0
0
0
1
0
0
0
0
0
0
0

i
i
H
L

0
0
0
0
0
1
0
0
0
0
0
0

L
i
H
i

0
0
0
0
0
0
1
0
0
0
0
0

H
i
L
i

0
0
0
0
0
0
0
1
0
0
0
0

i
L
i
H

0
0
0
0
0
0
0
0
1
0
0
0

i
H
i
L

0
0
0
0
0
0
0
0
0
1
0
0

L
i
i
H

0
0
0
0
0
0
0
0
0
0
1
0

H
i
i
L

0
0
0
0
0
0
0
0
0
0
0
1

The pins are set to either OUTPUT with a 3V3 potential (H) or zero V condition (L) or as INPUT (i).  Making a port an INPUT effectively turns it off in this case. The fact that there are 3 possibilities for port conditions is known as tri-state logic.

To address LED 1, GPIO18 needs to be set as an OUTPUT, in the LOW state, GPIO23 also needs to be set as an OUTPUT, but in the HIGH state.  Both GPIO24 and 25 need to be set as INPUTS.  Then to light up LED2, GPIO18 and 23 need to swap their states, and so on.

Thanks, Charlie! (Reference HERE).