Reader small image

You're reading from  Raspberry Pi LED Blueprints

Product typeBook
Published inSep 2015
Publisher
ISBN-139781782175759
Edition1st Edition
Right arrow
Author (1)
Agus Kurniawan
Agus Kurniawan
author image
Agus Kurniawan

Agus Kurniawan is an independent technology consultant, author, and lecturer. He has over 18 years' experience working on various software development projects, including delivering training courses and workshops, and delivering technical writing. He has done a few research activities related to wireless networking, software, and security in multiple universities. Currently, he is pursuing a Ph.D. program in Computer Science in Germany. He has previously written five books for Packt.
Read more about Agus Kurniawan

Right arrow

Chapter 4. LED Dot Matrix

An LED dot matrix display is a two-dimensional LED pattern array, used to represent characters, symbols, and images. This chapter will explore how to control an LED dot matrix display from Raspberry Pi. At the end of the chapter, we will cascade several LED dot matrix modules.

You will learn the following topics in this chapter:

  • Introducing LED dot matrix display (8 x 8 LEDs)

  • Displaying a random number in an LED dot matrix display

  • Displaying a random character in an LED dot matrix display

  • Building a ball reflection game

  • Cascading LED dot matrix modules

Introducing LED dot matrix display (8 x 8 LEDs)


In the previous chapter, you learned how to build a digital clock using 4-digit 7-segment and OLED display modules. In this chapter, we will learn how to work with an LED dot matrix display and focus on 8 x 8 LEDs' model. These LEDs can have monochrome color or RGB color. To simplify the problem, we will use monochrome color on the LED dot matrix display module.

In an LED matrix display, multiple LEDs are connected together in row and columns. This connection is established to reduce the number of pins needed to manipulate them. For instance, the 8 x 8 LED dot matrix is shown in the following figure:

In the preceding figure, if R3 is in 1 logic and C2 is in 0 logic, the LED on line 3 column 3 will be turned on. The characters will be displayed by fast scanning each row and column. For illustration, if we need to display the character A, we can draw the following figure on our dot matrix:

An MCU, which wants to work with 8 x 8 LED dot matrix display...

Introducing an LED dot matrix driver


Richard Hull and his team have already built an LED dot matrix driver based on IC MAX7219. You can download it from https://github.com/rm-hull/max7219. This library also supports cascading several dot matrix display modules. This means that we can cascade several LED dot matrix modules by connecting DOUT to DIN among these modules. You will learn how to cascade LED dot matrix modules in the last section of this chapter.

The library needs the SPI library to run the LED dot matrix display module. In the next section, I'm going to explain how to activate SPI on a Raspberry Pi board.

Enabling Raspberry Pi SPI

By default, Raspberry Pi disables the SPI port, so if we want to access the SPI port, we must activate it via raspi-config. Type the following command:

sudo raspi-config

You should get a raspi-config form. Select Advanced Options. Then, you should select A6 SPI, as shown in the following screenshot. Confirm to enable and load SPI onto Raspberry Pi. The...

Displaying a random number on the LED dot matrix display


In this section, we will explore more practices using 8 x 8 LED dot matrix displays. We will display a random number on the LED dot matrix module. A number will be generated by a Python program via random object and then shown on the LED dot matrix display.

Let's start to write the program. Create a file named ch04_02.py. Write the following completed code:

# ch04_02.py file

import max7219.led as led
import time
import random

device = led.matrix(cascaded=1)

print("Running...")
print("Press CTRL+C to exit ")
try:
    while 1:
        number = random.randint(0,9)
        print "display ", number
        device.letter(0, ord(str(number)))
        time.sleep(1)

except KeyboardInterrupt:
    device.command(led.constants.MAX7219_REG_SHUTDOWN,0x00)
    time.sleep(0.01)

print("done")

We can generate a random number by using random.randint() from random object. We pass (0,9) so it generates value from 0 to 9. After this, we pass this random...

Displaying a random character on the LED dot matrix display


Using the scenario from the previous section, we can build a simple program to display a random character on an 8 x 8 LED dot matrix module. We can use random.choice(string.ascii_letters) to retrieve a random character in Python. After this, the random value is passed to the MAX7219 library.

Let's start. Create a file named ch04_03.py. The following is completed code for our scenario:

# ch04_03.py file

import max7219.led as led
import time
import random
import string

device = led.matrix(cascaded=1)

print("Running...")
print("Press CTRL+C to exit ")
try:
    while 1:
        character = random.choice(string.ascii_letters)
        print "display ", character
        device.letter(0, ord(character))
        time.sleep(1)

except KeyboardInterrupt:
    device.command(led.constants.MAX7219_REG_SHUTDOWN,0x00)
    time.sleep(0.01)

print("done")

Save this code. Execute this program by typing the following command:

sudo python ch04_03.py...

Building a ball reflection game


A dot matrix display module consists of LEDs. Each LED can act as a pixel and can be used as a ball. In this section, we build a ball reflection game. If a ball hits a corner, it will bounce back. The ball can move with a specific speed, which is extracted as horizontal speed (vx) and vertical speed (vy). The following is the formula for moving the ball:

pos_x = pos_x + (vx * direction)
pos_y = pos_y + (vy * direction)

This formula uses object moving-based vectors with speeds vx and vy direction is a direction orientation. If the direction value is 1, the ball will move from left to right. Otherwise, it will move from right to left.

Let's start to build a program. Create a file named ch04_04.py. Write the following completed code:

# ch04_04.py file

import max7219.led as led
import time


device = led.matrix(cascaded=1)

# you can change these initial data
pos_x = 4  # current position x
pos_y = 4  # current position y
last_x = pos_x # last position x
last_y ...

Cascading LED dot matrix modules


Sometimes you want to cascade several LED dot matrix display modules. If you have LED dot matrix display with the MAX7219 driver, it provides DIN and DOUT pins. These pins are used to cascade our LED dot matrix display.

For instance, we have two 8 x 8 LED dot matrix display modules. The first module is attached to Raspberry Pi as usual. For the second module, connect DIN to DOUT from the first module. The CS and CLK pins from the first module are connected to the CS and CLK pins from the second module.

Please note that if you cascade two 8 x 8 LED dot matrix display modules, the (0, 0) position is located on the second LED dot matrix module. The following is the wiring for two cascaded LED dot matrix modules:

  • All LED dot matrix VCC pins are connected to +5 V Raspberry Pi

  • All LED dot matrix GND pins are connected to GND Raspberry Pi

  • The DOUT pin from the first LED dot matrix display is connected to the DIN pin from the second LED dot matrix display

  • The DIN pin from...

Summary


We have used an 8 x 8 LED dot matrix display module on Raspberry Pi. The module uses IC MAX7219 as the driver to control the display of LEDs. We also built some programs to get more practices to use the 8 x 8 LED dot matrix display module.

In the next chapter, you will learn to build a light traffic controller by using Raspberry Pi. We will start with designing a light traffic controller and then implementing it.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Raspberry Pi LED Blueprints
Published in: Sep 2015Publisher: ISBN-13: 9781782175759
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Agus Kurniawan

Agus Kurniawan is an independent technology consultant, author, and lecturer. He has over 18 years' experience working on various software development projects, including delivering training courses and workshops, and delivering technical writing. He has done a few research activities related to wireless networking, software, and security in multiple universities. Currently, he is pursuing a Ph.D. program in Computer Science in Germany. He has previously written five books for Packt.
Read more about Agus Kurniawan