texy
Forum Moderator
Forum Moderator
Posts: 5160
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England

HD44780 Large 20 x 4 display board, I2C

Wed Jan 01, 2014 4:24 pm

Instructions for basic setup and configuration of my I2C driven LCD board.
Configuration details for LCDproc and/or Raspbmc can be found here : http://www.raspberrypi.org/forum/viewto ... 35&t=63412
Image
Image
SSH into the pi and install smbus and i2c-tools :

Code: Select all

sudo apt-get update 
sudo apt-get install python-smbus i2c-tools -y
Enable i2c on the pi :

Code: Select all

sudo nano /etc/modules
and add

Code: Select all

i2c-bcm2708
i2c-dev
Also the I2C bus may be blacklisted by default so you need to remove it from the blacklist by

Code: Select all

sudo nano /etc/modprobe.d/raspi-blacklist.conf
and putting a # at the beginning of the line 'blacklist i2c-bcm2708', so that it looks like this

Code: Select all

#blacklist i2c-bcm2708
After saving reboot the pi

Code: Select all

sudo reboot
Here is a python test program that displays on every line and detects button presses. It automatically detects if you are running a rev 1 or rev 2 pi also.

Code: Select all

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# working 24/11/13
# with backlight control
# and auto Pi Rev detection

import smbus
from time import *
import RPi.GPIO as GPIO

BackLight = True   # LCD backlight enable
Revision = 0       # Pi revision
UP=0
LEFT=0
DOWN=0
RIGHT=0
SELECT=0


# General i2c device class so that other devices can be added easily
class i2c_device:
   def __init__(self, addr, port):
      self.addr = addr
      self.bus = smbus.SMBus(port)

   def write(self, byte):
      if (BackLight == False):
         byte = byte & 0x7F # switch off backlight LED
      else:
         byte = byte | 0x80 # switch on backlight LED

      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
   '''
   pinout is as follows: PCF8574 - HD44780
   P0 - LCD11 - D4
   P1 - LCD12 - D5
   P2 - LCD13 - D6
   P3 - LCD14 - D7
   P4 - LCD4  - RS
   P5 - LCD5  - R/W
   P6 - LCD6  - E
   P7 - LCD15 - LED BACKLIGHT
   '''
   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(0x01)

   # 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(0x80)
      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)

   def test(self):
      print("")
      print("toggle E x 10")
      for i in range(10):
         print(".")
         self.lcd_device.write((self.lcd_device.read() | 0x40))
         sleep(1)
         self.lcd_device.write((self.lcd_device.read() & 0xbF))
         sleep(1)

      print("")
      print("toggle RS x 10")
      for i in range(10):
         print(".")
         self.lcd_device.write((self.lcd_device.read() | 0x10))
         sleep(1)
         self.lcd_device.write((self.lcd_device.read() & 0xEF))
         sleep(1)

def get_revision():
   with open('/proc/cpuinfo') as lines:
      for line in lines:
         if line.startswith('Revision'):
            return int(line[line.index(':') + 1:], 16) & 0xFFFF
   raise RuntimeError('No revision found.')

def get_board_revision():
   revision = get_revision()
   if revision in (2, 3):
      return 1
   else:
      return 2

def gpio_setup():
   global UP, DOWN, LEFT, RIGHT, SELECT
   if Revision==1:
      RIGHT=21
   else:
      RIGHT=27
   SELECT=4
   DOWN=17
   UP=22
   LEFT=23
   GPIO.setmode(GPIO.BCM)     # set up BCM GPIO numbering
   GPIO.setup(SELECT, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)    # set as input (Select button)
   GPIO.setup(UP, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)    # set as input (Up button)
   GPIO.setup(DOWN, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)    # set as input (Down button)
   GPIO.setup(RIGHT, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)    # set as input (Right button)
   GPIO.setup(LEFT, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)    # set as input (Left button)

def main():
      global Revision
      Revision = get_board_revision()
      print "get board revision=",Revision
      if Revision==1:
         i2cbus=0
      else:
         i2cbus=1
      print "i2cbus=",i2cbus
      device = lcd(0x20,i2cbus)
      gpio_setup()

      device.lcd_clear()
      device.lcd_puts("abcdefghijklmnopqrst",1)
      device.lcd_puts("1234567890abcdefghij",2)
      device.lcd_puts("ABCDEFGHIJKLMNOPQRST",3)
      device.lcd_puts("Line 4 - Hello !!!!!",4)
      sleep(3)
      device.lcd_clear()
      while True:

         if GPIO.input(SELECT): # if port 4 == 1
            device.lcd_puts("SELECT pressed   ",1)
            print "SELECT is 1/HIGH/True"
            if GPIO.input(UP):
               break # quit if SELECT an UP pressed at the same time
         elif GPIO.input(DOWN): # if port 17 == 1
            device.lcd_puts("DOWN pressed     ",1)
            print "DOWN is 1/HIGH/True"
         elif GPIO.input(RIGHT): # if port 21 or 27== 1
            device.lcd_puts("RIGHT pressed    ",1)
            print "RIGHT is 1/HIGH/True"
         elif GPIO.input(UP): # if port 22 == 1
            device.lcd_puts("UP pressed       ",1)
            print "UP is 1/HIGH/True"
         elif GPIO.input(LEFT): # if port 23 == 1
            device.lcd_puts("LEFT pressed     ",1)
            print "LEFT is 1/HIGH/True"
         else:
            device.lcd_puts("No button pressed",1)

