## Wednesday, May 9, 2012

### Designing a large charlieplex

I saw a new cool way to design a charlieplex, one which is easy and obvious, even for an extremely large LED matrix. This was buried in a post below where I blather on about my own problems, so I broke it out into a post that Google is more likely to find.

First, figure out how many lights you want. Then figure out how many pins you need. The formula is
N=(P)(P-1)=P2-P
where N is the number of lights you want and P is the number of pins you need. Find the smallest P for the N you want. You can use the quadratic formula, or just a table like this:

Number of pinsNumber of lights
10
22
36
412
520
630
742
856
972
1090
11110
12132
13156
14182
15210
16240
17272
18306
19342
20380

Now, in your favorite schematic program, draw one rail horizontally for every pin you need. Make sure to put a resistor on each rail in between the microcontroller and the grid we are about to draw.

I have an ATmega328 here, but any controller with sufficient pins is fine. I use P for "pin" for the rails that are connected to the controller, and Q in the grid because it comes after P. I number the grid lines according to my convenience. In this case notice that the P numbers do not line up with the "D" numbers for programming Arduino code.

On the controller, each pin used actually has four names. One is the ATmega name, PB0 or so on, saying that this is bit 0 of digital port B. One is the actual pin number 12, the next is the grid rail name P05, followed by the pin name in Arduino D8, which is used for digitalWrite(8,HI); etc.

I could have drawn the confusing mishmash of wires directly from the controller to the grid. I chose not to, since that mishmash is not essential to the design, but might look like it is. My (hypothetical) audience would say, "What is that complicated maze of wiring there, and how did you design that?" The point is, each P wire is drawn twice, once attached to a pin of the controller and once in the grid. The wires are named and labeled, and my design program keeps track of this so that the pins are properly connected in the board design. As it turns out, the pins are ordered the way they are because that puts most of the physical pins on the physical board layout closest to the appropriate resistor terminal.

Next, draw a vertical wire for each grid line, like this. Right now, none of the horizontal and vertical lines are connected. Connect each vertical wire to one horizontal Q rail, along a diagonal.

Now, put a diode at every other intersection in the grid, leaving out the intersections we just connected. Connect the anode to the vertical rail, and the cathode to the horizontal rail.

Here we see why we can light as many lights as we can. Using P pins, we get a P2 array, but we can't use the P intersections along the diagonal, leaving P2-P=P(P-1) lights left.

To light a light, decide which vertical and horizontal rail you need. For instance, LED24 is on the vertical Q04 and horizontal Q06, or (Q04,Q06) for short. To light it, set the vertical rail to high voltage, the horizontal rail to low voltage, and all the other rails to input.

This grid form makes it a bit harder to understand the theory of operation of a Charlieplex,in particular its flaws, but this form is much easier to design, expand, and program with, and is much more compact. Check Wikipedia for the other form. In that form, each diode is in a pair, one pointing up and one down. In this grid form, the pair is the one diagonally opposite, so the pair for LED4, on (Q01,Q05) is at (Q05,Q01) and is LED35. In particular in the Precision charlieplex, the LEDs in the top triangle go on one board and the ones on the bottom triangle go oh the other board.

If you felt like it, you could do a composite form by putting the pairs back together, like so:
I think this is the form that I will design new charlieplexes in. There's no point in rearranging the schematic for Precision now.