Raspberry Pi - LED Matrix

This tutorial instructs you how to use Raspberry Pi with LED matrix display. In detail, we will learn:

Subsequently, customizing the code for other LED matrices such as 16x8, 64x8 and so on is straightforward.

Hardware Preparation

1×Raspberry Pi 4 Model B
1×FC-16 LED Matrix 32x8
1×FC-16 LED Matrix 8x8
1×Generic LED Matrix 8x8
1×Jumper Wires
1×DC Power Jack
1×5V Power Adapter for Raspberry Pi
1×(Optional) Screw Terminal Block Shield for Raspberry Pi
1×(Optional) USB-C Power Cable with On/Off Switch for Raspberry Pi 4B
1×(Optional) Plastic Case and Cooling Fan for Raspberry Pi 4B
1×(Optional) HDMI Touch Screen Monitor for Raspberry Pi

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables.

Overview of LED Matrix

LED matrix displays are commonly referred to as LED displays or dot matrix displays.

LED Matrix display

LED Matrix come in many varieties. The MAX7219-based LED matrix is popularly used with Raspberry Pi. It has the following characteristics:

  • A single block comprises a base unit of an LED matrix, which contains an 8x8 LED matrix (64 LED) and a MAX7219 driver.
  • There are two types of blocks available: the generic module and the FC-16 module.
  • You can create a LED matrix with one block or multiple blocks connected in a daisy-chain.
  • Pre-built multiple-block LED Matrix (e.g. 4-in-1, 8-in-1) are also available for purchase.
  • Alternatively, you can buy multiple blocks and wire them together to form a LED matrix with the desired size.
  • In the Raspberry Pi code, you will need to declare the size of the LED matrix you are using.

The LED Matrix Pinout

LED Matrix pinout

A LED Matrix is composed of one or multiple blocks. Each block has two sets of pins:

  • Input pins group:
    • VCC: connected to 5V power supply.
    • GND: connected to ground.
    • DIN: Data pin, linked to the GPIO10 (MOSI) pin of the Raspberry Pi.
    • CS: Chip Select, linked to the GPIO8 (SPI CE0) pin of the Raspberry Pi.
    • CLK: Clock pin, linked to the GPIO11 (SPI CLK) pin of the Raspberry Pi.
  • Output pins group:
    • VCC: connects to VCC on the following module.
    • GND: connects to GND on the following module.
    • DOUT: Data Out, links to the DIN pin of the next module.
    • CS: connects to CS on the next module.
    • CLK: connects to CLK on the next module.

Wiring Diagram

If the LED matrix is composed of a single block:

  • Connect the input pins group to Raspberry Pi
  • Leave the output pins group unconnected
The wiring diagram between Raspberry Pi and 8x8 LED matrix FC-16

This image is created using Fritzing. Click to enlarge image

The wiring diagram between Raspberry Pi and 8x8 LED matrix generic

This image is created using Fritzing. Click to enlarge image

If the LED matrix is pre-built of multiple blocks:

  • Connect the input pins group to Raspberry Pi
  • Leave the output pins group unconnected
The wiring diagram between Raspberry Pi and LED matrix display

This image is created using Fritzing. Click to enlarge image

If the LED matrix is made of multiple blocks by yourself:

  • Attach the input pins groups of the first block to the Raspberry Pi
  • Link the output pins groups of each block to the input pins groups of the subsequent block
  • Leave the output pins group of the last block unconnected
Raspberry Pi 32x8 LED matrix wiring FC-16 diagram

This image is created using Fritzing. Click to enlarge image

To simplify and organize your wiring setup, we recommend using a Screw Terminal Block Shield for Raspberry Pi. This shield ensures more secure and manageable connections, as shown below:

Raspberry Pi Screw Terminal Block Shield

Since Raspberry Pi is connected to LED matrix through SPI pins:

  • Pin GPIO9 (SCK) and GPIO11 (MOSI) of the Raspberry Pi must be used. If you are using a different Raspberry Pi board, consult the official documentation for the equivalent SPI pins.
  • Pin GPIO8 (CS) can be altered to any pin on the Raspberry Pi board. This tutorial uses the GPIO25 for CS pin.

