Wednesday, November 24, 2010

Building a 2-color 8x8 LED matrix

If you're new to electronics, or if you've just started playing with the Arduino or Japanino, it probably won't take long before you start thinking about building an LED matrix, like the scrolling LED dot signs you see outside of various stores. You can pick up the LCD display from Spark Fun, which gives you a nice little 132x132 pixel monitor screen that you can use for displaying text and pictures, but it's not quite as eye-catching as the more primitive 8x8 matrix is. In fact, everything you can do on a matrix, you can do on the LCD shield, and a whole lot more. But, the LCD shield is nowhere near as bright as an LED matrix can be.

(Proving the initial concept.)

If you build an LED matrix, you can choose to use as many LEDs as you like. But, the Japanino only has 20 I/O pins (the Arduino only has 18), so if you go over over a 4x5 rectangle, you can't address the LEDs individually per-pin. The work-around is to connect the LEDs together so that they share common rows and columns. Depending on how you connect the LEDs at the junction points of the matrix, raising one row high and setting one column low can make the LED at that junction turn on. Then, if you do this 400-500 times per second, the human eye's "persistence of vision" will overcome the flickering and all of the LEDs in the matrix will look like they're on at the same time. It's a little more difficult to program than just writing a "1" to one pin, but it's still fairly easy to work with. And, there are so many people on the net that have done exactly this that you can easily pull up circuit diagrams and sketches for the matrix size you want.

One drawback to making your own matrix is that for an 8x8 circuit, buying 64 LEDs will cost more than just buying a pre-made commercial matrix off-the-shelf. Note that if you use 64 LEDs, the resulting matrix will only be one color. If you want to use a second set of LEDs to get 3 colors together (red, green, or red+green to get yellow), you're essentially going to be building two 64-LED matrices in the same space. An off-the-shelf bi-color 8x8 matrix can be about $4 USD. Tri-color no more than $6 or $7. Might as well get the off-the-shelf product, even if you can find 64 tri-color LEDS (which may total almost $100, depending on the size you get and who you buy them from).

(Starting out 4x4.)

Back right after getting the dual-axis accelerometer chip and Spark Fun LCD shield, I decided to build a simple one-color 4x4 LED matrix on the Spark Fun proto shield, alongside the accelerometer chip. Not much of a challenge wiring-wise, and really easy to program. But, kind of limited on the types of patterns you can animate. Still, it was fun seeing something I'd built by hand working on the first try. (Note that in the picture below, the wires crossing over each other aren't actually touching. There's a 1/8" gap between them.)

(Back of the 4x4 matrix.)

I found a bi-color 8x8 matrix at Marutsu in Akihabara for 400 yen (about $5 USD). With all of the other parts I needed to build the circuit, the total came to 900 yen, not including the Switch Science vanilla proto shield board and through headers (another 800 yen for that). So, the total for the project was around maybe $22-$23. (I'm thinking of adding two more push button switches, which will be another $3.)

(Finished kit with a simple "plus sign" pattern.)

If you're programming a 1-color 8x8 matrix, you'll have 8 rows and 8 columns. It's very easy to hook these up to the Arduino pins and you're done. For a bi-color matrix, either the red and green LEDs will share the same anode, or cathode connections, so there will be 8 rows, and 16 columns. The Para Light A-3880EG matrix I got is common anode, and has 2 rows of 12 pins each - 24 pins total. It was really hard to get a pin out diagram for this part, even from the manufacturer's own website. I did find a tutorial page that described using 74hc595's for driving the columns, and that helped a lot.

(Same pattern. The camera flash overwhelms the matrix image.)

Since we're looking at 24 pins, we can't just connect them directly to the Arduino. Instead, the idea is to use a 16-bit shift register. What this does is to take the data from one pin of the Arduino, and then clock it a bit at a time into the shift register in serial form. This lets you program one full row of LEDs, both red and green. Turn off that row and turn on the next row, really fast, and you're back to the case of a single color matrix - no problem. If you have a tri-color matrix, then you just need to have a 24-pin shift register. In any case, the shift register uses 4 Arduino pins, compared to the 8 (16 or 24) pins needed by addressing the LED matrix directly. The trade off is that with the shift registers you get more colors and more LEDs with less pins, but loading the data serially slows the data transfer rate and the LEDs may look dimmer.

I wanted to use the STP16C596 chip mentioned in the above tutorial, which is a 16 pin shift register, with the ability to moderate current to the LEDs using one simple resister connected to a control pin. But Marutsu doesn't carry it. Instead, I fell back on getting 2 74hc595's, which are 8-bit registers, with no current control circuits. But, even so, the 2 chips are 80 yen each, and a bag of 100 220-ohm resistors is also only 100 yen. No problem on cost - it just means that the circuit takes up more board space.

(Holy rat's nest, Batman!)

One thing I discovered, and that I don't like, is that the Switch Science proto board blocks the battery connector of the Japanino. I could have taken a pair of cutters and cut a notch out of the proto board (like I did for the magic candle circuit), but I figured that I might as well pull out the Japanino connector and replace it with a 30 yen right-angle connector I got from a different shop in Radio Land.

Something else that I discovered is that the pins of this particular matrix don't line up with the holes of a regular perf board. I decided to bend the pins to make them line up, and then put in a socket on the proto board so that I can easily replace the device later if I want to. Current ideas are to build extension cables to let me hang the LED matrix on a shirt or jacket, and to be able to attach a physically larger matrix (one that is almost two-times bigger is only 600 yen).

If you use two 74hc595's, you chain them so that the carry pin of the first chip acts as the Data In source for the second one. Otherwise, you're wiring all of the other control pins in parallel. For programming the matrix - the Arduino has a built-in command called "shiftOut". This makes it much easier to send data to the full row at one time, but I think it also adds some kind of unnecessary delay for the clocking that slows the program down too much. You can also use some of the matrix driver libraries on the net, but I wanted to write my own code so that I have more control over how I generate my patterns. I like the idea of having 2 binary arrays, one for the red LEDs and one for the green, and then combining them in a 64-cell unsigned int array that then gets sent to the display function. This makes animation patterns much more compact, and I can use the red binary array for any mono-color patterns, such as text fonts.

1 comment:

Bunny said...

you'll find that real grown up LED matrixes are a whole different game, eg. AKAMI's 992x320 near your office, or the AXIS 2560x40 along the same building. They are typically fed DVI (vga for the akami!) and then the actual display refreshes at a couple of hundred Hz.

I don't have any close ups of this unit, but somewhere I have photos of Lighthouse units.

For repair, they are modular. I believe the akami panels can be replaced from the inside. Though it looks tricky, there are frames, sub frames, etc.