Pi-Lite LED Matrix Board Python ‘Hello World’ Example

If you’ve followed the previous Pi-Lite setup article you should have a working Pi-Lite. The example Python scripts are great but I wanted to show a simple example of my own demonstrating the basics of sending scrolling text to the Pi-Lite.

Scrolling text is something that the on-board microcontroller handles for you so it really doesn’t involve much effort to do using a short Python script.

The following script scrolls two messages to demonstrate the technique.

#!/usr/bin/env python

import sys
import serial
import time

# Define message complete with
# carriage return at the end
message1 = "Hello World!\r"
message2 = "Pi-Lite messages are easy!\r"

# Configure Pi serial port
s = serial.Serial()
s.baudrate = 9600
s.timeout = 0
s.port = "/dev/ttyAMA0"

try:
    # Open serial port
    s.open()
except serial.SerialException, e:
    # There was an error
    sys.stderr.write("could not open port %r: %s\n" % (port, e))
    sys.exit(1)

print "Serial port ready"

# Clear display
s.write("$$$ALL,OFF\r")

# Send message 1 to the Pi-Lite
print message1
s.write(message1)

# Short delay to allow the
# 12 character message to finish
time.sleep(6)

# Send message 2 to the Pi-Lite
print message2
s.write(message2)

# Short delay to allow the
# 26 character message to finish
time.sleep(12)

print "Good bye"

You can create whatever messages you like and add more messages if required. The delays after each message give it time to finish scrolling before more data is sent to the Pi-Lite. If you don’t add the delay you risk sending data before the device has finished dealing with the previous set.

I use the following quick calculation to estimate the delay required :

delay = length x 5 x 0.08

Pi-Lite LED Matrix BoardLength is the number of characters in the message. 5 is the average number of columns per character. 0.08 is the default scroll speed (80ms). The delay is then the time it takes the total number of columns to scroll across the display. You can then round up the value to the nearest whole number of seconds. It is only an approximation and you may need to tweak as appropriate.

Sticking a message into a “while” loop would allow the message to repeat. For example you can replace the “Serial port ready” line in the above script with this code :

print "Serial port ready"

# Clear display
s.write("$$$ALL,OFF\r")

while True:

  # Send message 1 to the Pi-Lite
  print message1
  s.write(message1)

  # Short delay to allow the
  # 12 character message to finish
  time.sleep(6)

  # Send message 2 to the Pi-Lite
  print message2
  s.write(message2)

  # Short delay to allow the
  # 26 character message to finish
  time.sleep(12)

Here is a short video of the Pi-Lite in action using the “while” loop above :

This script is the basis by which you could start sending all sorts of information to the display e.g. time, temperature, web sourced data

Here are the scripts which you can download directly rather than cutting and pasting the examples above :

pi_lite_hello_world.py

pi_lite_hello_world_repeat.py

This entry was posted in Pi-Lite, Python, Tutorials & Help and tagged , , . Bookmark the permalink.

4 Responses to Pi-Lite LED Matrix Board Python ‘Hello World’ Example

  1. Pete says:

    Hey when I try to run the code it says error on line 3 import serial

    no module named serial?

    sorry im quite new to all this

  2. carlos hernandez says:

    hello, it is a good blog, but i have a question, if i want to comunicate 2 raspberrys for the serial port, what i can do?

  3. Tony Goodhew says:

    Your blogs are so much easier to follow than the ‘Official Documentation’.

    I had to add an extra line to get it to work:
    import sys
    at the top, after import time

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>