Raspberry Pi - LED Matrix Code

This Raspberry Pi code is for a 32x8 FC-16 LED matrix display with four blocks. It can be easily adapted for 8x8, 16x8, 64x8... displays by changing the BLOCK_NUM constant in the code.

Detailed Instructions

  • Make sure you have Raspbian or any other Raspberry Pi compatible operating system installed on your Pi.
  • Make sure your Raspberry Pi is connected to the same local network as your PC.
  • Make sure your Raspberry Pi is connected to the internet if you need to install some libraries.
  • If this is the first time you use Raspberry Pi, See how to set up the Raspberry Pi
  • Connect your PC to the Raspberry Pi via SSH using the built-in SSH client on Linux and macOS or PuTTY on Windows. See to how connect your PC to Raspberry Pi via SSH.
  • Make sure you have the RPi.GPIO library installed. If not, install it using the following command:
sudo apt-get update sudo apt-get install python3-rpi.gpio
pip install luma.led_matrix
  • Create a Python script file led_matrix.py and add the following code:
""" This Raspberry Pi code was developed by newbiely.com This Raspberry Pi code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/raspberry-pi/raspberry-pi-led-matrix """ from luma.led_matrix.device import max7219 from luma.core.interface.serial import spi, noop from luma.core.virtual import viewport, sevensegment from time import sleep CS_PIN = 25 # Replace with your actual CS pin BLOCK_NUM = 4 # Replace with your block number HEIGHT = 8 WIDTH = 8 * BLOCK_NUM # Define SPI interface serial = spi(port=0, device=0, gpio=noop(), cs=CS_PIN) # Define LED matrix device device = max7219(serial, cascaded=BLOCK_NUM, block_orientation=-90) # Define virtual device virtual = viewport(device, width=WIDTH, height=HEIGHT) # Create instance of sevensegment for text display ledMatrix = sevensegment(virtual) def clear_display(): ledMatrix.text = " " sleep(1) def display_text(text, alignment): ledMatrix.text = text ledMatrix.text_align = alignment sleep(2) try: while True: clear_display() display_text("Left", "left") display_text("Center", "center") display_text("Right", "right") clear_display() display_text("Invert", "center") sleep(2) ledMatrix.text = "1234" sleep(2) except KeyboardInterrupt: pass finally: device.cleanup()
  • Save the file and run the Python script by executing the following command in the terminal:
python3 led_matrix.py
  • Check out the LED matrix showing the text.

The script runs in an infinite loop continuously until you press Ctrl + C in the terminal.

Raspberry Pi LED Matrix Code – Scrolling Text

If a message is too lengthy to be displayed on a LED matrix display, the scroll text effect can be used to print it.

This Raspberry Pi code illustrates how to scroll a message across the LED matrix display.

""" This Raspberry Pi code was developed by newbiely.com This Raspberry Pi code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/raspberry-pi/raspberry-pi-led-matrix """ from luma.led_matrix.device import max7219 from luma.core.interface.serial import spi, noop from luma.core.virtual import viewport, sevensegment from luma.core.legacy import show_message from time import sleep CS_PIN = 25 # Replace with your actual CS pin BLOCK_NUM = 4 # Replace with your block number HEIGHT = 8 WIDTH = 8 * BLOCK_NUM # Define SPI interface serial = spi(port=0, device=0, gpio=noop(), cs=CS_PIN) # Define LED matrix device device = max7219(serial, cascaded=BLOCK_NUM, block_orientation=-90) # Define virtual device virtual = viewport(device, width=WIDTH, height=HEIGHT) # Create instance of sevensegment for text display ledMatrix = sevensegment(virtual) def clear_display(): ledMatrix.text = " " sleep(1) while True: if not device.is_animating(): ledMatrix.text = "Hello" show_message(device, ledMatrix.text, fill=None, font=None, scroll_delay=0.1) try: pass # Do other things if needed except KeyboardInterrupt: pass finally: device.cleanup()

Video Tutorial

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!