Driving Matrices

OK we're onto the fun part now! Be sure you have completed the Assembly step before continuing, the soldering is not optional

Step 1. Plug HAT/Bonnet into Raspberry Pi

Shut down your Pi and remove power. Plug the HAT or Bonnet on so all the 2x20 pins go into the GPIO header.

Step 2. Connect Matrix Power cable to terminal block

Your RGB matrix came with a red & black power cable. One end has a 4-pin MOLEX connector that goes into the matrix. The other end probably has a spade connector. If you didn't get a spade connector, you may have to cut off the connector and tin the wires to plug them into the terminal block

Either way, unscrew the terminal blocks to loosen them

Plug the red wire into the + side, and the black wire into the - side.

Step 3. Connect RGB Matrix Data cable to IDC

The RGB matrix also came with a 2x8 data cable. Connect one end to the matrix's INPUT side and the other end to the IDC socket on the HAT/bonnet.

It wont damage the matrix if you accidentally get the cable connected to the output end of the matrix but it wont work so you might as well get it right first time!

If you're using a 64x64 RGB matrix and either a Bonnet or a Rev C HAT, use your soldering iron to melt a blob of solder on the bottom solder jumper so the middle pad is 'shorted' to 8. (This is compatible with 64x64 matrices in the Adafruit store. For 64x64 matrices from other sources, you might need to use 16 instead, check the datasheet.)

Step 4. Power up your Pi via MicroUSB (optional but suggested)

Connect your Raspberry Pi to power via the microUSB cable, just like you normally would to power it up.

You can power the Pi via the 5V wall plug that is also used for the Matrix but its best to have it powered seperately

Step 5. Plug in the 5V DC power for the Matrix

OK now you can plug in your 5V 2A or 4A or larger wall adapter into the HAT/bonnet. This will turn the green LED on but nothing will display on your matrix yet because no software is running!

Check that the Matrix plugs are installed and in the right location

IDC goes into the INPUT side (look for any arrows, arrows point from INPUT side to OUTPUT)

Power plug installed, red wires go to VCC, black wires to GND

Step 6. Log into your Pi to install and run software

OK now you are ready to run the Pi software. You will need to get into a command line via the HDMI monitor, ssh or console cable. You will also need to make sure your Pi is on the Internet via a WiFi or Ethernet connection.

We have a script that downloads the code and any prerequisite software. It works with the current Raspbian “Stretch” operating system (either the Lite or Desktop version):

curl https://raw.githubusercontent.com/adafruit/Raspberry-Pi-Installer-Scripts/master/rgb-matrix.sh >rgb-matrix.sh
sudo bash rgb-matrix.sh

The LED-matrix library is (c) Henner Zeller [email protected] with GNU General Public License Version 2.0 http://www.gnu.org/licenses/gpl-2.0.txt

Earlier versions of this guide used our own fork of this library. That’s deprecated now, but still available if you have existing code built atop it. Otherwise, use this installer script and latest code.

When first run, the script will explain its plans and give you the option to cancel.

Of particular note here: any existing installation will be replaced. If there is a directory called “rpi-rgb-led-matrix” in the current working directory, its contents will be overwritten. Additionally, a Python module is installed and will replace anything currently there. If this is a problem, cancel and make a backup. Otherwise, sometimes reinstalling is exactly what you want.

Next the script will ask you what kind of adapter you’re using between the Pi and RGB matrix: either an Adafruit RGB Matrix Bonnet, or RGB Matrix HAT with RTC. If you select the latter, you’ll also be asked if you want to install additional drivers for the realtime clock.

Then you’re asked whether you need the absolute best image possible from the LED matrix, or can accept slightly reduced quality for the sake of simplicity.

The “quality” option comes at a cost. First, you need to solder a jumper wire between GPIO4 and GPIO18 on the Bonnet or Hat board. Also, the normal sound output of the Raspberry Pi must be disabled. You can still use a USB sound adapter if needed, but audio over HDMI or from the 1/8" jack will not be present.

The “convenience” setting requires no changes and sound still works. For many casual projects this might look good enough. There’s an occasional bit of flicker from the matrix, that’s all.

If you’re not sure, or if you just want to get started experimenting with your new gadget, select “convenience” for now. You can make the change and reinstall the software later if needed.

The script will confirm your selections and offer one more chance to cancel without changes.

