0
0
Iot-protocolsHow-ToBeginner · 4 min read

How to Use 16x2 LCD with Raspberry Pi: Simple Guide

To use a 16x2 LCD with a Raspberry Pi, connect it via an I2C interface or directly with GPIO pins, then control it using Python libraries like lcd or RPi.GPIO. The I2C method simplifies wiring and coding by using fewer pins and the python-smbus library for communication.
📐

Syntax

Using a 16x2 LCD with Raspberry Pi typically involves importing libraries, initializing the LCD, and sending text to display.

Key parts:

  • Import libraries: For I2C, use import smbus and import time.
  • Initialize LCD: Set up the LCD address and mode.
  • Write text: Use functions to send characters to the LCD.
python
import smbus
import time

# Define some device parameters
I2C_ADDR  = 0x27 # I2C device address
LCD_WIDTH = 16   # Maximum characters per line

# Define some device constants
LCD_CHR = 1 # Mode - Sending data
LCD_CMD = 0 # Mode - Sending command
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.0005
E_DELAY = 0.0005

# Initialize I2C (SMBus)
bus = smbus.SMBus(1)  # Rev 2 Pi uses 1

# Functions to send commands and data to LCD would follow here
💻

Example

This example shows how to display "Hello, World!" on the first line of a 16x2 LCD connected via I2C to a Raspberry Pi.

python
import smbus
import time

I2C_ADDR  = 0x27
LCD_WIDTH = 16
LCD_CHR = 1
LCD_CMD = 0
LCD_LINE_1 = 0x80
LCD_LINE_2 = 0xC0
E_PULSE = 0.0005
E_DELAY = 0.0005

bus = smbus.SMBus(1)

def lcd_toggle_enable(bits):
    bus.write_byte(I2C_ADDR, (bits | 0b00000100))
    time.sleep(E_PULSE)
    bus.write_byte(I2C_ADDR,(bits & ~0b00000100))
    time.sleep(E_DELAY)

def lcd_send_byte(bits, mode):
    bits_high = mode | (bits & 0xF0) | 0b00001000
    bits_low = mode | ((bits << 4) & 0xF0) | 0b00001000
    bus.write_byte(I2C_ADDR, bits_high)
    lcd_toggle_enable(bits_high)
    bus.write_byte(I2C_ADDR, bits_low)
    lcd_toggle_enable(bits_low)

def lcd_init():
    lcd_send_byte(0x33, LCD_CMD)
    lcd_send_byte(0x32, LCD_CMD)
    lcd_send_byte(0x06, LCD_CMD)
    lcd_send_byte(0x0C, LCD_CMD)
    lcd_send_byte(0x28, LCD_CMD)
    lcd_send_byte(0x01, LCD_CMD)
    time.sleep(E_DELAY)

def lcd_string(message, line):
    message = message.ljust(LCD_WIDTH, " ")
    lcd_send_byte(line, LCD_CMD)
    for i in range(LCD_WIDTH):
        lcd_send_byte(ord(message[i]), LCD_CHR)

lcd_init()
lcd_string("Hello, World!", LCD_LINE_1)
Output
The LCD screen shows: Hello, World! on the first line
⚠️

Common Pitfalls

Common mistakes when using a 16x2 LCD with Raspberry Pi include:

  • Incorrect I2C address: Use sudo i2cdetect -y 1 to find the correct address.
  • Wiring errors: Ensure SDA and SCL pins are connected properly (GPIO 2 and 3 on Pi).
  • Not enabling I2C interface: Enable it via raspi-config.
  • Forgetting to install required libraries like python3-smbus.
  • Sending commands without proper delays, causing display glitches.
python
## Wrong: Using wrong I2C address
I2C_ADDR = 0x3F  # Might be incorrect

## Right: Detect and use correct address
# Run in terminal: sudo i2cdetect -y 1
# Then set I2C_ADDR accordingly, e.g.,
I2C_ADDR = 0x27
📊

Quick Reference

Tips for using 16x2 LCD with Raspberry Pi:

  • Use I2C interface to reduce wiring complexity.
  • Enable I2C in Raspberry Pi settings before running code.
  • Use sudo apt install python3-smbus i2c-tools to install needed packages.
  • Check LCD address with i2cdetect.
  • Use delays between commands to ensure stable display.

Key Takeaways

Connect the 16x2 LCD via I2C for simpler wiring and control.
Enable I2C on Raspberry Pi and install necessary Python libraries before coding.
Use correct I2C address found with i2cdetect to avoid communication errors.
Include small delays between commands to ensure the LCD updates properly.
Test with simple text output first to confirm wiring and code correctness.