if __name__ == '__main__':
    main()


Texy
Various male/female 40- and 26-way GPIO header for sale here ( IDEAL FOR YOUR PiZero ):
https://www.raspberrypi.org/forums/viewtopic.php?f=93&t=147682#p971555

cerebro
Posts: 17
Joined: Sun Feb 03, 2013 10:32 pm

Re: HD44780 Large 20 x 4 display board, I2C

Mon Jan 27, 2014 11:43 pm

Texy's screen also works great on Volumio, the updated version of RaspyFi.

Download and install Volumio to an SD card, I have used a 2GB one but it is almost full, so I would recommend at least a 4GB card just to allow room for further expansion.

Once installed I would recommend connecting via LAN at first boot and from within Volumio's network setting you can set up your Wi-Fi network as required.
Follow the instructions as per viewtopic.php?f=35&t=63412 to get LCDproc installed. Once you get to the end of the instructions do not reboot, you need to enter a few more commands to get the screen to show what’s playing.

Enter the following:

sudo apt-get install python-mpd
sudo apt-get install python-pip
sudo pip install mpdlcd
wget -O "/etc/mpdlcd.conf" https://raw.github.com/rbarrois/mpdlcd/ ... pdlcd.conf
sudo update-rc.d LCDd defaults


You now need to make mpdlcd start at boot so use this command

sudo nano /etc/init.d/bootmisc.sh

and add this line to the file

su pi -c "mpdlcd --no-syslog &"

Close and save the file, now reboot (sudo reboot) .
At start up you should have an lcd screen that will show the track info you are playing. Credit to Andypi for the MPDLCD instructions.

Track info for Mp3's works fine, but on web radio streams you will get the song title and artist and the stream time but I have not found a way to change the name of the station as they all show as <Unknown> , if anyone knows how to solve this I would be most grateful if they could share the knowledge.

atomic3
Posts: 99
Joined: Thu Jan 17, 2013 4:31 pm

Re: HD44780 Large 20 x 4 display board, I2C

Sun Jun 01, 2014 11:31 pm

I've tried setting up a 16x2 lcd using your tutorial, my pins are slightly different, how can I change them?

My P0 is actually VSS on the LCD.

cerebro
Posts: 17
Joined: Sun Feb 03, 2013 10:32 pm

Re: HD44780 Large 20 x 4 display board, I2C

Sat Jun 07, 2014 12:40 pm

atomic3 wrote:I've tried setting up a 16x2 lcd using your tutorial, my pins are slightly different, how can I change them?

My P0 is actually VSS on the LCD.
If you are using 16 x 2 try having a look at andypi.co.uk as he has a tutorial that includes the pins used to wire his screens.

Hope this helps

atomic3
Posts: 99
Joined: Thu Jan 17, 2013 4:31 pm

Re: HD44780 Large 20 x 4 display board, I2C

Mon Jun 09, 2014 4:39 pm

cerebro wrote:
atomic3 wrote:I've tried setting up a 16x2 lcd using your tutorial, my pins are slightly different, how can I change them?

My P0 is actually VSS on the LCD.
If you are using 16 x 2 try having a look at andypi.co.uk as he has a tutorial that includes the pins used to wire his screens.

Hope this helps
Unfortunately he's not using i2c PCF8574 which I have.

Thanks for the input though.

texy
Forum Moderator
Forum Moderator
Posts: 5160
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England

Re: HD44780 Large 20 x 4 display board, I2C

Mon Jun 09, 2014 4:50 pm

You need to tell us more about how your chip is wired to your LCD. Also take a look at this part of the code, and adjust accordingly :

Code: Select all

   pinout is as follows: PCF8574 - HD44780
   P0 - LCD11 - D4
   P1 - LCD12 - D5
   P2 - LCD13 - D6
   P3 - LCD14 - D7
   P4 - LCD4  - RS
   P5 - LCD5  - R/W
   P6 - LCD6  - E
   P7 - LCD15 - LED BACKLIGHT
I don't understand why P0 would be permanently connected to VSS?
Texy
Various male/female 40- and 26-way GPIO header for sale here ( IDEAL FOR YOUR PiZero ):
https://www.raspberrypi.org/forums/viewtopic.php?f=93&t=147682#p971555

