Moving Forward with Arduino – Chapter 18 – RGB LED Matrix

This is part of a series originally titled “Getting Started with Arduino!” by John Boxall – A tutorial on the Arduino universe. The first chapter is here, the complete series is detailed here.

Welcome back fellow arduidans!

In this instalment we will take a turn away from the serious things for a while, (plus I wanted a bit of a break) and instead enjoy some introductory fun with RGB LED matrices. (Matrices is the plural form of matrix). I am sure some of you have seen these do all sorts of things, so now it is time for me to learn about them and share this with you.

First of all, let’s have a look at one:

Quite large – 60 by 60 mm. Thankfully this unit has the diffused/opaque LED surfaces – some cheaper ones have clear surfaces which don’ t look that good – it is like looking directly at an incandescent light globe, you can see the element more than the colour it emits. When you turn the matrix over, there are less pins than a newcomer may expect:

Two rows of eight pins. Considering there is so much real-estate on the bottom, one would think the manufacturer could print some markings on there – but they don’t. So you need the (example) data sheet.pdf. The three most important things we need to know are:

  • the forward voltages: red – 2 V, green and blue – 3.3;
  • the current at the recommended forward voltage – 20 milliamps;
  • and the pinouts (click to enlarge):

It looks like a mess but isn’t that hard to work out. Do you remember how we used the 8×8 red LED matrix back in chapter nine? We will work on the same style of design in this chapter as well. I do realise there are chips from TI, Maxim and so on that are quite tricky – but I am trying to keep the cost down, of which I am sure you would appreciate. So instead we will use four 74HC595 shift registers (one to control the anodes of each colour, and one to control the cathodes via a bank of switching transistors.

To get this started, first let’s get the hardware out of the way. To fire this baby up we will need:

Initially I was concerned about the amount of current this circuit would draw, however it was not to be an issue. With all the LEDs on, using the 560 ohm current-limiting resistors, is was much less than expected. To check, I powered the lot from a 9V PP3 battery and measured the current flow. 135 milliamps, not bad at all.

It just occurred to me that if you had an Arduino Mega – it could directly take care of everything instead of using three of the shift registers. Hmmm.

So here is our schematic:

In the schematic above, there are eight transistor-resistor combinations between the cathodes of the matrix (pins 25, 24, 23, 10, 9, 8, 7 and IC4. And there are 560 ohm resistors on all output pins of ICs 1~3.  Furthermore,  note that your LED matrix’s pinouts may vary – so please check your data sheet before wiring it all up… having to retrace all those wires once they’re in is a real problem. As you can see from the resulting breadboard photo:

Now how does all this work?

Quite easily really, the hardest part is putting the hardware together. First of all, please review how we used shift registers in chapter four. And, you may recall how we used two 74HC595 shift registers to control an 8×8 red LED matrix back in chapter nine. This is just the same type of set up, but with two more shift registers – now we have one for the cathodes (as we did before), and one shift register each for the red, green and blue LEDs.

Instead of sending out two bytes of data using shiftOut();, we need to send out four bytes. For example, to turn on every LED at once (thereby mixing red, green and blue – producing white) we would create a function such as:

void allOn() // turns on all LEDs. { digitalWrite(latchpin, LOW); shiftOut(datapin, clockpin, MSBFIRST, 255); // cathodes shiftOut(datapin, clockpin, MSBFIRST, 255); // green shiftOut(datapin, clockpin, MSBFIRST, 255); // blue shiftOut(datapin, clockpin, MSBFIRST, 255); // red digitalWrite(latchpin, HIGH); }

So as you can see, the first byte out the door is the data for the cathodes, in this case 255 – which is 11111111 in binary, or in 74HC595-speak “hey, turn on all outputs”. And the same again in turn for each bank of colours, the other three registers are told to open all gates and let current flow through the LEDs to the common-cathode lines controlled by IC4. So naturally, using some binary to base-10 conversion you can set which LEDs to come on and where. And of course, by mixing the primary colours – you can create new ones. For example, the additive colour chart gives us:

So now you can create yellow with red and green; red and blue makes purple; green and blue makes aqua or teal, etc. However I am colour blind, so you tell me. Unfortunately we cannot use PWM to alter the brightness of the LEDs, our 74HC595s can not offer such a signal. If a lot of people ask me, I will show how to do it with a common-anode matrix and some TLC5940 ICs.

Example 18.1

This time we will view the demonstration video first:

Here is the matching sketch: example 18.1.pdf.

Now to examine how each of the effects were created, so you can understand,  use and modify them yourself.

The basic operations are contained in these four lines:


digitalWrite(latchpin, LOW); shiftOut(datapin, clockpin, MSBFIRST, c); // cathodes shiftOut(datapin, clockpin, MSBFIRST, g); // green shiftOut(datapin, clockpin, MSBFIRST, b); // blue shiftOut(datapin, clockpin, MSBFIRST, r); // red digitalWrite(latchpin, HIGH);

So all you need to do is replace r, b, g and c with the base-10 values you want. For example, to light up the red LED in position 1, 1 – use 1, 0, 0, 1. Or if you want the whole first line to be green, use: 255, 0, 0, 1. After a few moments you should become proficient at converting binary to base-10. This chart from chapter four should help you:

Remember that you can also create patterns and so on. For example, if you only wanted LEDs 1 and 8 for your x-axis, you would add 1 and 128 together, and use the sum (129) for your x-value. To save some time, I have created a few functions for you to use. For example:

void displayLEDs(int rr, int gg, int bb, int cc, int dd)  {    digitalWrite(latchpin, LOW);    shiftOut(datapin, clockpin, MSBFIRST, cc); // cathodes    shiftOut(datapin, clockpin, MSBFIRST, gg); // green    shiftOut(datapin, clockpin, MSBFIRST, bb); // blue    shiftOut(datapin, clockpin, MSBFIRST, rr); // red    digitalWrite(latchpin, HIGH);    delay(dd);  }

So instead of having to manually repeat a lot of code, you can just insert the values into displayLEDs();. Another handy thing to know about is looping. When looking at the matrix it is easy to accidentally think “Oh, I can just loop from 1 to 8″… No. Remember your binary to base-10 conversions. So if you wanted to scroll a horizontal line of red LEDs your cathode or y-axis value must increment as such: 1, 2, 4, 8, 16, 32, 64, 128. Every time the loop cycles, it needs to double the value. To do this, consider:

for (int q=1; q<129; q*=2) { displayLEDs(255,0,0,55,100); }

Notice the q*=2? This will multiply the value of q by 2 every loop. Very useful. Another method would be to create an array, as such:

int va[]={1,2,4,8,16,32,64,128,255};

and refer to the elements as required. This is done within the function lostinspace(); within example 18.1.

The next thing to take into account is the screen refresh. Every time you send four bytes of data through the shift registers, those new bytes will ‘shift’ the old bytes out of the way. So if you want to alter the display even by just one LED, you need to redraw the entire display over again with four new bytes of data. Also note that to ‘hold’ an image on the display, you only need to send the data once – the shift registers will stay put until the next four bytes of data come along.

And sometimes, you might just want to turn off the display. Just send four zeros down to the registers, as the function clearMatrix(); does in the example sketch.

For now, please review the various functions found in example 18.1 – alter them, mess about and have some fun. Thus concludes our introduction to RGB LED matrices. However, stay tuned for more about this and other interesting things! If you have any requests, don’t hesitate to ask.

Please subscribe (see the top right of this page) to receive notifications of new articles. High resolution photos are available from flickr.

If you have any questions at all please leave a comment, or better yet – ask in our Google Group –  dedicated to the projects and related items on the website – please sign up, it’s free and we can all learn something. If you would like to showcase your work from this article, email a picture or a link to john at tronixstuff dot com. You might even win a prize!

Don’t forget to check out the range of gear at Little Bird Electronics!

So have fun, stay safe and see you soon for our next instalment!

By John Boxall

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s