How to use i2c lcd with Arduino

In this tutorial, you’ll learn how to use a 16×2 LCD display with an I2C interface using the Adafruit_LiquidCrystal library. The code example shows how to display a counter that increments every second and blinks the backlight to create a visual effect. 

 📝 Required components

  • Arduino Uno
  • 16×2 LCD with I2C backpack
  • Jumper wires
  • Breadboard (optional)

Step 1: wiring the electronics

  1. Connect the GND of the I2C LCD to the GND pin on Arduino.
  2. Connect VCC of the LCD to 5V on Arduino.
  3. Connect SDA on the LCD to A4 on Arduino Uno.
  4. Connect SCL on the LCD to A5 on Arduino Uno.

🖥️Step 2: Writing the Code

Below you will find the complete code to use the humidity sensor:

				
					Upload this sketch to display a message: 

 
#include <Wire.h> 
#include <LiquidCrystal_I2C.h> 
 
LiquidCrystal_I2C lcd(0x27, 16, 2);  // Set I2C address 0x27, 16 columns and 2 rows 
 
void setup() { 
  lcd.init();                       // Initialize the LCD 
  lcd.backlight();                  // Turn on the backlight 
  lcd.setCursor(0, 0);              // First column, first row 
  lcd.print("Hello, World!");       // Print message on first row 
  lcd.setCursor(0, 1);              // First column, second row 
  lcd.print("I2C LCD Demo");        // Print message on second row 
} 
 
void loop() { 
  // Nothing needed here 
} 

				
			

🔎 Breaking down the Code

Let’s review the code section by section:

				
					#include <Adafruit_LiquidCrystal.h>

				
			
  • Includes the Adafruit_LiquidCrystal library that allows you to control the I2C LCD easily.

  • Make sure to install this library via the Arduino Library Manager.

				
					int seconds = 0;

				
			
  • Declares a global variable seconds to store the number of seconds that have passed.
				
					Adafruit_LiquidCrystal lcd_1(0);

				
			
  • Creates an LCD object named lcd_1.

  • The argument 0 is a reference ID (not the I2C address). In some versions, you might use lcd_1(0x27) or lcd_1(0x3F) depending on your I2C backpack.

				
					void setup() {
  lcd_1.begin(16, 2);           // Initialize 16x2 LCD
  lcd_1.print("hello world");   // Print on first row
}

				
			
    • lcd_1.begin(16, 2); initializes the LCD with 16 columns and 2 rows.

    • lcd_1.print("hello world"); prints the message starting from the top-left corner (default cursor position).

				
					lcd_1.setCursor(0, 1);
lcd_1.print(seconds);

				
			
    • These lines toggle the backlight on and off every second to create a blinking effect.

				
					seconds += 1;
				
			
  • Increments the counter by 1 each time the loop runs (once per second).

🛠️Step 3: Troubleshooting

Nothing appears on the LCD:

  • Verify the I2C address with an I2C scanner sketch. 
  • Check wiring (SDA to A4, SCL to A5). 

 

Text is garbled: 

  • Adjust potentiometer on I2C backpack to set contrast. 
en_US