Use a 8×8 LED Matrix With Raspberry PI and Python

Check my RPI articles in Best Raspberry PI projects article or home page. Or subscribe my newsletter (top right in this page) to be notified when new projects are available! Also interested to start 3D printing with a cheap budget? Visit my cheap 3D printers list

A very simple electronic component, 8×8 Led Matrix with Raspberry PI (and Python) can display nice and simple images whose application limit is only your fantasy

In this tutorial I’m going to show you how to setup and wire a 8×8 Led Matrix with Raspberry PI and Python, explaining code.

8×8 LED matrix is a small display composed of 8 LED row, each one including 8 LEDs, thus forming a LED matrix (as per its name) . All its LED are usually monochromatic (only 1 colour). It appears as in following picture:

8x8 Led Matrix picture

8×8 LED Matrix has 2 row of PINs in its back side. The lower one (near side label) are usually referred to from 1 to 8. The upper ones, on the other hand, are from 9 to 16 but in reverse sense, as shown in previous picture.

Positive pole of LEDs in each row and negative pole of LEDs in each column are connected together to create a matrix module. In this configuration, we have a “common anode” LED Matrix. There is a second type, where negative pole of LEDs in each row and positive pole of LEDs in each column are connected, This second type is called “common cathode”.

In this project I’m going to use a common anode LEDMatrix, but the following code should work also with a common cathode by inverting LED logic.

Following picture shows internal circuit og both common anode and common cathode models:

8x8 Led Matrix internal circuit

Please note in previous images that, differently from what one could think, pin rows physically close doesn’t represent columns or rows. PINs are mixed in a quite confusing way and you have to pay attention to cabling to connect in right way, otherwise you will get an unpredictable output.

8×8 LED matrix can be powered and controlled both column by column or row by row. In this tutorial I’m going to setup a python code which scans row by row your display, as it simplify creating LED masks to show.

Our script will work with the following logic. A loop is set to iteratively scan 8×8 LED matrix, so that each loop run will select a specific row and put on/off LEDs of that row. Repeating this loop with a very high speed, it will appear to the human eye as a defined and continuous image, instead of a row scan, because of the principle of persistence already used also in my previous 4 digit-7 Segment display tutorial.

8x8 Led Matrix python code logic

Please note that LED row data matches columns configuration.

As my matrix is a common anode, to get a LED on I will have to put its row selector to 1 and column selector to 0 (zero).

Last but not least, as you can see a 8×8 LED matrix requires 16 PINS, which can occupy a high number of GPIO ports from our Raspberry PI. To solve this problem, I’ve adopted for this project the use of 2 cheap 74HC595 shift register.

Finally, I will use a Raspberry PI Zero W for my project, but this guide works also with other Raspberry PI computer boards.

What We Need

As usual, I suggest adding from now to your favourite ecommerce shopping chart all needed hardware, so that at the end you will be able to evaluate overall costs and decide if continuing with the project or removing them from shopping chart. So, hardware will be only:

Check hardware prices with following links:

Amazon raspberry pi boards box
Amazon raspberry pi Zero W box
Amazon Micro SD box
Amazon Raspberry PI Power Supply box
Amazon 8x8 led matrix box
Amazon 74hc595 shift register box
Amazon Dupont Wiring box
Amazon Resistors box
Amazon Breadboard box

Wiring Diagram

Please find below my wiring diagram:

Raspberry PI 8x8 led matrix wiring diagram

I’ve named shift registers to distinguish between the one which drives columns and the other which works for rows. Please note that latch from both shift registers goes together on same Raspberry PI PIN. Same for clock. Vcc from both shift registers are connected to Raspberry PI’s 5V PIN. More details can be found in my Using 74hc595 Shift Register with Raspberry PI tutorial.

As it can be a little complicated to wire from an optimized image inside a post, please find full definition image at×8-led-matrix-wiring-diagram.jpg.

Next table also helps in understanding connections:

