Jump to content

Liquid Crystal Display (LCD)

From cognifront

Interfacing 16x2 LCD Display with Arduino UNO and ESP32 using I2C

Interfacing 16x2 LCD Display with Arduino UNO and ESP32 using I2C demonstrates how a 16x2 Liquid Crystal Display (LCD) can be interfaced with microcontroller development boards to display text and messages.

An LCD is an electronic display module commonly used to display alphanumeric characters, numbers and symbols.

In this experiment, a 16x2 LCD with an I2C interface is connected to Arduino UNO and ESP32 separately. The I2C communication protocol is used to transfer data between the microcontroller and the LCD module.

The experiment demonstrates displaying messages on the LCD using Arduino IDE programming.

Microcontroller → I2C Communication → 16x2 LCD → Text Display

Theory

A 16x2 LCD (Liquid Crystal Display) is an electronic display module capable of displaying 16 characters per line and 2 lines of text.

The LCD consists of liquid crystal cells arranged in a matrix. These cells control the passage of light to display alphanumeric characters.

The LCD module used in this experiment is equipped with an I2C interface module, which simplifies the connection between the LCD and the microcontroller.

I2C stands for Inter-Integrated Circuit. It is a serial communication protocol that uses two communication lines:

  • SDA (Serial Data): Used for transferring data between devices.
  • SCL (Serial Clock): Used for synchronizing data communication.

The I2C interface reduces the number of connections required between the microcontroller and the LCD.

The LCD module generally uses the I2C address 0x27. The address may vary depending on the I2C module.

The LiquidCrystal_I2C library is used in Arduino IDE to control the LCD display.

The library provides functions such as:

Function Description
lcd.init() Initializes the LCD display
lcd.begin() Initializes the LCD using the supported library interface
lcd.backlight() Turns ON the LCD backlight
lcd.print() Displays text or data on the LCD
lcd.setCursor() Sets the cursor position on the LCD
lcd.clear() Clears the LCD display

I2C SDA + SCL → LCD Communication → Character Display

Applications

16x2 LCD displays are commonly used in:

  • Embedded system projects
  • Industrial monitoring systems
  • Home automation systems
  • Temperature monitoring systems
  • Digital measurement instruments
  • Electronic control panels
  • IoT-based monitoring systems

Interfacing 16x2 LCD with Arduino UNO

Connections

The 16x2 LCD with I2C interface is connected to the Arduino UNO using the I2C communication pins.

The following connections are used:

LCD I2C Module Arduino UNO Function
VCC 5V Power supply
GND GND Ground
SDA A4 Serial Data
SCL A5 Serial Clock

Arduino UNO I2C Pin Details:

Parameter Arduino UNO
SDA A4
SCL A5
Operating Logic Voltage 5V
LCD I2C Address 0x27 (as specified in the program)

Algorithm

The following algorithm is used to interface the 16x2 LCD with Arduino UNO:

  1. Start the Arduino IDE.
  2. Connect the Arduino UNO board to the computer using a USB cable.
  3. Connect the 16x2 LCD I2C module to the Arduino UNO.
  4. Include the Wire.h and LiquidCrystal_I2C.h libraries.
  5. Create an LCD object with I2C address 0x27 and display dimensions of 16 columns and 2 rows.
  6. In the void setup() function, initialize the LCD.
  7. Turn ON the LCD backlight.
  8. Display the message "Hello, World!" on the LCD.
  9. Set the cursor position for displaying the next message.
  10. Display the message "Uno Here :)" on the second row.
  11. Keep the LCD display active.

Working

The Arduino UNO communicates with the 16x2 LCD through the I2C interface.

The working sequence is:

  1. The Arduino UNO initializes the I2C communication interface.
  2. The LCD module is initialized using the LiquidCrystal_I2C library.
  3. The LCD backlight is turned ON.
  4. The message "Hello, World!" is displayed on the first row.
  5. The cursor is moved to the second row.
  6. The message "Uno Here :)" is displayed on the second row.
  7. The displayed messages remain visible on the LCD.

Arduino UNO → I2C Communication → 16x2 LCD → Message Display

Arduino UNO Program Code

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

// Change the address based on the scanner output
LiquidCrystal_I2C lcd(0x27, 16, 2);  
//5V -> 5V
//GND -> GND
//SDA -> A4
//SCL -> A5

void setup() {
  lcd.init();
  lcd.backlight();
  lcd.setCursor(0, 0);
  lcd.print("Hello, World!");
  lcd.setCursor(0, 1);
  lcd.print("Uno Here :)");
}