There’s a lot of software to update, download and install, so it may take up to 15 minutes or so to complete. Afterward, you’ll be asked whether you want to reboot the system. If you’ve selected to install RTC support (for the Matrix HAT + RTC) or have made a change in the “quality” vs “convenience” setting, a reboot is required.

All other settings (LED matrix size, number of “chained” matrices and so forth) are specified at run-time.

Overclocked Raspberry Pi boards may produce visual glitches on the LED matrix. If you encounter such trouble, first thing to try is to set the Pi to the default (non-overclocked) speed using raspi-config, then reboot and retest.

Testing the Examples

The installer creates a directory called rpi-rgb-led-matrix, and inside this is a subdirectory examples-api-use with a few programs we can use to experiment with the matrix and confirm everything’s working.

All of the examples — and any code using the companion libraries — accept a common set of command-line switches for specifying the LED matrix size and other options. Among the more vital options are:

--led-rows=(rows)

Specifies the number of rows (or height or the number of pixels vertically) of your LED matrix (or matrices, if you have several chained…they all need to be the same size though). Default value if unspecified is 32. Maximum value with the Adafruit RGB Matrix HAT + RTC is 32. Maximum with the RGB Matrix Bonnet is 64.

--led-cols=(columns)

Specifies the number of columns (or width or the number of pixels horizontally) of your LED matrix/matrices. Default value if unspecified is 32.

--led-chain=(chained)

Specifies the number of matrices in the chain…the output of one connects to the input of the next. Default value if unspecified is 1.

Here’s how to run one of the examples — a rotating colored square. Because this code is performing low-level hardware operations, it must be run using the sudo command:

sudo ./demo -D0 --led-rows=32 --led-cols=16

That’s for a single 32x16 pixel RGB matrix. If you have a different size, change the --led-rows and/or --led-cols values. Add a --led-chain value if multiple matrices are chained.

There are 12 different examples in the demo program (0 through 11), chosen with -D. For a full list of the program’s options, just type demo.

Depending on your matrix type and Raspberry Pi model, some additional options may need fine-tuning:

--led-slowdown-gpio=(0…2) Sometimes needed to “throttle back” the speed when using a fast Pi with a slower panel. Default is 1.

--led-rgb-sequence=(RGB order) Some LED matrices may have their red, green and blue LEDs wired up in a different order…for example, if you need to swap the green and blue channels, use --led-rgb-sequence=RBG. Default is RGB.

--led-pwm-bits=(1…11) For long matrix chains you’ll probably need to use fewer PWM bits, sacrificing some color fidelity to improve refresh speed. Default is 11.

There are still many additional options but they’re increasingly esoteric and might only be needed with RGB matrices from other sources. For a complete explanation of these options (and a more in-depth explanation of the options above) see the documentation accompanying hzeller’s code repository.

The demos kinda run, but I’m seeing weird rectangles and glitches.

If your Pi is overclocked, or if you’re using a Raspberry Pi 2, you may need to dial back the matrix control speed slightly. This can be done with the --led-slowdown-gpio=2 setting. Conversely, early Raspberry Pis (Model A, B and similar) might get an improved image by speeding up the matrix code with a value of 0 here.

There are a few additional examples in that directory showing how to write C++ programs to draw to the matrix. Look through the source code and Makefile to see how this is done and how to link with the rgbmatrix library. And there’s more documentation in the hzeller repository, including initializing the matrix size and chain length in your code so it’s not necessary to specify this on the command line every time.

Using the Python Library

Some Python examples are included in the rpi-rgb-led-matrix/bindings/python/samples directory. The matrix installer script has already loaded the prerequisite Python Imaging Library and installed the rgbmatrix module for both Python 2.7 and Python 3.

Again, more documentation is available in the library author’s repository, and some of the examples show how to specify the matrix size and chain length in code rather than command-line selections every time.

While the rgbmatrix module provides its own drawing operations, it can also work with the Python Imaging Library as an “offscreen canvas” that’s then issued to the matrix with the SetImage() or SwapOnVSync() function — see the examples with “image-” in their name.

Core PIL image functions are explained here: The Image Module

Graphics functions (lines, etc.) are here: The ImageDraw Module

Reminder: the older Adafruit fork of the RGB matrix library is still available if you need it for existing code, but consider this deprecated. For new projects we recommend the more up-to-date hzeller code installed by the rgb-matrix.sh script!

This guide was first published on Jan 16, 2015. It was last updated on Oct 14, 2018. This page (Driving Matrices) was last updated on Oct 04, 2018.