Shift RegisterSh.Reg portConnects toPIN
ColumnC08×8 Matrix13
ColumnC18×8 Matrix3
ColumnC28×8 Matrix4
ColumnC38×8 Matrix10
ColumnC48×8 Matrix6
ColumnC58×8 Matrix11
ColumnC68×8 Matrix15
ColumnC78×8 Matrix16
ColumnLatchRaspb.PIGPIO 14
ColumnClockRaspb.PIGPIO 15
ColumnDataRaspb.PIGPIO 20
RowR08×8 Matrix9
RowR18×8 Matrix14
RowR28×8 Matrix8
RowR38×8 Matrix12
RowR48×8 Matrix1
RowR58×8 Matrix7
RowR68×8 Matrix2
RowR78×8 Matrix5
RowLatchRaspb.PIGPIO 14
RowClockRaspb.PIGPIO 15
RowDataRaspb.PIGPIO 21

Following pictures show my RPI connected and working:

Raspberry PI 8x8 led matrix detail 1
Raspberry PI 8x8 led matrix detail 2
Raspberry PI 8x8 led matrix detail 3
Raspberry PI 8x8 led matrix detail 4
Raspberry PI 8x8 led matrix detail 5

… I know, cabling could be better organized… 🙁

As you can see from pictures, I had to use a second expansion breadboard. This was because I was becoming crazy to make cabling correct, but you can also use breadboard only for shift registers and resistors, using cables directly to LED matrix.

Step-by-Step Procedure

Prepare Operating System

Start preparing your Raspberry PI operating system. You can install Raspberry PI OS Lite (for a headless, fast OS) or Raspberry PI OS Desktop (in this case using its internal terminal).

Make your OS updated:

sudo apt update -y && sudo apt upgrade -y

Install RPI.GPIO, as it enables PIN interfacing and management from Python:

sudo apt install rpi.gpio

Prepare 8×8 Led Matrix Script for Python

You can get my script directly in your Raspberry PI with the built-in wget, issuing following terminal command:


Below my script description.

As common for python scripts, required modules are imported first:

import RPi.GPIO as GPIO
import sys

Then used Raspberry PI PINs are associated to variables for better management

columnDataPin = 20
rowDataPin = 21
latchPIN = 14
clockPIN = 15

As we use 2 shift registers, latch and clock will work together on same pins, while data PIN will be one for each shift register.

PIN naming is set to BroadCom (BCM) and all PINs are set to output mode:


A Shift function is now defined. This function works as the one from Using 74hc595 Shift Register with Raspberry PI tutorial, with the difference that this sends data to 2 shift registers at the same time.

def shift_update_matrix(input_Col,Column_PIN,input_Row,Row_PIN,clock,latch):

  for i in range(7, -1, -1):
    GPIO.output(Column_PIN, int(input_Col[i]))
    GPIO.output(Row_PIN, int(input_Row[i]))


Now a LED map is required. Following “smile” variable is a list which has other lists as its elements. Each internal list represents a row. All rows will be sent to 8×8 LED Matrix, with “0” meaning a powered on LED, while “1” will represent a powered off LED:


You can better identfy a smile from this variable in following alternative representation, where red (“0”) bits will be on and green (“1”) bits will be off:

Raspberry PI 8x8 led matrix smile mapping

From here the main loop starts with a while statement, followed by a “try”.

The RowSelect variable is initialized. This variable selects within the Row shift register what row to configure. So [1,0,0,0,0,0,0,0] will make 1rst row active from LED Matrix, then passing row data which will set what LEDs in the first row have to be on. [0,1,0,0,0,0,0,0] will select the second row, [0,0,1,0,0,0,0,0] the third and so on.

while True:

A for loop is then used. Note that a range(0,8) doesn’t include the last value (8), so “i” will have values from 0 to 7. This loop send datat to drive both shift registers with the previous function. In this execution we are going to send “smile” variable to be displayed, but you can configure to show whatever you want, defining your image variable before the main loop:

    for i in range(0,8): # last value in rage is not included by default
      shift_update_matrix(''.join(map(str, smile[i])),columnDataPin,\
                          ''.join(map(str, RowSelect)),rowDataPin,clockPIN,latchPIN)

Then we use the well known, built-in slicing function from python to implement a list rotation:

      RowSelect = RowSelect[-1:] + RowSelect[:-1]

Finally, the “except KeyboardInterrupt:” statement intercepts interrupt key combination from user (CTRL+C from keyboard) to clean GPIO state before exiting the script:

  except KeyboardInterrupt:

Run the 8x8LedMatrix Python Script

To un this script and activate 8×8 LED matrix, simply use following terminal command:


Your image will start appearing in your display. Stop the script by using CTRL+C.


How useful was this post?

Click on a star to rate it anonymously!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?