void loop() {}

Program Explanation

  • The Wire.h library is included for I2C communication.
  • The LiquidCrystal_I2C.h library is included to control the LCD display.
  • The LCD object is created using the I2C address 0x27 with 16 columns and 2 rows.
  • The lcd.init() function initializes the LCD display.
  • The lcd.backlight() function turns ON the LCD backlight.
  • The lcd.setCursor(0, 0) function positions the cursor at the first column of the first row.
  • The lcd.print("Hello, World!") function displays the message "Hello, World!" on the first row.
  • The lcd.setCursor(0, 1) function positions the cursor at the first column of the second row.
  • The lcd.print("Uno Here :)") function displays the message "Uno Here :)" on the second row.
  • The loop() function is empty because the messages are displayed only once during initialization and remain visible.

Observation

When the Arduino UNO is connected to the 16x2 LCD using the I2C interface, the LCD initializes successfully and displays the programmed messages.

The following output is displayed:

Hello, World!

Uno Here :)

Conclusion

The Arduino UNO successfully interfaces with the 16x2 LCD using the I2C communication protocol.

The LCD displays "Hello, World!" on the first row and "Uno Here :)" on the second row.

Thus, text display using a 16x2 I2C LCD and Arduino UNO is successfully implemented.

Interfacing 16x2 LCD with ESP32

Connections

The 16x2 LCD with I2C interface is connected to the ESP32 using its I2C communication pins.

The following connections are used:

LCD I2C Module ESP32 Function
VCC 3.3V (subject to module compatibility) Power supply
GND GND Ground
SDA GPIO 21 Serial Data
SCL GPIO 22 Serial Clock

ESP32 I2C Pin Details:

Parameter ESP32
SDA GPIO 21
SCL GPIO 22
Logic Voltage 3.3V
LCD I2C Address 0x27 (as specified in the program)

Note: Check the LCD I2C module's supply voltage requirements and I2C pull-up voltage before connecting it to the ESP32. Many LCD backpacks operate at 5V and have pull-up resistors connected to their supply voltage. Ensure that the ESP32 GPIO pins are never exposed to 5V.

Algorithm

The following algorithm is used to interface the 16x2 LCD with ESP32:

  1. Start the Arduino IDE.
  2. Connect the ESP32 development board to the computer using a USB cable.
  3. Connect the 16x2 LCD I2C module to the ESP32.
  4. Include the Wire.h and LiquidCrystal_I2C.h libraries.
  5. Create an LCD object with I2C address 0x27 and display dimensions of 16 columns and 2 rows.
  6. In the void setup() function, initialize the LCD.
  7. Turn ON the LCD backlight.
  8. Display the message "Hello world!" on the LCD.
  9. Provide a delay of 500 milliseconds.
  10. In the void loop() function, clear the LCD display.
  11. Set the cursor position to column 2 and row 0.
  12. Display the message "Good".
  13. Set the cursor position to column 6 and row 0.
  14. Display the message "Morning".
  15. Provide a delay of 500 milliseconds.
  16. Repeat the process continuously.

Working

The ESP32 communicates with the 16x2 LCD through the I2C communication interface.

The working sequence is:

  1. The ESP32 initializes the LCD using the LiquidCrystal_I2C library.
  2. The LCD backlight is turned ON.
  3. The message "Hello world!" is displayed.
  4. A delay of 500 milliseconds is provided.
  5. The LCD display is cleared.
  6. The cursor is positioned at column 2 of the first row.
  7. The message "Good" is displayed.
  8. The cursor is moved to column 6 of the first row.
  9. The message "Morning" is displayed.
  10. A delay of 500 milliseconds is provided.
  11. The process repeats continuously.

ESP32 → I2C Communication → 16x2 LCD → Text Display

ESP32 Program Code

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
//scl 22
//sda 21
// Set the LCD address to 0x27 for a 16 columns and 2 rows LCD
LiquidCrystal_I2C lcd(0x27, 16, 2);

void setup()
{
  lcd.begin();//Use this method for Arduino UNO
  lcd.backlight();// Turn on the blacklight and print a message.
  lcd.print("Hello world!");
  delay(500);
}

void loop()
{
  lcd.clear();
  lcd.setCursor(2, 0);
  lcd.print("Good");
  lcd.setCursor(6, 0);
  lcd.print("Morning");
  delay(500);
}

