RGB Matrix: Part 1 – The Basics

I have recently finished scratch-building a RaspberryPi/QNX-powered portable game console with my son. The “screen” for the console consists of a 16×8 red LED matrix, which obviously limits the game options for the console.

For our next project we wanted a better screen, which led me to buy this 32×16 RGB LED matrix. Purchasing most things nowadays is the easy part, though. Making use of the purchase is another matter. On the web site for the LED matrix the provider makes the following statement:

Of course, we wouldn’t leave you with a datasheet and a “good luck!” We have a full wiring diagrams and working Arduino library code with examples from drawing pixels, lines, rectangles, circles and text. You’ll get your color blasting within the hour!

The statement is technically true: the link sends you to a nice tutorial on how to hook up the matrix to GPIO pins, and proceeds to point to a library that can be used to drive the matrix on various boards. However, at no time are there any simple explanations as to how to use the matrix if you want to do it yourself, rather than rely on a library for a specific set of boards and specific use cases. The code itself is of little help as a tutorial, being far too complicated, board-specific and suffering from a mild case of ifdef hell. I hope to rectify that with a few blog posts that will show how to work with the LED matrix in simple, incremental steps.

Let’s start from the beginning. The most basic use case is to light up various pixels on the matrix using the basic colours. Since each pixel consists of red, green and blue components, there are 8 such basic colours. It is possible to generate other colours using software PWM, but that will be left for another post.

The first step is to follow the instructions here on how to connect the RGB matrix to your board. Other than a few ground connections, you need a GPIO for each of the following inputs on the matrix (assuming the 32×16 variant):

  1. R1: Red component for the first line
  2. G1: Green component for the first line
  3. B1: Blue component for the first line
  4. R2: Red component for the second line
  5. G2: Green component for the second line
  6. B2: Blue component for the second line
  7. A: First address line
  8. B: Second address line
  9. C: Third address line
  10. CLK: Clock, used to transmit per-pixel data
  11. LAT: Latch, used to start and stop the transmission of per-line data
  12. OE: Output enable, switches between input and display modes

A couple of things to note:

  • All of these connections can go to standard GPIO pins.
  • There are three address lines to drive 16 rows. When selecting a row with these address lines, you actually select two rows, one in the range of 0-7 and one in the range 8-15. This is why there are two sets of colour component inputs.

To draw two rows of pixels on the LED matrix we need to take the following steps:

  1. Set the latch pin to 0
  2. For every column:
    1. Set the clock pin to 0
    2. Set each of the R1, G1, B1, R2, G2, B2 pins to the desired colour value
    3. Set the clock pin to 1
  3. Set the latch pin to 1
  4. Set the A, B and C address pins to the desired row number

To display a complete image we repeat these steps for each row over and over.

Further notes:

  • There needs to be some delay after changing the latch and clock pins. You can experiment with different values, but 200 nanoseconds seems to work.
  • The address lines need to be set after the data has been provided.
  • When displaying a complete image I found that there was a “bleeding” effect, where the next row would display a faint shade of the previous one. From experimentation, setting the OE pin to 0 before step 1 and to 1 after step 3 fixes the problem, but I cannot say why.

You are now set to display static images using 8 basic colours on the LED matrix.

Example code for QNX on RaspberryPi is available here.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

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

Google photo

You are commenting using your Google 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 )

Connecting to %s