This guide will teach us how to connect a Raspberry Pi Pico to a MAX7219 LED Dot matrix display. We shall first examine the MAX7219 LED dot matrix display module’s introduction and pinout. It is an LED array made up of LEDs that is used to display different kinds of messages, symbols, and images as dots. In this tutorial, we’ll introduce you to the MAX7219 dot matrix display and teach you how to programme the Raspberry Pi Pico to display texts in a variety of ways. On the dot matrix, text will be shown and scrolled as one of these.

MAX7219 LED Dot Matrix Display

MAX7219 LED Dot matrix display is one of the popular ones available in the market and used by students, electronics hobbyists, and especially in industrial display applications. There are two types of modules generally available. These are the FC-16 module and the generic module.

LED Dot Matrix

LED dot matrices are available in various dimensions (7×5,8×8, 7×15, etc). The typical 8×8 LED matrix is shown below which comprises 64 LEDs, 8 for each row and column. Each LED is addressed by its row and column number. Each LED is referred to as a dot.

For making an 8×8 dot matrix all the anodes terminals are connected together in rows R1 to R8, similarly, the cathodes are connected together in columns C1 to C8. The reason for connecting all rows and columns together is to save the required number of pins to control each LED dot. By doing this, the required number of I/O pins has been reduced to 16. Otherwise, we will need 64 pins to control an 8×8 LED matrix. This method of controlling a large number of LEDs with few pins is known as multiplexing.

To Turn on a specific do, we need to apply a positive voltage to the respective row of that dot and negative or ground to the respective column of that dot. If the row gets positive voltage and the column gets negative then only a particular LED will glow.

For example, If you want to glow an LED connected between R2 and C1, we have to apply 00000010 logic on row R2 and 11111110 on column C1. If R5 is pulled high and C4 pulled low, then the LED in the fifth row and the fourth column would be high. Similar logic can also be applied to other rows and columns to turn on and off each row and column LED. Furthermore, to display text on the dot matrix, we control each LED very fast speed that the human eye feels like LEDs are constantly on.

MAX7219 IC

MAX7219 is a common-cathode display driver IC with serial inputs and output pins. It has an adjustable current capability which can be set using only one external resistor. In addition to that, it has a four-wire serial interface that can be easily connected to all microprocessors. It can drive 64 individual LEDs connected at its output pins using only 4 wires by using Raspberry Pi Pico board. Furthermore, it can drive dot matrix displays, 7-Segments displays, and bar graphs.

On top of that, MAX7219 has a built-in BCD decoder that makes it easy to use with seven segment numeric displays. Additionally, it has an 8×8 static RAM that we can use to store numbers. It is one of the most popular displays driver IC.

Some key features of MAX7219 include:

  1. It is an LED driver display IC with a 10MHz serial interface which allows the user to select the decode/No-Decode digit.
  2. Its operation is specified in a voltage range of +4.0 to +5.5V. Normally, the voltage supply of +5V is used.
  3. It provides a feature of digital and analog brightness intensity control and a 150µA shutdown mode in which the current of all segments is pulled to ground.
  4. It consumes very low power.
  5. The data is displayed on segments with a delay time of 2.2ms.
  6. MAX7219 works well in a temperature range of 0°C to +70°C.
  7. The maximum current for each segment pin is 100mA and for each DIGIT ground pin is 500mA

MAX7219 LED Dot Matrix Module Interfacing with Raspberry Pi Pico

Following components are required:

Installing MAX7219 MicroPython Library

We will use Thonny IDE to program our Raspberry Pi Pico. Make sure you have the latest version of the IDE installed in your system. For this project we will require the MicroPython max7219 library. Copy this library and save it in your Raspberry Pi Pico with the respective file name (max7219.py) from the GitHub link.

Open a new file in Thonny. Copy the library given below or from the link given above. Save it to Raspberry Pi Pico with the name max7219.py under the lib folder.

max7219.py

from micropython import const

import framebuf

_NOOP = const(0)

_DIGIT0 = const(1)

_DECODEMODE = const(9)

_INTENSITY = const(10)

_SCANLIMIT = const(11)

_SHUTDOWN = const(12)

_DISPLAYTEST = const(15)

