Skip to content
DisplayLGPL-3.0v1.1.2

LiquidCrystal I2C

by Frank de Brabander

This library lets you drive the classic HD44780 character LCDs (the 16x2 LCD1602 and the 20x4 LCD2004) through the small 4-pin I2C backpack soldered to the back of the display. Instead of the 6 or more wires a bare LCD needs, you connect only VCC, GND, SDA and SCL. After lcd.init() and lcd.backlight(), writing text is just setCursor(col, row) followed by print(). The library handles the I2C traffic to the PCF8574 chip on the backpack for you. The same API works for every size, and only the column and row numbers in the constructor change, for example LiquidCrystal_I2C lcd(0x27, 16, 2) or lcd(0x27, 20, 4).

A warning before you install: several different libraries are all called "LiquidCrystal I2C", written by different people and forked many times. They mostly share the same functions, but small differences cause errors such as init() or begin() not being found. The one in the Arduino Library Manager is by Frank de Brabander, and the PlatformIO entry listed on this page installs a fork with the same API. If an example fails to compile, check that only one copy is installed and that it is the one you expect. The hd44780 library by Bill Perry is a well-regarded alternative that can even find the I2C address on its own.

The number one cause of a blank or dead-looking display is the I2C address. Backpacks use a PCF8574 chip, normally at 0x27, or a PCF8574A, normally at 0x3F, and the two are not interchangeable. Run the I2C scanner example below to find the right one. The second cause is contrast: if the backlight is on but you see nothing, or a row of solid blocks, turn the small blue potentiometer on the backpack with a screwdriver until the text appears.

Power matters too. A 16x2 or 20x4 LCD needs 5 V to display properly, so power it from the 5 V pin even on an ESP32. Be careful with that setup: many backpacks pull SDA and SCL up to the 5 V supply, which can exceed what an ESP32's 3.3 V pins tolerate. Many people run it this way without trouble, but the safest options are a bidirectional level shifter or removing the pull-up resistors on the backpack. On an Uno, SDA is A4 and SCL is A5. On most ESP32 boards the defaults are GPIO21 and GPIO22, and you can use other pins by calling Wire.begin(sda, scl) before lcd.init().

Finally, remember that print() writes over existing characters but never clears them. If a value gets shorter, for example 100 changing to 99, the old last digit stays on screen. The padded-line example below solves this without the flicker of calling clear() every loop.

Setup

Installation

Arduino IDE — Library Manager

Sketch → Include Library → Manage Libraries → search LiquidCrystal I2C

PlatformIO

lib_deps = marcoschwartz/LiquidCrystal_I2C

Compatibility

Supported boards

Arduino UnoESP32STM32 Blue Pill

Code

Examples

Print two lines of text (16x2)

The baseline pattern behind every LCD1602 example on this site. Change 0x27 to your address if nothing appears.

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

LiquidCrystal_I2C lcd(0x27, 16, 2);   // address, columns, rows

void setup() {
  lcd.init();
  lcd.backlight();

  lcd.setCursor(0, 0);                // column 0, row 0
  lcd.print("Hello, SolderHub");
  lcd.setCursor(0, 1);                // column 0, row 1
  lcd.print("LCD ready");
}

void loop() {}

Using a 20x4 display

Only the constructor changes. Rows are numbered 0 to 3 and columns 0 to 19.

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

LiquidCrystal_I2C lcd(0x27, 20, 4);   // 20 columns, 4 rows

void setup() {
  lcd.init();
  lcd.backlight();

  lcd.setCursor(0, 0); lcd.print("Row 0");
  lcd.setCursor(0, 1); lcd.print("Row 1");
  lcd.setCursor(0, 2); lcd.print("Row 2");
  lcd.setCursor(0, 3); lcd.print("Row 3");
}

void loop() {}

Find your LCD's I2C address

Run this if the display stays blank. It prints the address of every I2C device it finds, usually 0x27 or 0x3F for an LCD backpack.

#include <Wire.h>

void setup() {
  Serial.begin(115200);
  Wire.begin();                 // on ESP32 with custom pins: Wire.begin(SDA_PIN, SCL_PIN);
  Serial.println(F("Scanning..."));

  for (uint8_t addr = 1; addr < 127; addr++) {
    Wire.beginTransmission(addr);
    if (Wire.endTransmission() == 0) {
      Serial.print(F("Found device at 0x"));
      Serial.println(addr, HEX);
    }
  }
  Serial.println(F("Done"));
}

void loop() {}

Update a line without leftover characters or flicker

print() does not clear old characters. This helper pads each line with spaces to the full width, so shorter values fully replace longer ones and the screen never flashes.

void printLine(uint8_t row, const String &text, uint8_t cols = 16) {
  String padded = text;
  while (padded.length() < cols) padded += ' ';   // pad to full width
  lcd.setCursor(0, row);
  lcd.print(padded.substring(0, cols));            // never exceed the row
}

// usage inside loop():
// printLine(0, "Temp: " + String(tempC, 1) + " C");
// printLine(1, "Hum:  " + String(humidity, 0) + " %");

Custom character: degree symbol

The LCD can store up to 8 custom 5x8 characters. Here one is used for a degree sign. Create it after lcd.init().

byte degree[8] = {
  0b00110,
  0b01001,
  0b01001,
  0b00110,
  0b00000,
  0b00000,
  0b00000,
  0b00000
};

void setup() {
  lcd.init();
  lcd.backlight();
  lcd.createChar(0, degree);          // store in slot 0

  lcd.setCursor(0, 0);
  lcd.print("Temp: 27.4");
  lcd.write(byte(0));                 // draw custom character 0
  lcd.print("C");
}