How to Use I2C LCD with Arduino: Simple Guide and Example
To use an
I2C LCD with Arduino, connect the LCD's SDA and SCL pins to Arduino's SDA (A4) and SCL (A5) pins, then use the LiquidCrystal_I2C library to control the display. Initialize the LCD with its I2C address and call functions like lcd.print() to show text.Syntax
The basic syntax to use an I2C LCD with Arduino involves including the LiquidCrystal_I2C library, creating an LCD object with the I2C address and size, initializing it, and then using methods to control the display.
#include <LiquidCrystal_I2C.h>: Includes the library for I2C LCD control.LiquidCrystal_I2C lcd(address, cols, rows);: Creates an LCD object with the I2C address and display size.lcd.init();: Initializes the LCD.lcd.backlight();: Turns on the LCD backlight.lcd.print("text");: Prints text on the LCD.
arduino
#include <LiquidCrystal_I2C.h> // Set the LCD address to 0x27 for a 16 chars and 2 line display LiquidCrystal_I2C lcd(0x27, 16, 2); void setup() { lcd.init(); // Initialize the LCD lcd.backlight(); // Turn on the backlight lcd.print("Hello"); // Print text } void loop() { // Nothing here }
Example
This example shows how to display "Hello, World!" on the first line and "I2C LCD Demo" on the second line of a 16x2 I2C LCD connected to an Arduino.
arduino
#include <Wire.h> #include <LiquidCrystal_I2C.h> LiquidCrystal_I2C lcd(0x27, 16, 2); // Address 0x27, 16 columns, 2 rows void setup() { lcd.init(); lcd.backlight(); lcd.setCursor(0, 0); // Set cursor to column 0, row 0 lcd.print("Hello, World!"); // Print first line lcd.setCursor(0, 1); // Set cursor to column 0, row 1 lcd.print("I2C LCD Demo"); // Print second line } void loop() { // No repeated actions needed }
Output
LCD displays:
Hello, World!
I2C LCD Demo
Common Pitfalls
Common mistakes when using I2C LCD with Arduino include:
- Using the wrong I2C address. Use an I2C scanner sketch to find the correct address.
- Not connecting SDA and SCL pins correctly (Arduino Uno uses A4 for SDA and A5 for SCL).
- Forgetting to call
lcd.init()andlcd.backlight(). - Not installing the
LiquidCrystal_I2Clibrary or using an incompatible version.
arduino
// Wrong I2C address example // LiquidCrystal_I2C lcd(0x3F, 16, 2); // May not work if address is 0x27 // Correct way after scanning LiquidCrystal_I2C lcd(0x27, 16, 2);
Quick Reference
| Function | Description |
|---|---|
| lcd.init() | Initializes the LCD display |
| lcd.backlight() | Turns on the LCD backlight |
| lcd.noBacklight() | Turns off the LCD backlight |
| lcd.clear() | Clears the display |
| lcd.setCursor(col, row) | Sets the cursor position |
| lcd.print(text) | Prints text at the current cursor position |
Key Takeaways
Connect SDA and SCL pins of the I2C LCD to Arduino's A4 and A5 pins respectively.
Use the LiquidCrystal_I2C library and initialize the LCD with the correct I2C address.
Always call lcd.init() and lcd.backlight() before printing text.
Use an I2C scanner sketch to find your LCD's correct I2C address if unsure.
Check wiring and library installation to avoid common connection issues.