Program Explanation

  • The Wire.h library is included for I2C communication.
  • The LiquidCrystal_I2C.h library is included to control the LCD display.
  • The LCD object is created with I2C address 0x27 and dimensions of 16 columns and 2 rows.
  • The comments specify GPIO 22 as SCL and GPIO 21 as SDA.
  • The lcd.begin() function is used to initialize the LCD display.
  • The lcd.backlight() function turns ON the LCD backlight.
  • The lcd.print("Hello world!") function displays the message "Hello world!" on the LCD.
  • The delay(500) function provides a delay of 500 milliseconds.
  • The lcd.clear() function clears the LCD display.
  • The lcd.setCursor(2, 0) function positions the cursor at column 2 of the first row.
  • The lcd.print("Good") function displays the text "Good".
  • The lcd.setCursor(6, 0) function positions the cursor at column 6 of the first row.
  • The lcd.print("Morning") function displays the text "Morning".
  • The delay(500) function provides a delay of 500 milliseconds before the next iteration.
  • The loop() function continuously repeats the display operation.

Observation

When the ESP32 is connected to the 16x2 LCD using the I2C interface, the LCD initializes and displays the programmed messages.

Initially, the message "Hello world!" is displayed for 500 milliseconds.

After that, the LCD displays "Good Morning" on the first row. The display is cleared and updated repeatedly.

The following output is displayed during the loop operation:

Good Morning

Conclusion

The ESP32 successfully interfaces with the 16x2 LCD using the I2C communication protocol.

The LCD displays the initial message "Hello world!" and subsequently displays "Good Morning" through continuous execution of the loop() function.

Thus, text display using a 16x2 I2C LCD and ESP32 is successfully implemented.

Result

The 16x2 I2C LCD is successfully interfaced with both Arduino UNO and ESP32 development boards.

The Arduino UNO displays static messages on the LCD, while the ESP32 continuously updates the displayed text using the lcd.clear(), lcd.setCursor() and lcd.print() functions.

Thus, LCD interfacing using the I2C communication protocol is successfully demonstrated with both microcontroller platforms.


Interfacing 16x2 LCD with Raspberry Pi using I2C

Interfacing 16x2 LCD with Raspberry Pi using I2C demonstrates how a 16x2 Liquid Crystal Display (LCD) can be connected to a Raspberry Pi board and controlled using Python programming.

The LCD is interfaced with the Raspberry Pi using the I2C communication protocol. The I2C interface requires only two communication lines, SDA and SCL, along with power supply and ground connections.

In this experiment, the Raspberry Pi displays messages on both rows of the 16x2 LCD.

Raspberry Pi → I2C Communication → 16x2 LCD → Text Display

Theory

A 16x2 LCD (Liquid Crystal Display) is an electronic display module capable of displaying 16 characters per line and 2 lines of text. The LCD consists of liquid crystal cells arranged in a matrix. These cells control the passage of light to display alphanumeric characters. The LCD module used in this experiment is equipped with an I2C interface module, which simplifies the connection between the Raspberry Pi and the LCD. I2C stands for Inter-Integrated Circuit. It is a serial communication protocol that uses two communication lines:

  • SDA (Serial Data): Used for transferring data between devices.
  • SCL (Serial Clock): Used for synchronizing data communication.

The Raspberry Pi communicates with the LCD through its I2C interface using Python programming. The RPLCD library is used to control the LCD display. The library provides functions such as:

Function Description
CharLCD() Creates and initializes the LCD object
lcd.clear() Clears the LCD display
lcd.write_string() Displays text on the LCD
lcd.cursor_pos Sets the cursor position on the LCD
lcd.close() Closes the LCD interface

The LCD module generally uses the I2C address 0x27. The address may vary depending on the I2C module.

Connections

The 16x2 LCD with I2C interface is connected to the Raspberry Pi using the I2C communication pins.

The following connections are used:

LCD I2C Module Raspberry Pi Function
VCC 5V (Physical Pin 2) Power supply
GND GND (Physical Pin 6) Ground
SDA GPIO 2 (Physical Pin 3) Serial Data
SCL GPIO 3 (Physical Pin 5) Serial Clock

Raspberry Pi I2C Pin Details:

Parameter Raspberry Pi
GPIO Numbering BCM
SDA GPIO 2
SCL GPIO 3
SDA Physical Pin Pin 3
SCL Physical Pin Pin 5
I2C Address 0x27 (example)
I2C Bus Bus 1

Note: Raspberry Pi GPIO pins operate at 3.3V logic. Check the LCD I2C module's voltage requirements and pull-up resistor connections before interfacing. If the LCD backpack pulls SDA and SCL up to 5V, use a suitable bidirectional I2C level shifter to protect the Raspberry Pi.

Required Library Installation

