Raspberry PI max7219 8×8 LED Matrix – scrolling digital clock

Based on these fine instructions by raspi.tv I wrote some tiny python code to display a scrolling digit clock on an 8 by 8 LED matrix using the Raspberry PI.

The datatype of font5x3 = {“…” : …} is a so-called dictionary and works intuitively. The binary notation such as 0b01110 makes it easy to conceive and edit the pixel based graphics of the font.

My functions creatematrix() and drawmatrix() can also be used for different purpose such as scrolling a text. They can be nested: drawmatrix(creatematrix(“Hello World”), 0, i)
You can easily extend the font5x3 with additional characters. different size such as 8×8 will work seamlessly.

#!/usr/bin/env python

import max7219.led as led
import max7219.canvas as canvas
import time

font5x3 = { # python data type dictionary for the pixelfont
    "0" : [0b01110, 0b10001, 0b01110], # "0"
    "1" : [0b10010, 0b11111, 0b10000], # "1"
    "2" : [0b11001, 0b10101, 0b10010], # "2"
    "3" : [0b10001, 0b10101, 0b01110], # "3"
    "4" : [0b01110, 0b01000, 0b11111], # "4"
    "5" : [0b10111, 0b10101, 0b01001], # "5"
    "6" : [0b01110, 0b10101, 0b01000], # "6"
    "7" : [0b10001, 0b01001, 0b00111], # "7"
    "8" : [0b01010, 0b10101, 0b01010], # "8"
    "9" : [0b00010, 0b10101, 0b01110], # "9"
    ":" : [0b01010], # ":"
    "-" : [0b00100, 0b00100, 0b00100], # "-"
    "|" : [      0], # one blank line
    " " : [      0,       0,       0] # space
    } # create additional letters

def creatematrix(text):
    text = str(text)
    matrix = []
    for i in range(len(text)): # write complete pixelmatrix in a buffer
        if text[i].upper() in font5x3: # check if dictionary entry exists
            matrix = matrix + font5x3[text[i].upper()] # add letter; upper()
            matrix = matrix + [0] # add separator/space
    return matrix

def drawmatrix(matrix, up=0, left=0):
    for i in range(8): # fill the 8x8 matrix buffer canvas.gfxbuf
        if up < 0:
            canvas.gfxbuf[i] = matrix[(i + left) % len(matrix)] << abs(up) # move down
        else:
    canvas.gfxbuf[i] = matrix[(i + left) % len(matrix)] >> abs(up)

led.init()
horz = 0
vert = -2
speed = 15
led.brightness(0) # 0, 3, 7, 15 seems to work

while True:
    m = creatematrix(time.strftime("%k:%M ")) # write temporary matrix m
    horz = (horz + 1) % len(m) # scroll left
    drawmatrix(m, vert, horz) # draw 8x8 frame into gfxbuf 
    canvas.render() # draw gfxbuf into matrix
    time.sleep( 1.0 / speed ) # pause

If you like to include my small 5×3 pixel alphabeth, you can use the following font5x3 definition:

