Hitachi HD44780 based 16x2 character LCD are very cheap and widely available, and is a essential part for any projects that displays information. Using the I2C bus on Raspberry Pi ,PCF8574 IC, and Python characters/strings can be displayed on the LCD. The PCF8574 is an general purpose bidirectional 8 bit I/O port expander that uses the I2C protocol.
The LCD(HD44780) is connected in 4 bit mode as follows to the PCF8574:-

P0 - D4
P1 - D5
P2 - D6
P3 - D7
P4 - RS
P5 - R/W
P6 - E
Port A0 is connected to VCC(5V) with a 10k resistor so that it will be addressed at 0x21.
Coming to the software part, Python is used to drive the logic.I have written a simple library to communicate with the LCD using the I2C bus.For this code to work python-smbus package must be installed(sudo apt-get install python-smbus).Save the below code as pylcdlib.py.
import smbus
from time import *
# General i2c device class so that other devices can be added easily
class i2c_dev:
def __init__(self,addr,port):
self.addr=addr
self.bus=smbus.SMBus(port)
def write(self,byte):
self.bus.write_byte(self.addr,byte)
def read(self):
return self.bus.read_byte(self.addr)
def read_nbytes_data(self,data,n): # For sequential reads>1 byte
return self.bus.read_i2c_block_data(self.addr,data,n)
class lcd:
#initializes objects and lcd
def __init__(self,addr,port):
self.lcd_device=i2c_device(addr,port)
self.lcd_device.write(0x03)
self.lcd_strobe()
sleep(0.0005)
self.lcd_strobe()
sleep(0.0005)
self.lcd_strobe()
sleep(0.0005)
self.lcd_device.write(0x02)
self.lcd_strobe()
sleep(0.0005)
self.lcd_write(0x28)
self.lcd_write(0x08)
self.lcd_write(0x01)
self.lcd_write(0x06)
self.lcd_write(0x0C)
self.lcd_write(0x0F)
# clocks EN to latch command
def lcd_strobe(self):
self.lcd_device.write((self.lcd_device.read()|0x40))
self.lcd_device.write((self.lcd_device.read()&0xbF))
# write a command to lcd
def lcd_write(self,cmd):
self.lcd_device.write((cmd>>4))
hi= self.lcd_device.read()
self.lcd_strobe()
self.lcd_device.write((cmd&0x0F))
lo= self.lcd_device.read()
self.lcd_strobe()
self.lcd_device.write(0x0)
print 'cmd',cmd,hi,lo
# write a character to lcd (or character rom)
def lcd_write_char(self,charvalue):
print "char",charvalue
self.lcd_device.write((0x10|(charvalue>>4)))
self.lcd_strobe()
self.lcd_device.write((0x10|(charvalue&0x0F)))
self.lcd_strobe()
self.lcd_device.write(0x0)
# put char function
def lcd_putc(self,char):
self.lcd_write_char(ord(char))
# put string function
def lcd_puts(self, string,line):
if line==1:
self.lcd_write(0x10)
if line==2:
self.lcd_write(0xC0)
if line==3:
self.lcd_write(0x94)
if line==4:
self.lcd_write(0xD4)
for char in string:
self.lcd_putc(char)
# clear lcd and set to home
def lcd_clear(self):
self.lcd_write(0x1)
self.lcd_write(0x2)
# add custom characters(0-7)
def lcd_load_custon_chars(self,fontdata):
self.lcd_device.bus.write(0x40);
for char in fontdata:
for line in char:
self.lcd_write_char(line)
import pylcdlib
lcd=pylcd2.lcd(0x21,0,2)
lcd.lcd_clear()
lcd.lcd_puts("Raspberry Pi",1) #display "Raspberry Pi" on line 1
lcd.lcd_puts("Hello World!",2) #display "Hello World!" on line 2




