# 2004-LCD with PCF8574-I2C and wiringPi (Python)

I try to interface my 2004-LCD display with wiringPi (in Python 3). The display is connected to a PCF8574-chip, so I need to use I2C.

This is my test code:

import wiringpi2 as wiringpi

wiringpi.wiringPiSetupSys();

af_base = 100

af_rs = af_base + 0
af_e = af_base + 2
af_db4 = af_base + 4
af_db5 = af_base + 5
af_db6 = af_base + 6
af_db7 = af_base + 7

af_backlight = af_base + 3

def backlight_on():
wiringpi.digitalWrite(af_backlight, 1)

def backlight_off():
wiringpi.digitalWrite(af_backlight, 0)

wiringpi.pcf8574Setup(af_base, 0x27)

lcd_handle = wiringpi.lcdInit(4, 20, 4, af_rs, af_e, af_db4, af_db5, af_db6, af_db7, 0, 0, 0, 0)

backlight_on()

wiringpi.lcdPrintf(lcd_handle, "Hello World!")


The code runs without any errors. The backlight on the LCD first turns out and then turns on again, when I run backlight_on().

However, the text doesn't show up on the display.

To check if the LCD or the I2C-chip is damaged, I followed the instructions from this website.

Now comes the strange part:
When I now run the test code from above again, it works without any hassle.

It seems, the other project loads some stuff which is essential for controlling the LCD.

What is missing in my test code to interface the LCD in the first place?

• Presumably the other set of code is doing some required initialisation. – joan Sep 17 '15 at 21:43
• @joan I also think so. However, I looked through the other code and didn't find anything special... – speendo Sep 17 '15 at 21:44

I have exactly the same problem. I did some research and found out the following code works. I don't know why it works but it just works. Add the following to the beginning of your script:

port=1   # Depends on your pi rev. Run i2cdetect to find out
bus = smbus.SMBus(port)
bus.write_byte_data(self.i2caddr, self.port, 0x0)

Also you might want to force the RW pin to be LOW:

af_rw = pinbase + 1
wiringpi.pinMode(af_rw, 1)
wiringpi.digitalWrite(af_rw, 0)
• Update: just these two lines will work, bus = smbus.SMBus(port) bus.write_byte_data(self.i2caddr, self.port, 0x0) – Timothy Wong Mar 30 '16 at 23:05