Python Script

NOTE: This page describes an older version of the Raspberry Pi character LCD Python code.

You can find a newer library that works on more platforms like the Raspberry Pi and BeagleBone Black by checking out the new character LCD Python library guide. For new projects it's recommended to use the new library!

The Code


The Python code for Adafruit's Character LCDs on the Pi is available on Github at  https://github.com/adafruit/Adafruit-Raspberry-Pi-Python-Code

There are two files we will be working with:

  1. Adafruit_CharLCD.py - contains python class for LCD control
  2. Adafruit_CharLCD_IPclock_example.py - code for IP address and date/time calls methods from Adafruit_CharLCD.py

The first file, Adafruit_CharLCD.py is a mashup from two different sources of LCD code. Github user lrvick put together a nice python class. His work is the baseline that is slowly being changed as we are bringing in more elements from the Arduino LiquidCrystal library

The easiest way to get the code onto your Pi is to hook up an Ethernet cable, and clone it directly using 'git', which is installed by default on most distros.  Simply run the following commands from an appropriate location (ex. /home/pi):

apt-get install git
git clone git://github.com/adafruit/Adafruit-Raspberry-Pi-Python-Code.git
cd Adafruit-Raspberry-Pi-Python-Code
cd Adafruit_CharLCD

Testing

 You can test the wiring from the previous step by simply running the  Adafruit_CharLCD.py Python code, as it has a little code it in that will simply display a test message when wired correctly

You can use nano Adafruit_CharLCD.py to edit.

chmod +x Adafruit_CharLCD.py
sudo ./Adafruit_CharLCD.py

IP Clock Example

This script assumes you'll want to display the Ethernet (eth0) IP address. You may want to replace eth0 with wlan0 or wlan1 (etc) for Wireless IP address!
#!/usr/bin/python

from Adafruit_CharLCD import Adafruit_CharLCD
from subprocess import *
from time import sleep, strftime
from datetime import datetime

lcd = Adafruit_CharLCD()

cmd = "ip addr show eth0 | grep inet | awk '{print $2}' | cut -d/ -f1"

lcd.begin(16,1)

def run_cmd(cmd):
        p = Popen(cmd, shell=True, stdout=PIPE)
        output = p.communicate()[0]
        return output

while 1:
        lcd.clear()
        ipaddr = run_cmd(cmd)
        lcd.message(datetime.now().strftime('%b %d  %H:%M:%S\n'))
        lcd.message('IP %s' % ( ipaddr ) )
        sleep(2)

Running the Code

Start the IP + Clock example

$ sudo ./Adafruit_CharLCD_IPclock_example.py 

What You Should See

Last updated on 2015-11-23 at 05.26.19 PM Published on 2012-08-17 at 06.32.36 PM