Connect 8x8 LED Matrix
Connecting and programing the matrix
The 8x8 LED Matrix and the max7219 chip allow us to control 64 LEDs per module. Because of the max7219 chip on the module we don’t need to connect every single led to the Raspberry Pi. We only need to connect the chip to the Pi and the chip will control all of the LEDs on our behalf. The SPI communication protocol will be utilized to provide communication between the chip and the Pi. If you have not already activated this, please take the time to do so now. You can find out how here: cw42.io/spi
The first step for programming this project is to install the max7219 library. We can do that with the following terminal commands:
sudo apt-get install python-dev python-pip
sudo pip install spidev
git clone https://github.com/coding-world/max7219
sudo python setup.py install
Now that we have installed the library, we next need to connect the 8x8 LED matrix to the Raspberry Pi.
|Connections Raspberry Pi||Connections LED Matrix|
|5V||1 - VCC|
|GND||2 - GND|
|GPIO 10 (MOSI)||3 - DIN|
|GPIO 8 (SPI CE0)||4 - CS|
|GPIO 11 (SPI CLK)||5 - CLK|
The library has its own simple matrix test file. With the following terminal command you can execute this test Python program and observe text and images on the LED matrix.
sudo python max7219/examples/matrix_test.py
If you connected everything correctly you should first see moving text and then other examples. These examples include nearly all possible functions for the LED matrix. You can always stop the program manually with
Strg/Crl+ c. Our next step is to create our own program for the led matrix.
import max7219.led as led matrix = led.matrix() matrix.show_message("HI HI") matrix.letter(0, ord("1"))
After starting the program you should see the text message “Hi Hi” and then a “1” on the LED Matrix. But let’s take look at the code. In line 1 we are importing the necessary max7219 library. Because we don’t need the whole library we specify the specific part we want
max7219.led and give it an alias with the
as led statement so we don’t have to type the full name each time.
In line 3 we are starting the programming foundation for working with the LED matrix and save this object in the matrix variable. We already discussed a bit about objects. Objects have a lot of different functions which we can call with the follow syntax:
In line 5 we are calling the function
.show_message(). This function will display a text on the LED Matrix. Because the matrix can only show a certain amount of characters at a time, they will be shown like a running electronic ticker.
In line 7 we display a single character with the
.letter() function. This function requires at least two arguments, first the matrix (if you only connected one matrix it is 0) and then the character. The parameter won’t except normal strings so we first need to convert our string to ASCII. In ASCII all characters have a certain number. Python already has a built-in function called
ord() for this conversion.
The library also has other functions which you can use for your program. You can find a list of all of these functions here:
||With this function you can control the brightness of the leds. It accepts a parameter between 0 and 15.|
||Clears all inputs from the display.|
||Scrolls everything to the left|
||Scrolls everything to the right.|
||Inverts everything which is shown on the display. So instead of showing a letter or number it will only show the excerpt.|
||Rotates the matrix. The possible parameters are: 0, 90, 180, 270|
||Turns a single led on or off. Parameter 1: X Position; Parameter 2: Y Position; Parameter 3 controls if the led is on: 1 or off: 0|
Remember, you must always call this function on an already created matrix object: