To setup your Raspberry Pi to use I2C (needed by the display module) follow the instructions here.
Please follow the steps here to install the Adafruit I2C library and PIL (Python Imaging Library).
You may also wish to try out some of the examples in the latter guide to check that I2C and the LED Matrix display is working correctly.
The following example takes the display examples a bit further, allowing you to display scrolling text on them.
Open an editor window using:

nano scrolling_clock.py
Then paste in the following text and save the file using CTRL-X and then Y and then ENTER.
import time
from datetime import datetime
from PIL import ImageFont
from PIL import Image
from PIL import ImageDraw
from Adafruit_LED_Backpack import Matrix8x8

display = Matrix8x8.Matrix8x8()
display.begin()
 
font = ImageFont.truetype("/usr/share/fonts/truetype/freefont/FreeSansBold.ttf", 9)
 
im = Image.new("1", (8, 8), "black")
draw = ImageDraw.Draw(im)
width, ignore = font.getsize("88 : 88 : 88")

def format_time():
    d = datetime.now()
    return "{:%H : %M : %S}".format(d)

message = format_time()
x = 8
while True:
    x = x - 1
    if x < -(width + 20):
        x = 8
        message = format_time()
    draw.rectangle((0, 0, 7, 7), outline=0, fill=0)
    draw.text((x, -1), message, 1, font=font)
    display.set_image(im)
    display.write_display()
    time.sleep(0.1)
You can now run the program using the command:
sudo python scrolling_clock.py
The time should now slowly scroll across the display.
The code uses a number of libraries to do its job. The Adafruit_LED_Backpack library handles the low level interface to the matrix display. To be able to write text onto the display, we need to use the Python Imaging Library (PIL) and write the text to an image that can then be written to the display.
After initializing the display, the Truetype font FreeSansBold size 9 point is loaded. This seemed to work the best from the pre-installed fonts. This can be changed to one of the other fonts in the directory /usr/share/fonts/truetype/freefont/ if you want to experiment. The fonts are really not intended to work in 8x8 pixels, so the results are legible but not great.
An 8x8 image is created with a bit depth of 1. We are just going to write text in green for now. The following command works out how many pixels wide the text for the time is going to be:

width, ignore = font.getsize("88 : 88 : 88")
The formatTime function just returns the current time formatted for the display.
The main loop of the program produces the scrolling text effect. It does this by writing the text of the time onto the display with a decreasing x offset, so that the message appears to move. Most of the time, the value of x will be negative (off the left of the display), but the draw.text function automatically crops to the visible part of the display.

This guide was first published on Jul 23, 2014. It was last updated on Mar 08, 2024.

This page (Software) was last updated on Jul 23, 2014.

Text editor powered by tinymce.