01 Feb

Lcd Interfacing with Raspberry Pi

Requirements:
  • A Raspberry Pi with SD card preinstalled with Raspbian
  • LCD Display [ 16×2 ]
  • Power Supply

images

 

LCD Display (16X2):  LCD (Liquid Crystal Display) is an electronic display module and find a wide range of applications. A 16×2 LCD display is very basic module and is very commonly used in various devices and circuits. These modules are preferred over Seven Segment and Leds.

The reasons being: LCDs are economical; easily programmable; have no limitation of displaying special & even Custom Characters and animations unlike Seven Segment and Leds.
A 16×2 LCD provides 2 Rows and 16 Columns, that means you can display 32 characters at a time. In this LCD each character is displayed in 5×7 pixel matrix.
LCD has two registers: 
  1. Command
  2. Data
Command Register: 
Command register stores the command instruction to given to LCD like LCD initializing, LCD clear, Data shifting…etc
Data Register:

The data register stores the data to be displayed on the LCD. The data is the ASCII value of the character to be displayed on the LCD.LCD Display Address:In 16×2 LCD, First Row address starts from 0x80 to 0x8F and Second Row address starts from 0xC0 to 0xCF.


LCD Commands:

PIN OUT Description:

LCD CONTROL PINS:

1. RS (Register Select Pin): 

         RS = 0 For LCD Command

         RS = 1 For LCD Data2. R/W (Read / Write):         R/W = 0 For Data Write

         R/W = 1 For Data Read

3. EN (Enable):

         When High to Low pulse pass on this pin then data will be latched on LCD.

Usually the device requires 8 data lines to provide data to bits DB0 – DB7. However the device can be set to a “4 bit” mode which allows you to send data in two chunks (or nibbles)

of 4 bits. This is great as it reduces the number of GPIO connections you require when interfacing with your Pi.

 CIRCUIT Diagram:

Software / Coding:

You can use leafpad to write python code, after writing complete code, Save your code 

with .py extension like lcd.py

# ———–Code Start——————
import RPi.GPIO as GPIO
import time
# Define GPIO to LCD mapping
LCD_RS = 7
LCD_E  = 8
LCD_D4 = 25
LCD_D5 = 24
LCD_D6 = 23
LCD_D7 = 18
# Define some device constants
LCD_WIDTH = 16    #
Maximum characters per line
LCD_CHR = True
LCD_CMD = False
LCD_LINE_1 = 0x80 # LCD RAM address for the 1st line
LCD_LINE_2 = 0xC0 # LCD RAM address for the 2nd line
# Timing constants
E_PULSE = 0.00005
E_DELAY = 0.00005
def main():
# Main program block

GPIO.setmode(GPIO.BCM)       # Use
BCM GPIO numbers
  GPIO.setup(LCD_E,
GPIO.OUT)  # E
  GPIO.setup(LCD_RS,
GPIO.OUT) # RS
  GPIO.setup(LCD_D4,
GPIO.OUT) # DB4
  GPIO.setup(LCD_D5,
GPIO.OUT) # DB5
  GPIO.setup(LCD_D6,
GPIO.OUT) # DB6

 

  GPIO.setup(LCD_D7,
GPIO.OUT) # DB7
# Initialise display
  lcd_init()
# Send some text
  lcd_byte(LCD_LINE_1,
LCD_CMD)
  lcd_string(“Raspberrypi-spy”)
  lcd_byte(LCD_LINE_2,
LCD_CMD)

lcd_string(“.co.uk”)
  time.sleep(20)
#Function Definition
  # Initialize display
  def lcd_init():
  lcd_byte(0x33,LCD_CMD)
  lcd_byte(0x32,LCD_CMD)
  lcd_byte(0x28,LCD_CMD)
  lcd_byte(0x0C,LCD_CMD)
  lcd_byte(0x06,LCD_CMD)
 

lcd_byte(0x01,LCD_CMD)  
  # Send string to display
def lcd_string(message):
message =
message.ljust(LCD_WIDTH,” “) 
for i in
range(LCD_WIDTH):

lcd_byte(ord(message[i]),LCD_CHR)
 # Send byte to data pins
def lcd_byte(bits, mode):
  # bits = data
  # mode = True  for character
  # False for command
   GPIO.output(LCD_RS,
mode) # RS
   # High bits
  GPIO.output(LCD_D4,
False)
  GPIO.output(LCD_D5,
False)
  GPIO.output(LCD_D6,
False)
  GPIO.output(LCD_D7,
False)
  if bits&0x10==0x10:
    GPIO.output(LCD_D4,
True)
  if bits&0x20==0x20:
    GPIO.output(LCD_D5,
True)
  if bits&0x40==0x40:
    GPIO.output(LCD_D6,
True)
  if bits&0x80==0x80:
    GPIO.output(LCD_D7,
True)
 
  # Toggle ‘Enable’ pin
  time.sleep(E_DELAY)
  GPIO.output(LCD_E, True)
  time.sleep(E_PULSE)
  GPIO.output(LCD_E,
False)
  time.sleep(E_DELAY)     
 
  # Low bits
  GPIO.output(LCD_D4,
False)
  GPIO.output(LCD_D5,
False)
  GPIO.output(LCD_D6,
False)
  GPIO.output(LCD_D7,
False)
  if bits&0x01==0x01:
    GPIO.output(LCD_D4,
True)
  if bits&0x02==0x02:
    GPIO.output(LCD_D5,
True)
  if bits&0x04==0x04:
    GPIO.output(LCD_D6,
True)
  if bits&0x08==0x08:
    GPIO.output(LCD_D7,
True)
 
  # Toggle ‘Enable’ pin
  time.sleep(E_DELAY)
  GPIO.output(LCD_E, True)
  time.sleep(E_PULSE)
  GPIO.output(LCD_E,
False)
  time.sleep(E_DELAY)  
 
if __name__ == ‘__main__':
 
  main()# —————End————————
Hardware Troubleshooting:
  • Check Power Supply
  • Check all connecting wires
How to run: 
To run the script file, Open LX Terminal and type:
 sudo python lcd.py
When you press Enter then you can see on LCD display If you face any problem to drive motor check Hardware 
 
 

Video: