Below find a very rough Python script to scroll text on the Parallax LCD. You need to set your LCD's char width before you run it (i.e., SCREEN_WIDTH). You'll also see how to turn on the back light using Python in the script. Make sure you properly set the baudrate to 9600 using the switches on the back of the LCD device.
You'll need to download and install pyserial if you haven't already:
http://pyserial.sourceforge.net/pyserial.html You'll also have to run the Python script below as root, or add your pi username ('pi' by default) to the dialout group:
Code: Select all
sudo usermod -a -G dialout username
Attach your pi's ground, 5v, and serial out (TxD) to the appropriate pins on the back of the Parallax LCD device and you're good to go. Pins vary depending on what rev you have of the RPi, so check your version's pins just to be sure at:
http://wiringpi.com/pins/
Code: Select all
import serial,time
# Set screen width in characters
SCREEN_WIDTH = 16
# Initialize serial connection
ser = serial.Serial(port='/dev/ttyAMA0', baudrate=9600)
# Set display to with cursor blink (25 for blinking cursor)
ser.write(chr(24))
# Set backlight to true
ser.write(chr(17))
# Clear screen; we must pause at least 5 ms after this command
ser.write(chr(12))
time.sleep(0.01)
def scrolltext(text):
# Move cursor to far right of screen, in preparation for scrolling
cursor_start = 143
# Initialize window's head and tail
head = tail = 0
while tail <= len(text):
# Actually move the cursor to the far right as set above
ser.write(chr(cursor_start))
# Write out text "window"
ser.write(text[head:tail])
# Move the start cursor depending on whether or not the text
# has reached the far left of the screen, as it scrolls to the left
cursor_start = cursor_start - 1 if not cursor_start <= 128 else 128
# Updated window tail
tail += 1
# Update window head
head = head + 1 if tail >= SCREEN_WIDTH else 0
time.sleep(0.4)
# Now scroll some text
scrolltext("Now let's test this thing")