Google

Interfacing 16×2 LCD with Raspberry Pi using GPIO & Python

Arduino Battery Projects LCD
Written on:September 1, 2013
Comments
Add One

In this post I will be using the RPi.GPIO library and Python to control the LCD.The LCD used in this post is based on Hitachi HD44780 LCD controller. Although the LCD has 16 pins available for interfacing, using the 4 bit mode only 6 GPIO pins are required ( RS,E,D4,D5,D6,D7).

Interfacing 16x2 LCD with Raspberry Pi using GPIO & Python

LCD Pin    Pi Pin
01  <——>  GPIO-06
02  <——>  GPIO-02
03  <——>  GPIO-06
04  <——>  GPIO-26
05  <——>  GPIO-06
06  <——>  GPIO-24
07
08
09
10
11  <——>  GPIO-22
12  <——>  GPIO-18
13  <——>  GPIO-16
14  <——>  GPIO-12
15  +5V
16  <——>  GPIO-06

NOTE : With the help of  RW pin the device can be set to read/write mode.Setting [R/W=0] will write to the register and setting [R/W=1] will read from the register.To display data on LCD read access is not required,so the RW in connected to GND. This ensures that there is no outbound data from HD44780 as Pi cannot tolerate 5V.

You can check the pinout of Pi from here.

Code:-

HD44780 based display can be controlled using any programming environment.Here I have used Python & RPi.GPIO library to provide access to the GPIO.

  1. #!/usr/bin/python
  2. import RPi.GPIO as GPIO
  3. from time import sleep
  4. class HD44780:
  5.     def __init__(self, pin_rs=7, pin_e=8, pins_db=[25, 24, 23, 18]):
  6.         self.pin_rs=pin_rs
  7.         self.pin_e=pin_e
  8.         self.pins_db=pins_db
  9.         GPIO.setmode(GPIO.BCM)
  10.         GPIO.setup(self.pin_e, GPIO.OUT)
  11.         GPIO.setup(self.pin_rs, GPIO.OUT)
  12.         for pin in self.pins_db:
  13.             GPIO.setup(pin, GPIO.OUT)
  14.         self.clear()
  15.     def clear(self):
  16.         ”"” Blank / Reset LCD ”"”
  17.         self.cmd(0×33) # $33 8-bit mode
  18.         self.cmd(0×32) # $32 8-bit mode
  19.         self.cmd(0×28) # $28 8-bit mode
  20.         self.cmd(0x0C) # $0C 8-bit mode
  21.         self.cmd(0×06) # $06 8-bit mode
  22.         self.cmd(0×01) # $01 8-bit mode
  23.     def cmd(self, bits, char_mode=False):
  24.         ”"” Send command to LCD ”"”
  25.         sleep(0.001)
  26.         bits=bin(bits)[2:].zfill(8)
  27.         GPIO.output(self.pin_rs, char_mode)
  28.         for pin in self.pins_db:
  29.             GPIO.output(pin, False)
  30.         for i in range(4):
  31.             if bits[i] == ”1″:
  32.                 GPIO.output(self.pins_db[::-1][i], True)
  33.         GPIO.output(self.pin_e, True)
  34.         GPIO.output(self.pin_e, False)
  35.         for pin in self.pins_db:
  36.             GPIO.output(pin, False)
  37.         for i in range(4,8):
  38.             if bits[i] == ”1″:
  39.                 GPIO.output(self.pins_db[::-1][i-4], True)
  40.         GPIO.output(self.pin_e, True)
  41.         GPIO.output(self.pin_e, False)
  42.     def message(self, text):
  43.         ”"” Send string to LCD. Newline wraps to second line”"”
  44.         for char in text:
  45.             if char == ’\n’:
  46.                 self.cmd(0xC0) # next line
  47.             else:
  48.                 self.cmd(ord(char),True)
  49. if __name__ == ’__main__’:
  50.     lcd = HD44780()
  51.     lcd.message(“Raspberry Pi\n  Take a byte!”)
Interfacing 16x2 LCD with Raspberry Pi using GPIO & Python

Leave a Comment