class Matrix8x8:

    def __init__(self, spi, cs, num):

        “””

        Driver for cascading MAX7219 8×8 LED matrices.

        >>> import max7219

        >>> from machine import Pin, SPI

        >>> spi = SPI(1)

        >>> display = max7219.Matrix8x8(spi, Pin(‘X5’), 4)

        >>> display.text(‘1234’,0,0,1)

        >>> display.show()

        “””

        self.spi = spi

        self.cs = cs

        self.cs.init(cs.OUT, True)

        self.buffer = bytearray(8 * num)

        self.num = num

        fb = framebuf.FrameBuffer(self.buffer, 8 * num, 8, framebuf.MONO_HLSB)

        self.framebuf = fb

        # Provide methods for accessing FrameBuffer graphics primitives. This is a workround

        # because inheritance from a native class is currently unsupported.

        # http://docs.micropython.org/en/latest/pyboard/library/framebuf.html

        self.fill = fb.fill  # (col)

        self.pixel = fb.pixel # (x, y[, c])

        self.hline = fb.hline  # (x, y, w, col)

        self.vline = fb.vline  # (x, y, h, col)

        self.line = fb.line  # (x1, y1, x2, y2, col)

        self.rect = fb.rect  # (x, y, w, h, col)

        self.fill_rect = fb.fill_rect  # (x, y, w, h, col)

        self.text = fb.text  # (string, x, y, col=1)

        self.scroll = fb.scroll  # (dx, dy)

        self.blit = fb.blit  # (fbuf, x, y[, key])

        self.init()

    def _write(self, command, data):

        self.cs(0)

        for m in range(self.num):

            self.spi.write(bytearray([command, data]))

        self.cs(1)

    def init(self):

        for command, data in (

            (_SHUTDOWN, 0),

            (_DISPLAYTEST, 0),

            (_SCANLIMIT, 7),

            (_DECODEMODE, 0),

            (_SHUTDOWN, 1),

        ):

            self._write(command, data)

    def brightness(self, value):

        if not 0 <= value <= 15:

            raise ValueError(“Brightness out of range”)

        self._write(_INTENSITY, value)

    def show(self):

        for y in range(8):

            self.cs(0)

            for m in range(self.num):

                self.spi.write(bytearray([_DIGIT0 + y, self.buffer[(y * self.num) + m]]))

            self.cs(1)

Raspberry Pi Pico with MAX7219 LED Matrix: MicroPython Sketches

In this section, we will show you how to use the MAX7219 LED matrix with two sketches. We will be able to print various texts on the LED matrix. Additionally, we will also scroll a simple text from right to left.

MAX7219 Dot Matrix displaying texts

Open your Thonny IDE and go to File > New. Copy the code below in that file. This sketch will display a three different texts on the LED matrix after a delay of 1 second.

from machine import Pin, SPI

import max7219

from time import sleep

spi = SPI(0,sck=Pin(2),mosi=Pin(3))

cs = Pin(5, Pin.OUT)

display = max7219.Matrix8x8(spi, cs, 4)

display.brightness(10)

while True:

    display.fill(0)

    display.text(‘PICO’,0,0,1)

    display.show()

    sleep(1)

    display.fill(0)

    display.text(‘1234’,0,0,1)

    display.show()

    sleep(1)

    display.fill(0)

    display.text(‘done’,0,0,1)

    display.show()

    sleep(1)

How does the Code Works?

In this part we will discuss how the code works.

Importing Libraries

We will import the library for the Pin class and the SPI class from the machine module. To interact with the input/output GPIOs we will import the machine module that contains classes to interact with the GPIOs. We should also import the sleep module to insert delay in our MicroPython script. Moreover, we will import the max7219 library as well to work with the LED display.

from machine import Pin, SPI

import max7219

from time import sleep

Initializing SPI

The next step is to initialize the SPI interface with the required parameters including the SPI channel number and SPI pins by creating an object ‘spi’. By using SPI() we specified the SPI channel number as the first parameter and the SPI pins sck and mosi as the rest of the parameters.

spi = SPI(0,sck=Pin(2),mosi=Pin(3))

Next we will configure the GPIO pin connected with the CS pin of the module as an output pin. The first argument in the Pin() class is the pin number on which we are configuring the output. The output is on GP5 which is connected to CS. The second argument shows the pin mode e.g. digital input or digital output mode. As we are configuring GP5 as the digital output we gave it as ‘Pin.Out’. This is stored in the object ‘cs’.

cs = Pin(5, Pin.OUT)

Then we will create the matrix ‘display’ object and specify 4 devices.

display = max7219.Matrix 8×8(spi, cs, 4)

We will set the brightness of the display by using the brightness() method on the display object. This takes in a parameter from 1-15 where 15 is the highest level of brightness.

display.brightness(10)

Display Text

We will continuously change the text after every 1 second. First we will display “PICO” then the numbers “1234” and then “done.”

while True:

    display.fill(0)

    display.text(‘PICO’,0,0,1)

    display.show()

    sleep(1)

    display.fill(0)

    display.text(‘1234’,0,0,1)

    display.show()

    sleep(1)

    display.fill(0)

    display.text(‘done’,0,0,1)

    display.show()

    sleep(1)

Using the fill() method on the display object and passing 0 as a parameter inside it, we will first clear the display.

First we will display the text ‘PICO’ for 1 second. This will be achieved by using text() and passing the string as the first parameter, x as the second parameter, y as the third parameter and col=1 as the last parameter. The show() method will display the string on the LED display.

   display.fill(0)

    display.text(‘PICO’,0,0,1)

    display.show()

    sleep(1)

Likewise, we will display the numbers ‘1234’ and the text ‘done’ after that.

   display.fill(0)

    display.text(‘1234’,0,0,1)

    display.show()

    sleep(1)

    display.fill(0)

    display.text(‘done’,0,0,1)

    display.show()

    sleep(1)

Demonstration

To see the demonstration of the above code, upload the code to Raspberry Pi Pico.

Once the code is uploaded, the LED matrix will start displaying the texts.

Author

Write A Comment