How to Use an I2C LCD Display with Arduino
Adding a screen to your Arduino projects makes them instantly more interactive. Instead of relying on the Serial Monitor, you can display live sensor data, status messages, or menus directly on the device. In this tutorial, we will learn how to connect and use a 16x2 I2C LCD display.
1. Why Use an I2C LCD?
A standard 16x2 LCD requires at least 6 digital pins to operate, which can quickly consume all the available pins on your Arduino Uno. An I2C LCD has an adapter soldered onto the back that converts it to use the I2C protocol. This means it only requires two data pins (SDA and SCL) along with power and ground.
What You Need:
2. Wiring the LCD to Arduino
Connecting the I2C LCD is incredibly simple. Just connect four wires from the LCD to your Arduino:
- GND to Arduino GND
- VCC to Arduino 5V
- SDA to Arduino A4 (or the dedicated SDA pin)
- SCL to Arduino A5 (or the dedicated SCL pin)
Note: Some Arduino compatible boards use different pins for I2C. On the Arduino Mega, SDA is 20 and SCL is 21. On ESP8266 (NodeMCU), it's typically D2 (SDA) and D1 (SCL).
3. The Code
Before writing the code, you need to install the LiquidCrystal I2C library. In the Arduino IDE, go to Sketch > Include Library > Manage Libraries, search for "LiquidCrystal I2C" (by Frank de Brabander), and install it.
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Set the LCD address to 0x27 for a 16 chars and 2 line display
// If 0x27 doesn't work, try 0x3F.
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
// Initialize the LCD
lcd.init();
// Turn on the backlight
lcd.backlight();
// Print a message to the LCD
lcd.setCursor(0, 0); // Column 0, Row 0
lcd.print("Hello, BD!");
lcd.setCursor(0, 1); // Column 0, Row 1
lcd.print("ElectroMart BD");
}
void loop() {
// Nothing to do here
}
4. Troubleshooting
If you see a blank screen or solid black blocks:
- Check the contrast: There is a small blue potentiometer on the back of the I2C module. Use a screwdriver to turn it until the text becomes clear.
- Check the address: The most common I2C addresses are
0x27and0x3F. If one doesn't work, try the other in the code. - Check your wiring: Ensure SDA and SCL aren't swapped.
