# Connecting a 16x32 RGB LED Matrix Panel to a Raspberry Pi

## Overview

![](https://cdn-learn.adafruit.com/assets/assets/000/015/217/medium800thumb/raspberry_pi_raspanimated.jpg?1448311537)

Everyone loves a colorful LED screen! Our 16x32 RGB LED display is a low cost, and easy-to-use arrangement of bright LEDs - just like the ones used in Times Square! This display makes a fantastic addition to your Raspberry Pi. It is pretty easy to wire up, but the display is quite demanding when it comes to getting it to display something...luckily this tutorial has all the code you need to get blinky

![](https://cdn-learn.adafruit.com/assets/assets/000/015/215/medium800/raspberry_pi_overview_web.jpg?1394741129)

This tutorial is based on [this one](https://github.com/Boomerific/rpi-rgb-led-matrix) by Henner Zeller but as well as using Henner's C code to drive the display, we will also experiment (with limited success) in driving the display using Python.

# Connecting a 16x32 RGB LED Matrix Panel to a Raspberry Pi

## You Will Need

To connect up and use this display, you will need the following items.

If you don't mind cutting the connector off one end of the power cable for the display, and then stripping two of the wires, then you can do without the male to male headers.

# Connecting a 16x32 RGB LED Matrix Panel to a Raspberry Pi

## Wiring the Display

Danger: 

I noticed that the display is able to pull some power from the GPIO pins when it is not powered up. So I would suggest that, to be on the safe side, you wire up the display to the Pi, with both the Pi and the display powered off.  
  
Connect up the display to the GPIO connector using the female to female jumper wires as shown below:

![](https://cdn-learn.adafruit.com/assets/assets/000/015/207/medium800/raspberry_pi_wiring_diagram.png?1394711938)

Note that the 3.3V and 5V pins of the Raspberry Pi are on the edge of the Raspberry Pi where the SD card fits!  
  
If you prefer to make the connections using a list, they are as follow:

- **GND** on display to GND on the Pi (blue or black)  
- **R1** on display to GPIO 17 on the Pi (red)  
- **G1** on display to GPIO 18 on the Pi (green)  
- **B1** on display to GPIO 22 on the Pi (blue)  
- **R2** on display to GPIO 23 on the Pi (red)  
- **G2** on display to GPIO 24 on the Pi (green)  
- **B2** on display to GPIO 25 on the Pi (blue)  
- **A** on display to GPIO 7 on the Pi (yellow or white)  
- **B** on display to GPIO 8 on the Pi (yellow or white)  
- **C** on display to GPIO 9 on the Pi (yellow or white)  
- **OE** on display to GPIO 2 on the Pi (brown)  
- **CLK** on display to GPIO 3 on the Pi (orange)  
- **LAT** on display to GPIO 4 on the Pi (yellow)  

  
  
Info: 

![](https://cdn-learn.adafruit.com/assets/assets/000/018/145/medium800/raspberry_pi_1787-02.jpg?1405619182)

Using some kind of template like the [Raspberry Leaf](http://www.doctormonk.com/2013/02/raspberry-pi-and-breadboard-raspberry.html), soon also to be available ready made from Adafruit, will simplify the process of finding the right pins on the Raspberry Pi.  
When all the data pins are connected, we can wire up the power supply.

![](https://cdn-learn.adafruit.com/assets/assets/000/015/208/medium800/raspberry_pi_power_wiring.jpg?1394712050)

Attach the header pins to the 2.1mm to Screw Jack Adapter. Make sure that the red lead goes to the + mark on the screw terminal. Place the other ends of the jumper wires into the power cable. The power cable has double positive and negative leads. It does not matter which of the pairs of wires you connect to as long as you make sure that positive goes to one of the red leads and negative to one of the black leads.

Info: 

# Connecting a 16x32 RGB LED Matrix Panel to a Raspberry Pi

## Testing

Before powering up the Display and Raspberry Pi, check the wiring over carefully and make sure everything is connected as it should be.

![](https://cdn-learn.adafruit.com/assets/assets/000/015/209/medium800/raspberry_pi_wiring_closeup.jpg?1394712171)

Power the display up first and then the Raspberry Pi. 

  

Lets start by creating a directory in which to keep the code. So open an LX terminal session (you can use [SSH with the Pi](http://learn.adafruit.com/adafruits-raspberry-pi-lesson-6-using-ssh)) if you prefer and enter the following command.
```auto
$ mkdir display16x32
$ cd display16x32
```

We now need to fetch the code from Henner Zeller's original project using the command:

```auto
$ git clone https://github.com/hzeller/rpi-rgb-led-matrix/
```

This code is C source code, so we need to compile it before we can run it. It also requires a small change for the wiring layout we're using. Enter the following commands:

```auto
$ cd rpi-rgb-led-matrix/lib
$ nano Makefile
```

(Or substitute your editor of preference.)

Enable this line in the Makefile (it’s commented out by default):

```auto
DEFINES+=-DRGB_CLASSIC_PINOUT
```

Save the changes to the file, then exit the editor and type...

```auto
$ cd ..
$ make
```

To run the test program, enter the command:

```auto
$ sudo ./led-matrix
```

![](https://cdn-learn.adafruit.com/assets/assets/000/015/210/medium800/raspberry_pi_f2f_front_web.jpg?1394712279)

The test program can also display a scrolling message. Infortunately, this message was designed for a 32x32 display and so you will only see the top half of the message. In the next section I will show you how to create your own message image using GIMP.

  

Try out the oversized message anyway using:
```auto
$ sudo ./led-matrix 1 runtext.ppm
```

# Connecting a 16x32 RGB LED Matrix Panel to a Raspberry Pi

## A Scrolling Message

The images must be in a format called ppm (portable pixel format). Many image editors support this format, including the free open source application Gimp.

  

The image needs to be 16 pixels high, but can be wider than the 32 pixels of the display, as the text will scroll horizontally.

  

This image was created in GIMP. Notice that the background needs to be black.
![](https://cdn-learn.adafruit.com/assets/assets/000/015/211/medium800/raspberry_pi_image.png?1394712336)

Use the **File-\>Export** command on GIMP to export the image as ppm and then transfer the file into the directory **/home/pi/display16x32/rpi-rgb-led-matrix** on your Raspberry Pi. You can just use a flash drive to copy the file over.  
Run the command again, this time using the name that you gave the file:

```auto
$ sudo ./led-matrix 1 my_image.ppm

```

The image should then appear on the display.

![](https://cdn-learn.adafruit.com/assets/assets/000/015/212/medium800/raspberry_pi_overview_web.jpg?1394712386)

# Connecting a 16x32 RGB LED Matrix Panel to a Raspberry Pi

## Experimental Python Code

The display uses shift registers, those are all the chips on the back of the screen. This requires the Pi to do a lot of work bit-banging the pixels onto the screen. You can read more about how this display works [here](http://learn.adafruit.com/32x16-32x32-rgb-led-matrix/how-the-matrix-works "Link: http://learn.adafruit.com/32x16-32x32-rgb-led-matrix/how-the-matrix-works").  
As an experiment, I decided to see how practical it would be to try and drive the display from Python code, rather than using the much faster C code.  
I had fairly limited success in this. The display refreshes with a bit of flicker, is rather uneven in light intensity and dimmer than the C code. So if anyone can improve the code and make it work better, we would love to hear from you and be happy to update this section with due credit. ![](https://cdn-learn.adafruit.com/assets/assets/000/015/213/medium800/raspberry_pi_python_experiment.jpg?1394712504)

The code is relatively compact. It is crude, doing nothing in the way of PWM and can therefore only display eight colors.   
 If you want to give it a go, the wiring is exactly the same as before, and you will just need to paste the following code into a file called test\_display.py

```auto
import RPi.GPIO as GPIO
import time

delay = 0.000001

GPIO.setmode(GPIO.BCM)
red1_pin = 17
green1_pin = 18
blue1_pin = 22
red2_pin = 23
green2_pin = 24
blue2_pin = 25
clock_pin = 3
a_pin = 7
b_pin = 8
c_pin = 9
latch_pin = 4
oe_pin = 2

GPIO.setup(red1_pin, GPIO.OUT)
GPIO.setup(green1_pin, GPIO.OUT)
GPIO.setup(blue1_pin, GPIO.OUT)
GPIO.setup(red2_pin, GPIO.OUT)
GPIO.setup(green2_pin, GPIO.OUT)
GPIO.setup(blue2_pin, GPIO.OUT)
GPIO.setup(clock_pin, GPIO.OUT)
GPIO.setup(a_pin, GPIO.OUT)
GPIO.setup(b_pin, GPIO.OUT)
GPIO.setup(c_pin, GPIO.OUT)
GPIO.setup(latch_pin, GPIO.OUT)
GPIO.setup(oe_pin, GPIO.OUT)

screen = [[0 for x in xrange(32)] for x in xrange(16)]

def clock():
    GPIO.output(clock_pin, 1)
    GPIO.output(clock_pin, 0)

def latch():
    GPIO.output(latch_pin, 1)
    GPIO.output(latch_pin, 0)

def bits_from_int(x):
    a_bit = x &amp; 1
    b_bit = x &amp; 2
    c_bit = x &amp; 4
    return (a_bit, b_bit, c_bit)

def set_row(row):
    #time.sleep(delay)
    a_bit, b_bit, c_bit = bits_from_int(row)
    GPIO.output(a_pin, a_bit)
    GPIO.output(b_pin, b_bit)
    GPIO.output(c_pin, c_bit)
    #time.sleep(delay)

def set_color_top(color):
    #time.sleep(delay)
    red, green, blue = bits_from_int(color)
    GPIO.output(red1_pin, red)
    GPIO.output(green1_pin, green)
    GPIO.output(blue1_pin, blue)
    #time.sleep(delay)

def set_color_bottom(color):
    #time.sleep(delay)
    red, green, blue = bits_from_int(color)
    GPIO.output(red2_pin, red)
    GPIO.output(green2_pin, green)
    GPIO.output(blue2_pin, blue)
    #time.sleep(delay)

def refresh():
    for row in range(8):
        GPIO.output(oe_pin, 1)
        set_color_top(0)
        set_row(row)
        #time.sleep(delay)
        for col in range(32):
            set_color_top(screen[row][col])
            set_color_bottom(screen[row+8][col])
            clock()
        #GPIO.output(oe_pin, 0)
        latch()
        GPIO.output(oe_pin, 0)
        time.sleep(delay)

def fill_rectangle(x1, y1, x2, y2, color):
    for x in range(x1, x2):
        for y in range(y1, y2):
            screen[y][x] = color


def set_pixel(x, y, color):
    screen[y][x] = color

fill_rectangle(0, 0, 12, 12, 1)
fill_rectangle(20, 4, 30, 15, 2)
fill_rectangle(15, 0, 19, 7, 7)

while True:
    refresh()
```

You can run the program using the command:

  

```auto
$ sudo python test_display.py
```

Looking at the end of the file, you can see the "fill\_rectangle" and "set\_pixel" functions that you can use to draw on the screen. The final argument of both of these functions is the color that should be a number between 0 and 7.  
  
The display refreshing should really take place in a separate thread of execution, but as I said, this is more of an experiment than anything else.

# Connecting a 16x32 RGB LED Matrix Panel to a Raspberry Pi

## Next Steps

Using a scrolling image file is the easiest way to get your raspberry Pi displaying something fancy. You could create a whole series of images, or even create images on the fly and then send them to the test program to be displayed.

  

  

You may also like to continue the Python experimentation!

  

  

  

  

**About the Author.**

As well as contributing lots of tutorials about Raspberry Pi, Arduino and now BeagleBone Black, Simon Monk writes books about open source hardware. You will find his books for sale [here](http://www.adafruit.com/index.php?main_page=adasearch&q=simon+monk) at Adafruit.

  


## Featured Products

### Raspberry Pi Model B 512MB RAM

[Raspberry Pi Model B 512MB RAM](https://www.adafruit.com/product/998)
Adafruit ships the **Raspberry Pi Model B 512MB RAM** as of 10/18/2012.  
  
The Raspberry Pi® is a single-board computer developed in the UK by the Raspberry Pi Foundation with the intention of stimulating the teaching of basic computer science in schools. The Raspberry...

No Longer Stocked
[Buy Now](https://www.adafruit.com/product/998)
[Related Guides to the Product](https://learn.adafruit.com/products/998/guides)
### Medium 16x32 RGB LED matrix panel - 6mm Pitch

[Medium 16x32 RGB LED matrix panel - 6mm Pitch](https://www.adafruit.com/product/420)
Bring a little bit of Times Square into your home with this 16 x 32 RGB LED matrix panel. These panels are normally used to make video walls, here in New York we see them on the sides of busses and bus stops, to display animations or short video clips. We thought they looked really cool so we...

In Stock
[Buy Now](https://www.adafruit.com/product/420)
[Related Guides to the Product](https://learn.adafruit.com/products/420/guides)
### The Raspberry Leaf (Three Pack)

[The Raspberry Leaf (Three Pack)](https://www.adafruit.com/product/1772)
Have you ever tried making a connection from your Raspberry Pi to a breadboard and had trouble finding the pin you need because nothing is labelled? Have you made mistakes matching a diagram of the pinout to the actual pins? Are you not making enough connections to use our amazing <a...></a...>

No Longer Stocked
[Buy Now](https://www.adafruit.com/product/1772)
[Related Guides to the Product](https://learn.adafruit.com/products/1772/guides)
### Pi GPIO Reference Board for Raspberry Pi Model B

[Pi GPIO Reference Board for Raspberry Pi Model B](https://www.adafruit.com/product/1432)
Connecting wires to the Raspberry Pi GPIO? The Pi GPIO reference board from Low Voltage Labs provides an easy way to make sure you are hooking up the wires correctly.  
Features:

- Only 0.8mm thick.
- Compatible with Raspberry Pi Rev 1 and Rev 2 GPIO.
- Idiot proof,...

No Longer Stocked
[Buy Now](https://www.adafruit.com/product/1432)
[Related Guides to the Product](https://learn.adafruit.com/products/1432/guides)
### 74AHCT125 - Quad Level-Shifter (3V to 5V)

[74AHCT125 - Quad Level-Shifter (3V to 5V)](https://www.adafruit.com/product/1787)
Level shifting chips let you connect 3V and 5V devices together safely. This chip is similar to others in the shop (such as the 74LVC125) except this one is particularly good at converting **3V logic up to 5V**. This is in demand, especially when connecting some 3V devices such as...

In Stock
[Buy Now](https://www.adafruit.com/product/1787)
[Related Guides to the Product](https://learn.adafruit.com/products/1787/guides)
### 5V 2A (2000mA) switching power supply - UL Listed

[5V 2A (2000mA) switching power supply - UL Listed](https://www.adafruit.com/product/276)
This is an FCC/CE certified and UL listed power supply. Need a lot of 5V power? This switching supply gives a clean regulated 5V output at up to 2000mA. 110 or 240 input, so it works in any country. The plugs are "US 2-prong" style so you may need a plug adapter, but you can pick one...

In Stock
[Buy Now](https://www.adafruit.com/product/276)
[Related Guides to the Product](https://learn.adafruit.com/products/276/guides)
### Premium Female/Female Jumper Wires - 40 x 6"

[Premium Female/Female Jumper Wires - 40 x 6"](https://www.adafruit.com/product/266)
Handy for making wire harnesses or jumpering between headers on PCB's. These premium jumper wires approximately 6" (150mm) long and come in a 'strip' of 40 (4 pieces of each of ten colors). They have 0.1" sockets on either end and fit cleanly next to each other on...

In Stock
[Buy Now](https://www.adafruit.com/product/266)
[Related Guides to the Product](https://learn.adafruit.com/products/266/guides)
### Premium Male/Male Jumper Wires - 40 x 6" (150mm)

[Premium Male/Male Jumper Wires - 40 x 6" (150mm)](https://www.adafruit.com/product/758)
Handy for making wire harnesses or jumpering between headers on PCB's. These premium jumper wires are 6" (150mm) long and come in a 'strip' of 40 (4 pieces of each of ten rainbow colors). They have 0.1" male header contacts on either end and fit cleanly next to each other...

In Stock
[Buy Now](https://www.adafruit.com/product/758)
[Related Guides to the Product](https://learn.adafruit.com/products/758/guides)

## Related Guides

- [Moon Phase Clock for Adafruit Matrix Portal](https://learn.adafruit.com/moon-phase-clock-for-adafruit-matrixportal.md)
- [RGB LED Matrix Cube with 25,000 LEDs](https://learn.adafruit.com/rgb-led-matrix-cube-for-pi.md)
- [Adafruit 5x5 NeoPixel Grid BFF](https://learn.adafruit.com/adafruit-5x5-neopixel-grid-bff.md)
- [T³ Time Triangle Thing](https://learn.adafruit.com/t-time-triangle-thing.md)
- [Image Correction for RGB LED Matrices](https://learn.adafruit.com/image-correction-for-rgb-led-matrices.md)
- [5" Display Kippah Portable Raspberry Pi](https://learn.adafruit.com/portable-kippah-pi.md)
- [MCP9808 Temperature Sensor Python Library](https://learn.adafruit.com/mcp9808-temperature-sensor-python-library.md)
- [Simple Raspberry Pi Robot](https://learn.adafruit.com/simple-raspberry-pi-robot.md)
- [RGB Matrix Panels With Raspberry Pi 5](https://learn.adafruit.com/rgb-matrix-panels-with-raspberry-pi-5.md)
- [Read-Only Raspberry Pi](https://learn.adafruit.com/read-only-raspberry-pi.md)
- [Adafruit 8x16 LED Matrix FeatherWing](https://learn.adafruit.com/adafruit-8x16-led-matrix-featherwing.md)
- [3D Printed Frame for Adafruit IS31FL3741 LED Glasses](https://learn.adafruit.com/3d-printed-frame-for-led-glasses-is31fl3741.md)
- [Set up Home Assistant with a Raspberry Pi](https://learn.adafruit.com/set-up-home-assistant-with-a-raspberry-pi.md)
- [IoT Twitter Listener Party Parrot](https://learn.adafruit.com/iot-twitter-listener-party-parrot.md)
- [Large Pi-based Thermometer and Clock](https://learn.adafruit.com/large-pi-based-thermometer-and-clock.md)
