A while ago I designed a circuit that would power an 8×8 RGB LED Matrix (192 LEDs in all) using only 3 pins from an Arduino Diecimila and supporting Pulse Width Modulation. I’ve uploaded some videos of this project on Youtube and folks have been emailing me with questions on how it works. In this post I’ll try to answer all those questions so you can build your own.
I started with the LED Matrix component itself. I used an RGB 8×8 common anode matrix from LedSee (only $10 on ebay). The matrix takes 32 pins: 8 anodes, 8 cathodes for Red, 8 for Green and 8 for Blue. Then I had an arduino Diecimila which was about $25 I think but you can use any ATMega based board.
A little googling revealed that a thing called a Shift Register could help me drive all those 32 pins using just a couple from the arduino. Shift Registers are cheap and easy to source. I used the 74HC595 IC.
The arduino website has a tutorial on Shift Registers which can be found here: http://www.arduino.cc/en/Tutorial/ShiftOut. This explains what they are, how they work, the clock, latch and data pins as well as how to chain them together.
After a few evenings of experimentation I knew I could drive 8 pins per 74HC595 chip. If I chained enough together I could drive all 32 pins. So I was set.
Next I drew up the schematic on paper. I played with a few prototypes using Breadboard until I had confidence that the circuit would work. This was a smart move as it turned out there was a problem. I found through experimentation that the Red LEDs were overpowering the Green and the Blue. I couldn’t figure it out since the code treated them all as equals. I went back to the reference manual for the LED matrix and found that the operating voltage of the RED LEDs in the matrix is actually lower than the Blue or Green.
I was able to even them out by introducing a higher resistor (330) into the RED cathodes than is in line with the blue and green (220 ohms). That was a true moment of enlightenment.
The blue, green and yellow lines go to the Arduino.
Next up I worked on the code. Besides turning the LEDs on and off I wanted to actually get different colors. I had no idea how to do this but it turned out Robert aka “MadWorm” had already figured it out: [LINK]. My code is essentially based entirely off of Robert’s so I won’t complicate or confuse you with my own extensions.
To better understand the ISR business, I’ll refer you this time to ucHobby [LINK]
The arduino supports an interrupt which is essentially a routine that runs in the the background, almost like a separate thread, from your code. This interrupt is what updates the Matrix pins and it happens very very fast. Even better is that it frees up the main loop of your Arduino sketch to do the interesting stuff like update the actual matrix data.
Pulse Width Modulation or PWM is essentially a way of simulating an analog output from a binary source. Huh? Wha?
If I turn on a Red LED I get red, if I turn on a Blue LED I get blue. If I turn them on both at the same time I get purple. What if I want to get some color in between? with this type of system you can’t get anything but 7 colors (R, G, B, RG, RB, GB, RGB). The only way is to turn on the LEDs partially. But an LED can only be fully on or fully off, how can you get a “partial” LED? The answer is PWM.
Pulse Width Modulation varies the time for which an LED is active. It’s still either fully on or fully off during that time, but if you think of that time as a wave, the “width” of the “pulse” during which the LED is active varies. Here’s a good visual:
The result is an ability to “mix” colors by different degrees and get a far wider range than the 7 possible combinations of RGB. The good news is this is all implemented within Robert’s ISR code above.
So I soldered away busily for three nights and in the end I had a nice little circuit board. Definitely not as small as it could be but good enough for a RevA.
At this point I had everything working nicely so I built simple case of Cherry/Lexan and four hex-head bolts to finish up.
That’s basically it! You can checkout the pictures below or find the video on Youtube. I had a lot of fun making this thing. It sits proudly on my desk today and although many have tried, no one can resist asking “what is that thing”.The PWM is very tough to videotape but take a peek at the pics. Here’s my daughter playing with the prototype: