Arduino library · Displays

LiquidCrystal_PCF8574: an I2C LCD with the size in begin()

LiquidCrystal_PCF8574 by Matthias Hertel drives the common 16x2 and 20x4 character LCDs that have a PCF8574 I2C backpack soldered on the back. It is one of several libraries for the same module, and the ESP8266 and ESP32 tutorials often pick it because it builds cleanly on every core.

Built in on Mokxi: #include <LiquidCrystal_PCF8574.h> and it builds, with nothing to install. Free, and no account needed.

What LiquidCrystal_PCF8574 does

If you know LiquidCrystal_I2C, three things change. The constructor takes only the address, LiquidCrystal_PCF8574 lcd(0x27). The size is given to begin(16, 2) instead. And the backlight is set with setBacklight(255) rather than backlight(). The rest (setCursor, print, clear, createChar, scrolling) is the standard LiquidCrystal set, so moving a sketch between the two takes a minute.

The runnable example first does what the library’s own example does: it calls Wire.beginTransmission(0x27) and prints the result, so you can see on the Serial Monitor whether the backpack answered before anything is written to the screen. An error of 0 means the address is right. In Mokxi the backlight is on or off; values between 1 and 255 all mean on.

Library Manager nameLiquidCrystal_PCF8574
AuthorMatthias Hertel
Include#include <LiquidCrystal_PCF8574.h>
Boardsevery board Mokxi compiles for
In the simulatorSimulated: it runs against the part the way it does on the bench.
Parts it drivesCharacter LCD with I2C backpack

Install it

It builds on every board Mokxi compiles for.

In Mokxi it is built in: #include <LiquidCrystal_PCF8574.h> and the sketch compiles, with no download, no zip and no Library Manager.

On a real board, install "LiquidCrystal_PCF8574" by Matthias Hertel from the Arduino IDE’s Library Manager (Sketch, Include Library, Manage Libraries), or add LiquidCrystal_PCF8574 to lib_deps in PlatformIO. Mokxi follows the calls of release 2.3.0, so a sketch written here builds there too.

The calls you will use

In the order a sketch meets them.

LiquidCrystal_PCF8574 lcd(0x27)

Makes the LCD at an I2C address. 0x27 is the most common; backpacks with a PCF8574A chip use 0x3F.

lcd.begin(16, 2)

Starts the LCD with its columns and rows. Use begin(20, 4) for the larger module.

lcd.setBacklight(255)

Turns the backlight on; 0 turns it off. Without this call the text is there but hard to read.

lcd.setCursor(0, 1)

Moves the cursor to a column and row, both counted from 0, so (0, 1) is the start of the second line.

lcd.print("Hello LCD")

Writes text or a number at the cursor. Text past the end of a line does not wrap onto the next one.

lcd.clear()

Blanks the screen and returns the cursor home. It takes a couple of milliseconds, so avoid it in a fast loop.

lcd.createChar(0, heart)

Stores a custom 5x8 character in one of eight slots; print it with lcd.write(0).

Example: An I2C LCD with LiquidCrystal_PCF8574

On the Arduino Uno R3. It runs in the browser exactly as printed.

sketch.ino · Arduino Uno R3
#include <Wire.h>
#include <LiquidCrystal_PCF8574.h>

LiquidCrystal_PCF8574 lcd(0x27);

void setup() {
  Serial.begin(9600);
  Wire.begin();
  Wire.beginTransmission(0x27);
  int error = Wire.endTransmission();
  Serial.print("Error: ");
  Serial.println(error);
  lcd.begin(16, 2);
  lcd.setBacklight(255);
  lcd.home();
  lcd.clear();
  lcd.print("Hello LCD");
}

void loop() {}

Wiring

  • Arduino Uno R3 A4 to Character LCD with I2C backpack SDA
  • Arduino Uno R3 A5 to Character LCD with I2C backpack SCL
  • Arduino Uno R3 5V to Character LCD with I2C backpack VCC
  • Arduino Uno R3 GND to Character LCD with I2C backpack GND

Open it in the editor, wired and ready to run

Common errors and how to fix them

Error: 2 on the Serial Monitor and a blank screen

Nothing acknowledged at 0x27. Check SDA and SCL (A4 and A5 on an Uno), then try 0x3F, or run an I2C scanner to find the real address and put it in the constructor.

The backlight is on but the screen shows only a row of blocks

begin() never ran or the contrast is wrong. Make sure begin(16, 2) is called before printing, then turn the contrast potentiometer on the backpack until characters appear.

no matching function for call to LiquidCrystal_PCF8574(int, int, int)

This library takes only the address in its constructor. Remove the columns and rows from the declaration and pass them to begin() instead.

Questions and answers

Should I use LiquidCrystal_PCF8574 or LiquidCrystal_I2C?

Either works with the same module. LiquidCrystal_PCF8574 is tidy on ESP boards and sets the size in begin(); LiquidCrystal_I2C sets it in the constructor and has more tutorials.

Try LiquidCrystal_PCF8574 without installing anything

Open the example, change the sketch, and keep your version in a free account.