User avatar
FTrevorGowen
Forum Moderator
Forum Moderator
Posts: 5271
Joined: Mon Mar 04, 2013 6:12 pm
Location: Bristol, U.K.
Contact: Website

Re: HD44780 Large 20 x 4 display board, I2C

Mon Jun 09, 2014 5:11 pm

texy wrote:You need to tell us more about how your chip is wired to your LCD. Also take a look at this part of the code, and adjust accordingly :

Code: Select all

   pinout is as follows: PCF8574 - HD44780
   P0 - LCD11 - D4
   P1 - LCD12 - D5
   P2 - LCD13 - D6
   P3 - LCD14 - D7
   P4 - LCD4  - RS
   P5 - LCD5  - R/W
   P6 - LCD6  - E
   P7 - LCD15 - LED BACKLIGHT
I don't understand why P0 would be permanently connected to VSS?
Texy
Hmm ... "the other configuration" . The two PCF8574-based backpacks I've used** have this setup:

Code: Select all

P0 - RS
P1 - RW
P2 - E
P3 - Backlight via transistor buffer? & removable link
P4 - D4
P5 - D5
P6 - D6
P7 - D7 
Trev.
** http://www.cpmspectrepi.webspace.virgin ... D_Backpack
Still running Raspbian Jessie or Stretch on some older Pi's (an A, B1, B2, B+, P2B, 3xP0, P0W, 2xP3A+, P3B+, P3B, B+, A+ and a B2) but Buster on the P4B's. See: https://www.cpmspectrepi.uk/raspberry_pi/raspiidx.htm

User avatar
Douglas6
Posts: 4801
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: HD44780 Large 20 x 4 display board, I2C

Mon Jun 09, 2014 6:24 pm

Is atomic3 confusing P0 (the expander chip) with LCD0 (the LCD module), which IS Vcc?

Nyonath
Posts: 1
Joined: Thu Nov 27, 2014 3:41 pm

Re: HD44780 Large 20 x 4 display board, I2C

Thu Nov 27, 2014 3:48 pm

Hi there =)

I also tried to use this nice .py to control my display.

i also use i2c and pcf8574.

The only difference is my pin setup.

This is the standard setup:
pinout is as follows: PCF8574 - HD44780
P0 - LCD11 - D4
P1 - LCD12 - D5
P2 - LCD13 - D6
P3 - LCD14 - D7
P4 - LCD4 - RS
P5 - LCD5 - R/W
P6 - LCD6 - E
P7 - LCD15 - LED BACKLIGHT

But my setup is like

pinout is as follows: PCF8574 - HD44780
P0 - LCD11 - D4
P1 - LCD12 - D5
P2 - LCD13 - D6
P3 - LCD14 - D7
P4 - NOT USED FOR LCD R/W is on ground cause i dont need to read only write.
P5 - LCD5 - RS
P6 - LCD6 - E
P7 - LCD15 - LED BACKLIGHT

I tried to follow through programm with print s but the commands are shifted and operated with &, | so i cant fix the values to get it work. Display is flashing and blinking but not chars.

i also but all that gpio out cause i dont need it.


please help

User avatar
FTrevorGowen
Forum Moderator
Forum Moderator
Posts: 5271
Joined: Mon Mar 04, 2013 6:12 pm
Location: Bristol, U.K.
Contact: Website

Re: HD44780 Large 20 x 4 display board, I2C

Fri Nov 28, 2014 8:55 am

FWIW, I've recently been testing a number of variations of the "4-bit interface", all with the LCD Read/¬Write line tied low ie. "Write only". This was mainly to check timing issues**** with various "port-expanding" chips. The schematics etc. can be found at:
http://www.cpmspectrepi.webspace.virgin ... alLCD.html
http://www.cpmspectrepi.webspace.virgin ... ut2p1.html
However, since I code in 'C', not Python I'm not sure whether that's of any help but, again, FWIW, a "snapshot" of the tarball can be found here**:
http://www.cpmspectrepi.webspace.virgin ... tware.html
Trev.
** or P.M. with contact details and I will e-mail the most up-to-date version.
Edit:
**** As posted here: http://www.raspberrypi.org/forums/viewt ... 00#p641100
Still running Raspbian Jessie or Stretch on some older Pi's (an A, B1, B2, B+, P2B, 3xP0, P0W, 2xP3A+, P3B+, P3B, B+, A+ and a B2) but Buster on the P4B's. See: https://www.cpmspectrepi.uk/raspberry_pi/raspiidx.htm

cerebro
Posts: 17
Joined: Sun Feb 03, 2013 10:32 pm

Re: HD44780 Large 20 x 4 display board, I2C

Sun Sep 25, 2016 11:01 pm

Hi Texy,

Does this code still work for Raspbain Jessie? Or do things need to changed to make it work?

Thanks in advance

Return to “HATs and other add-ons”