font5x3 = {
    "0" : [0b11111, 0b10001, 0b11111], # "0"
    "1" : [0b10010, 0b11111, 0b10000], # "1"
    "2" : [0b11001, 0b10101, 0b10111], # "2"
    "3" : [0b10101, 0b10101, 0b01110], # "3"
    "4" : [0b01110, 0b01000, 0b11111], # "4"
    "5" : [0b10111, 0b10101, 0b11001], # "5"
    "6" : [0b01110, 0b10101, 0b01000], # "6"
    "7" : [0b10001, 0b01001, 0b00111], # "7"
    "8" : [0b01010, 0b10101, 0b01010], # "8"
    "9" : [0b00010, 0b10101, 0b01110], # "9"

    "A" : [0b11111, 0b01001, 0b11110], # "A"
    "B" : [0b11111, 0b10101, 0b01010], # "B"
    "C" : [0b01110, 0b10001, 0b10001], # "C"
    "D" : [0b11111, 0b10001, 0b01110], # "D"
    "E" : [0b01110, 0b10101, 0b10101], # "E"
    "F" : [0b11110, 0b00101, 0b00101], # "F"
    "G" : [0b01110, 0b10001, 0b11101], # "G"
    "H" : [0b11111, 0b00100, 0b11111], # "H"
    "I" : [0b10001, 0b11111, 0b10001], # "I"
    "J" : [0b10000, 0b10000, 0b01111], # "J"
    "K" : [0b11111, 0b00100, 0b11011], # "K"
    "L" : [0b01111, 0b10000, 0b10000], # "L"
    "M" : [0b11111, 0b00010, 0b11111], # "M"
    "N" : [0b11111, 0b00001, 0b11110], # "N"
    "O" : [0b01110, 0b10001, 0b01110], # "O"
    "P" : [0b11111, 0b01001, 0b00110], # "P"
    "Q" : [0b01110, 0b10001, 0b01111], # "Q"
    "R" : [0b11111, 0b01001, 0b10110], # "R"
    "S" : [0b10010, 0b10101, 0b01001], # "S"
    "T" : [0b00001, 0b11111, 0b00001], # "T"
    "U" : [0b01111, 0b10000, 0b01111], # "U"
    "V" : [0b00111, 0b11000, 0b00111], # "V"
    "W" : [0b11111, 0b01000, 0b11111], # "W"
    "X" : [0b11011, 0b00100, 0b11011], # "X"
    "Y" : [0b00011, 0b11100, 0b00011], # "Y"
    "Z" : [0b11001, 0b10101, 0b10011], # "Z"

    "." : [0b10000], # "."
    "?" : [0b10101, 0b00101, 0b00010], # "?"
    "!" : [0b10111], # "!"
    ";" : [0b11010], # ";"
    "," : [0b11000], # ","
    ":" : [0b01010], # ":"
    "-" : [0b00100, 0b00100, 0b00100], # "-"
    "|" : [ 0],
    " " : [ 0, 0, 0]
    }

The code is not yet ready for multiple 8×8 LED matrices. You can try to work out the following instructions on multiple LED matrices: http://www.tutorials-raspberrypi.de/allgemein/bibliothek-fuer-mehrzeilige-m-x-n-max7219-led-matrizen/

Advertisements

5 thoughts on “Raspberry PI max7219 8×8 LED Matrix – scrolling digital clock

  1. I get this error;
    Traceback (most recent call last):
    File “clock.py”, line 4, in
    import max7219.canvas as canvas
    ImportError: No module named canvas

    ——
    How to fix error?

    Like

    1. I think you might wanna edit the setup.py to recognize the subfolders. canvas.py is in the folder ~/max7219/src

      #!/usr/bin/env python

      from distutils.core import setup,Extension
      setup(
      name = "max7219",
      version = "0.0.1",
      author = "Richard Hull",
      author_email = "richard.hull@destructuring-bind.org",
      description = ("A small library to drive a MAX7219 LED serializer using hardware SPI-Py"),
      license = "GPLv2",
      keywords = "raspberry pi rpi led max7219",
      url = "https://github.com/rm-hull/max7219",
      packages=['/home/pi/max7219'],
      package_dir={'home/pi/max7219': '/home/pi/max7219/src'},
      ext_modules=[Extension('max7219.spi',['/home/pi/max7219/src/spi.c'])]
      )

      Like

  2. Obviously the git resources for use of the max7219 matrix have been altered. The programmer known as rm-hull has removed the folder src and made another called max7219/max7219. Also the library canvas is removed an integrated into the main lib led.
    The new library now supports daisy chaining of multiple 8×8 matrices. Unfortunately I will have to alter my scripts as well.
    As simple adapted script would be the following to be used in the command line with your text of choice as an argumend:

    nano matrix-text.py


    #!/usr/bin/env python
    import max7219.led as led
    import time
    from max7219.font import proportional, TINY_FONT
    import sys
    device = led.matrix(cascaded=1)
    device.brightness(0)
    if len(sys.argv) > 1: device.show_message(str(sys.argv[1]) + " ", font=proportional(TINY_FONT))

    use now:

    pi@raspberrypi ~ $ python matrix-text.py "Hello, it's $(date +'%H:%M')"

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s