Interfacing 16x2 LCD to Raspberry Pi board

Interfacing 16x2 LCD to Raspberry Pi board


16x2 Segment LCD is the commonly used LCD for simple Embedded applications. In this post, we will see 16x2 Segment LCD interfacing with the Raspberry Pi board. Below is the pin mapping of the 16x2 LCD.


In the 16x2 LCD there are 16 columns and 2 rows which is defined in the code:

#!/usr/bin/python
# Example using a character LCD connected to a Raspberry Pi
import time
import Adafruit_CharLCD as LCD

# Raspberry Pi pin configuration:
lcd_rs        = 26  # Note this might need to be changed to 21 for older revision Pi's.
lcd_en        = 19
lcd_d4        = 13
lcd_d5        = 6
lcd_d6        = 5
lcd_d7        = 21
lcd_backlight = 4

# Define LCD column and row size for 16x2 LCD.
lcd_columns = 16
lcd_rows    = 2

# Alternatively specify a 20x4 LCD.
# lcd_columns = 20
# lcd_rows    = 4

# Initialize the LCD using the pins above.
lcd = LCD.Adafruit_CharLCD(lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6, lcd_d7,
                           lcd_columns, lcd_rows, lcd_backlight)

# Print a two line message
lcd.message('Hello viewers')

# Wait 5 seconds
time.sleep(3.0)

# Demo showing the cursor.
lcd.clear()
lcd.show_cursor(True)
lcd.message('Loading...')

time.sleep(3.0)

# Demo showing the blinking cursor.
lcd.clear()
lcd.blink(True)
lcd.message('Please, wait..')

time.sleep(3.0)

# Stop blinking and showing cursor.
lcd.show_cursor(False)
lcd.blink(False)

# Demo turning backlight off and on.
lcd.clear()
lcd.set_cursor(0,0)
lcd.message('Welcome to')
lcd.set_cursor(0,1)
lcd.message('Way2Know')
time.sleep(3.0)
# Turn backlight off.
lcd.set_backlight(0)
#time.sleep(2.0)
lcd.set_cursor(0,0)
# Change message.
lcd.clear()
lcd.message('Goodbye!')
# Turn backlight on.
lcd.set_backlight(1)


In the above backlight is controlled but in the hardware connection LEDA is hardwired to 5V and is not controlled, this meant backlight is always ON. If someone wants to demonstrate the power savings, then backlight control must be done.

The pins of the LCD are explained in the below table:


4-bit LCD interface or 8-bit LCD interface can be used. D4-D7 pins are used fro interfacing to Raspberry Pi. D0-D3 pins are left unused.

Below is the pin mapping used for the project:


The 5V and GND can be connected to any of the pins on GPIO header as per your convenience.

Potentiometer connection for contrast pin is as below:

For the potentiometer, VCC is connected to 5V and GND is connected to Ground. Output is connected to Contrast pin of LCD.

Explanation of the code and connectivity is done in the below video:

Post a Comment

0 Comments