The RPLCD library is used to interface the 16x2 LCD with Raspberry Pi using Python.

Open the Raspberry Pi terminal and execute the following commands:

sudo apt update

sudo apt install -y i2c-tools python3-smbus

pip install RPLCD smbus2

Note: If Raspberry Pi OS blocks system-wide pip installation, install RPLCD and smbus2 inside a Python virtual environment instead.

Enable I2C communication using:

sudo raspi-config

Navigate to:

Interface Options → I2C → Enable

After enabling I2C, reboot the Raspberry Pi.

To check the I2C address of the LCD module, execute:

sudo i2cdetect -y 1

The detected address is generally 0x27 or 0x3F, depending on the LCD I2C module.

Algorithm

The following algorithm is used to interface the 16x2 LCD with Raspberry Pi:

  1. Start the Raspberry Pi.
  2. Connect the 16x2 LCD I2C module to the Raspberry Pi.
  3. Enable I2C communication on the Raspberry Pi.
  4. Install the required RPLCD and smbus2 Python libraries.
  5. Import the required libraries in the Python program.
  6. Create an LCD object using the I2C address 0x27.
  7. Configure the LCD dimensions as 16 columns and 2 rows.
  8. Clear the LCD display.
  9. Set the cursor position to the first row.
  10. Display the message "Hello, World!".
  11. Set the cursor position to the second row.
  12. Display the message "Raspberry Pi".
  13. Keep the LCD display active until the program is interrupted.

Working

The Raspberry Pi communicates with the 16x2 LCD through the I2C communication interface.

The working sequence is:

  1. The RPLCD library is imported to control the LCD.
  2. The LCD object is created using the I2C address.
  3. The LCD is configured for 16 columns and 2 rows.
  4. The LCD display is cleared.
  5. The cursor is positioned at the first row.
  6. The message "Hello, World!" is displayed on the first row.
  7. The cursor is moved to the second row.
  8. The message "Raspberry Pi" is displayed on the second row.
  9. The messages remain visible until the program is interrupted.

Raspberry Pi → I2C Communication → LCD Initialization → Text Display

Raspberry Pi Python Program

The following Python program is used to interface the 16x2 LCD with Raspberry Pi using the I2C communication protocol.

from RPLCD.i2c import CharLCD
import time

lcd = CharLCD(
    'PCF8574',
    address=0x27,
    port=1,
    cols=16,
    rows=2,
    charmap='A00'
)

try:
    lcd.clear()

    lcd.cursor_pos = (0, 0)
    lcd.write_string("Hello, World!")

    lcd.cursor_pos = (1, 0)
    lcd.write_string("Raspberry Pi")

    while True:
        time.sleep(1)

except KeyboardInterrupt:
    print("LCD Program Stopped")

finally:
    lcd.clear()
    lcd.close()

Program Explanation

  • The RPLCD.i2c library is imported to control the LCD using I2C communication.
  • The time library is imported to provide time delays.
  • The CharLCD() function creates an LCD object using the PCF8574 I2C interface.
  • The address parameter specifies the LCD I2C address as 0x27.
  • The port parameter specifies I2C bus 1.
  • The cols parameter specifies 16 columns.
  • The rows parameter specifies 2 rows.
  • The charmap parameter specifies the character mapping used by the LCD.
  • The lcd.clear() function clears the LCD display.
  • The lcd.cursor_pos = (0, 0) statement positions the cursor at the first column of the first row.
  • The lcd.write_string("Hello, World!") function displays the message "Hello, World!" on the first row.
  • The lcd.cursor_pos = (1, 0) statement positions the cursor at the first column of the second row.
  • The lcd.write_string("Raspberry Pi") function displays the message "Raspberry Pi" on the second row.
  • The while True loop keeps the program running.
  • The time.sleep(1) function provides a delay of 1 second.
  • The except KeyboardInterrupt block handles program interruption using Ctrl+C.
  • The lcd.clear() function clears the display when the program exits.
  • The lcd.close() function closes the LCD interface and releases its resources.

Observation

When the Raspberry Pi is connected to the 16x2 LCD using the I2C interface, the LCD initializes successfully and displays the programmed messages.

The following output is displayed:

Hello, World!

Raspberry Pi

The LCD displays the first message on the first row and the second message on the second row.

Conclusion

The Raspberry Pi successfully interfaces with the 16x2 LCD using the I2C communication protocol. The LCD displays "Hello, World!" on the first row and "Raspberry Pi" on the second row. Thus, text display using a 16x2 I2C LCD and Raspberry Pi is successfully implemented using Python programming.