16×2 LCD Connecting To Raspberry

0

16×2 LCD Connecting To Raspberry

Connecting LCD Raspberry Pi

The next step once a user has got a good understanding of using Raspeberry Pi with LEDs, switches and motors, it is always interesting to connect an LCD with Raspberry Pi. The module of an LCD is not very expensive and can be purchased for nearly $10. I consist of 16 pins, however, our usage is limited to six pins only.

The best part is that mostly all the 16X2 LCD uses the same controller which is Hitachi HD44780 and hence, you can buy any LCD and expect it to operate in the same manner.

HITACHI HD44780

Step 1:

Connections

Connections

The HD44780 is connected with Raspberry Pi using an I2C IC. An eight bit PCF8574 IC can be used here.

IC PCF8574

The diagrammatic representation of the connected circuit can be viewed in the below figure:

Step 2:

Program
Program

The program can be written in Python programming language. The following code can be used in order to communicate with the LCD using the I2C bus.

  1. import smbus
  2. from time import *
  3. # General i2c device class so that other devices can be added easily
  4. class i2c_device:
  5.  def __init__(self, addr, port):
  6.   self.addr = addr
  7.   self.bus = smbus.SMBus(port)
  8.  def write(self, byte):
  9.   self.bus.write_byte(self.addr, byte)
  10.  def read(self):
  11.   return self.bus.read_byte(self.addr)
  12.  def read_nbytes_data(self, data, n): # For sequential reads > 1 byte
  13.   return self.bus.read_i2c_block_data(self.addr, data, n)
  14. class lcd:
  15.  #initializes objects and lcd
  16.  ”’
  17.  Reverse Codes:
  18.  0: lower 4 bits of expander are commands bits
  19.  1: top 4 bits of expander are commands bits AND P0-4 P1-5 P2-6
  20.  2: top 4 bits of expander are commands bits AND P0-6 P1-5 P2-4
  21.  ”’
  22.  def __init__(self, addr, port, reverse=0):
  23.   self.reverse = reverse
  24.   self.lcd_device = i2c_device(addr, port)
  25.   if self.reverse:
  26.    self.lcd_device.write(0x30)
  27.    self.lcd_strobe()
  28.    sleep(0.0005)
  29.    self.lcd_strobe()
  30.    sleep(0.0005)
  31.    self.lcd_strobe()
  32.    sleep(0.0005)
  33.    self.lcd_device.write(0x20)
  34.    self.lcd_strobe()
  35.    sleep(0.0005)
  36.   else:
  37.    self.lcd_device.write(0x03)
  38.    self.lcd_strobe()
  39.    sleep(0.0005)
  40.    self.lcd_strobe()
  41.    sleep(0.0005)
  42.    self.lcd_strobe()
  43.    sleep(0.0005)
  44.    self.lcd_device.write(0x02)
  45.    self.lcd_strobe()
  46.    sleep(0.0005)
  47.   self.lcd_write(0x28)
  48.   self.lcd_write(0x08)
  49.   self.lcd_write(0x01)
  50.   self.lcd_write(0x06)
  51.   self.lcd_write(0x0C)
  52.   self.lcd_write(0x0F)
  53.  # clocks EN to latch command
  54.  def lcd_strobe(self):
  55.   if self.reverse == 1:
  56.    self.lcd_device.write((self.lcd_device.read() | 0x04))
  57.    self.lcd_device.write((self.lcd_device.read() & 0xFB))
  58.   if self.reverse == 2:
  59.    self.lcd_device.write((self.lcd_device.read() | 0x01))
  60.    self.lcd_device.write((self.lcd_device.read() & 0xFE))
  61.   else:
  62.    self.lcd_device.write((self.lcd_device.read() | 0x10))
  63.    self.lcd_device.write((self.lcd_device.read() & 0xEF))
  64.  # write a command to lcd
  65.  def lcd_write(self, cmd):
  66.   if self.reverse:
  67.    self.lcd_device.write((cmd >> 4)<<4)
  68.    self.lcd_strobe()
  69.    self.lcd_device.write((cmd & 0x0F)<<4)
  70.    self.lcd_strobe()
  71.    self.lcd_device.write(0x0)
  72.   else:
  73.    self.lcd_device.write((cmd >> 4))
  74.    self.lcd_strobe()
  75.    self.lcd_device.write((cmd & 0x0F))
  76.    self.lcd_strobe()
  77.    self.lcd_device.write(0x0)
  78.  # write a character to lcd (or character rom)
  79.  def lcd_write_char(self, charvalue):
  80.   if self.reverse == 1:
  81.    self.lcd_device.write((0x01 | (charvalue >> 4)<<4))
  82.    self.lcd_strobe()
  83.    self.lcd_device.write((0x01 | (charvalue & 0x0F)<<4))
  84.    self.lcd_strobe()
  85.    self.lcd_device.write(0x0)
  86.   if self.reverse == 2:
  87.    self.lcd_device.write((0x04 | (charvalue >> 4)<<4))
  88.    self.lcd_strobe()
  89.    self.lcd_device.write((0x04 | (charvalue & 0x0F)<<4))
  90.    self.lcd_strobe()
  91.    self.lcd_device.write(0x0)
  92.   else:
  93.    self.lcd_device.write((0x40 | (charvalue >> 4)))
  94.    self.lcd_strobe()
  95.    self.lcd_device.write((0x40 | (charvalue & 0x0F)))
  96.    self.lcd_strobe()
  97.    self.lcd_device.write(0x0)
  98.  # put char function
  99.  def lcd_putc(self, char):
  100.   self.lcd_write_char(ord(char))
  101.  # put string function
  102.  def lcd_puts(self, string, line):
  103.   if line == 1:
  104.    self.lcd_write(0x80)
  105.   if line == 2:
  106.    self.lcd_write(0xC0)
  107.   if line == 3:
  108.    self.lcd_write(0x94)
  109.   if line == 4:
  110.    self.lcd_write(0xD4)
  111.   for char in string:
  112.    self.lcd_putc(char)
  113.  # clear lcd and set to home
  114.  def lcd_clear(self):
  115.   self.lcd_write(0x1)
  116.   self.lcd_write(0x2)
  117.  # add custom characters (0 – 7)
  118.  def lcd_load_custon_chars(self, fontdata):
  119.   self.lcd_device.bus.write(0x40);
  120.   for char in fontdata:
  121.    for line in char:
  122.     self.lcd_write_char(line)

The main code is:

  1. import pylcdlib
  2. lcd = pylcdlib.lcd(0x21,0)
  3. lcd.lcd_puts(“Raspberry Pi”,1)  #display “Raspberry Pi” on line 1
  4. lcd.lcd_puts(”  Take a byte!”,2)  #display “Take a byte!” on line 2

User can copy and paste it and save the file as test_lcd.py. However, the above code considers that the first four ports of IC are connected with 4 bits of LCD and the next 3 ports i.e. P4, P5, P6 are connected to RS, R/W and E respectively. LCD with different pin layout may require modification in the above code.

Share.

About Author

Leave A Reply