16×2 LCD guides · On this page

Print Text on a 16×2 I2C LCD with an Arduino

Print text on a 16×2 LCD with an Arduino, running in your browser before you build it. The screen shows two lines of 16 characters, and the small board on its back, the I²C backpack, cuts its wiring to four wires: power, and I²C, a two-wire bus on which SDA carries the data and SCL the clock. The sketch must use the backpack's I²C address; this one uses 0x27, and if yours answers at another, find it.

Left to right: a green 16 by 2 character LCD with four pins sticking out of its left edge, from the I2C backpack on its back; the same 16 by 2 LCD with no backpack and sixteen header pins along its top edge; and a larger 20 by 4 character LCD with four pins out of its left edge. The screens are yellow-green and off.
16×2, 4 pinsI²C backpack: this guide16×2, 16 pinsNo backpack: six GPIO wires20×4, 4 pinsLarger: 4 lines of 20

Try it first, no parts needed

This is the whole project, wired and running in your browser. Press Run simulation and watch the LCD: Hello, world! on the top line, and below it the seconds since the Uno started, counting up. The screen is drawn large under the circuit.

Ready
Open in Cirkit Designer

What the LCD shows

The display's 16 × 2 characters, enlarged.

The screen lights up when the simulation runs.

Arduino Uno serial monitor115200 baud

Output appears here when the simulation runs.

What the code does

The sketch starts the LCD, prints Hello, world! once, then prints the seconds on the second line every second.

hello_lcd.ino
// A 16x2 I2C LCD: Hello, world! on line 1 and the seconds
// since the Uno started on line 2, also on the Serial Monitor.
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

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

void setup() {
  Serial.begin(115200);
  lcd.init();       // start the LCD and its backpack
  lcd.backlight();
  lcd.setCursor(0, 0);  // column 0, line 1
  lcd.print("Hello, world!");
}

void loop() {
  unsigned long seconds = millis() / 1000;
  lcd.setCursor(0, 1);  // column 0, line 2
  lcd.print(seconds);
  lcd.print(" s");
  Serial.print(seconds);
  Serial.println(" s");
  delay(1000);
}
  1. LiquidCrystal_I2C lcd(0x27, 16, 2);  // address, columns, rows

    The LCD: its backpack's I²C address, then 16 columns and 2 lines. If your screen stays blank, check the contrast, then the address; see troubleshooting.

  2. lcd.init();       // start the LCD and its backpack

    Starts I²C and sets the LCD up: two lines, the display on, no cursor, the screen cleared. It waits about a second first, so the text appears a moment after the Uno starts.

  3. lcd.backlight();

    Turns the backlight on. The library starts with it off, so without this line the text is hard to see.

  4. lcd.setCursor(0, 0);  // column 0, line 1

    Puts the next character at column 0 of the top line; setCursor() numbers columns and lines from 0. print() then writes Hello, world! from there, left to right.

  5. lcd.setCursor(0, 1);  // column 0, line 2

    Moves to the start of the second line and prints the seconds since the Uno started, then a space and s. The count only grows, so each new number covers the last one.

  6. Serial.print(seconds);

    Prints the same count on the Serial Monitor, so you can check the board is running even before the screen works.

Build it on an Arduino

Now with real parts. Check which pin is which, wire it, get the code, then upload it and watch the screen. Using an ESP32 board? It has its own guide →

Which pin is which: the I²C LCD pinout

The four pins are on the backpack. On the module pictured they are at the screen's left edge with the screen facing you: GND, VCC, SDA and SCL, top to bottom. Backpacks differ, so wire yours by the labels printed beside its pins.

16×2 I2C LCD pinout, the backpack's four pins at the screen's left edge on the module pictured, top to bottom: GND, ground, to GND on an Uno; VCC, power, 5 V, to 5V; SDA, I2C data, to A4; SCL, I2C clock, to A5. The backpack's address is set by its chip's A0 to A2 inputs: all HIGH, it is 0x27 on a PCF8574 and 0x3F on a PCF8574A. Its small potentiometer sets the contrast.
Shows2 lines of 16 characters, each 5 × 8 dots
PinsGND, VCC, SDA, SCL
Supply5V on an Uno
InterfaceI²C: 0x27 or 0x3F with A0 to A2 high
Characters208 built in, and 8 you can design
LibraryLiquidCrystal I2C, by Frank de Brabander

You'll need

  • An Arduino Uno and its USB cable
  • A 16×2 LCD with an I²C backpack, four pins: GND, VCC, SDA, SCL
  • Four female-to-male jumper wires
  • Software to upload the sketch to the Uno: either Cirkit Designer, free in your browser, or the Arduino IDE, a free download for Windows, macOS and Linux
  1. Wire it

    Four wires, each from one of the backpack's pins straight to the Uno.

    Tap a connection to see it on the circuit.

    LCD pinConnect to the UnoShow
    GNDGNDShow
    VCC5VShow
    SDAA4Show
    SCLA5Show

    A4 is SDA and A5 is SCL on an Uno. The picture takes GND from the pin beside pin 13; any of the Uno's GND pins works.

  2. Get the code

    I'm using:

    Steps 2 and 3 follow the tool you pick.

    Open in Cirkit Designer

    The project opens with the sketch and its library already in it, so there's nothing to install.

    No Arduino IDE yet? Download it free for Windows, macOS or Linux.

    The sketch needs one library. Open the Library Manager (1), search for LiquidCrystal I2C (2), and install the one by Frank de Brabander (3).

    Arduino UnoLIBRARY MANAGERLiquidCrystal I2CType: AllTopic: AllLiquidCrystal I2Cby Frank de BrabanderA library for I2C LCD displays.More infoINSTALL123

    Then copy the sketch into a new one:

    How it works line by line is in what the code does, above.

  3. Upload and check it

    Plug in your Uno. In Cirkit, open the Build tab, then Upload firmware: click Select Port and pick your Uno, then Upload to Device. The first time, Cirkit asks you to install Cirkit Create Agent, a small desktop helper that lets it reach the board over USB. Then open Upload output and serial monitor and click Start Serial Monitor.

    Show me where to click in Cirkit
    16x2 I2C LCD on an Arduino UnoFileEditExportHelp
    DesignCodeSimulateBuild
    1Parts & purchasing2Assemble3Upload firmware
    Arduino UnoEdit Code

    Board

    Arduino Uno Change

    2

    Port

    Select PortCOM3 Connected

    3

    Upload

    Upload to DeviceUploading...Upload successful

    4

    Check that it works

    Your project should now run as designed.

    Upload output and serial monitor
    Compile / Upload Output
    Sketch uses 4452 bytes (13%) of program storage space. Maximum is 32256 bytes.
    Global variables use 452 bytes (22%) of dynamic memory, leaving 1596 bytes for local variables. Maximum is 2048 bytes.
    Device SerialStop

    Read serial data from your device.

    Start Serial Monitor
    Listening on COM3 at 115200 baud
    1 s
    2 s
    3 s
    4 s
    5 s
    Select port

    Select a device port

    Select the device port for uploading code and serial communications

    COM3 (Arduino Uno)
    1. Open Build
    2. Choose Upload firmware
    3. Click Select Port
    4. Pick COM3 (Arduino Uno)
    5. Click Upload to Device
    6. Open Upload output and serial monitor
    7. Click Start Serial Monitor

    In the Arduino IDE, choose Arduino Uno and its port from the board menu, press Upload, then open the Serial Monitor and set it to 115200 baud.

    Show me where to click in the Arduino IDE
    Select BoardArduino Uno

    Select other board and port

    Arduino UnoCOM3
    hello_lcd.ino
    // A 16x2 I2C LCD: Hello, world! on line 1 and the seconds
    // since the Uno started on line 2, also on the Serial Monitor.
    #include <Wire.h>
    #include <LiquidCrystal_I2C.h>
    
    LiquidCrystal_I2C lcd(0x27, 16, 2);  // address, columns, rows
    OutputSerial Monitor115200 baud

    Compiling sketch...

    Sketch uses 4452 bytes (13%) of program storage space. Maximum is 32256 bytes.

    Global variables use 452 bytes (22%) of dynamic memory, leaving 1596 bytes for local variables. Maximum is 2048 bytes.

    Uploading...

    1 s

    2 s

    3 s

    4 s

    5 s

    Done uploading.

    1. Click the board menu
    2. Pick Arduino Uno on COM3
    3. Click Upload
    4. Wait for Done uploading
    5. Click Serial Monitor
    6. Check it is set to 115200 baud

    It works when:

    • The backlight comes on and, about a second after the Uno starts, the top line shows Hello, world!
    • The second line counts up once a second: 1 s, 2 s, 3 s.
    • The Serial Monitor prints the same count.
    The LCD three seconds after the Uno starts: Hello, world! on the top line and 3 s on the second.
    Three seconds in: Hello, world! on the top line and the count below it.

    Not working? If the screen stays blank, see troubleshooting.

    Next guideNext: show temperature and humidity on itA DHT11 and the same LCD: the temperature on the top line and the humidity below, every two seconds.

Troubleshooting

Find your symptom; the checks under it run from the most common cause down.

The backlight is on, but there's no text

If the Serial Monitor counts, the Uno is running and the trouble is between it and the screen. The backpack's chip starts with its outputs high, so the backlight comes on even before the sketch reaches it. Check, in order:

  • The contrast. Turn the small potentiometer on the back of the backpack slowly, all the way one way and back, while the sketch runs.
  • The address. If it isn't the one in the sketch, nothing reaches the screen. Try 0x3F in place of 0x27, or find the address.
  • SDA goes to A4 and SCL to A5. Swapped, the Uno can't reach the backpack.

No backlight and no text

  • VCC is on 5V and GND on GND.
  • The sketch calls lcd.backlight(). The library turns the backlight off when it starts the LCD, so the sketch has to turn it on.

Old characters stay on the screen

The LCD keeps every character until something is written over it. When a number gets shorter, say 10 to 9, its last digit stays behind. Print spaces after it to cover the old digits, or call lcd.clear() before writing the line again.

A backslash shows as ¥

The LCD draws characters from its own character set, not your computer's. On the common one, Hitachi's ROM code A00, code 92 is ¥ where ASCII has a backslash, and codes 126 and 127 are arrows where ASCII has ~ and DEL. Letters, digits and most punctuation match ASCII.

LiquidCrystal_I2C.h: No such file or directory

Arduino IDE only
  • The library is installed. Install LiquidCrystal I2C by Frank de Brabander from the Library Manager, as in get the code. Cirkit Designer includes it.

Nothing in the Serial Monitor, or garbled characters

  • The Serial Monitor is set to 115200 baud. It must match Serial.begin(115200) in the sketch.

How the backpack drives the LCD

The LCD itself is an HD44780-compatible character display with 16 pins of its own. The backpack is a small I²C chip, a PCF8574, wired to those pins: the Uno sends it a byte over SDA and SCL, and it sets its eight outputs, P0 to P7, to that byte's bits.

How the backpack drives the LCD. The Uno sends bytes to the backpack's PCF8574 over I2C, on SDA and SCL. The PCF8574 sets its outputs to each byte's bits: P0 to the LCD's RS, P1 to RW, P2 to E, P3 to the backlight through a transistor, and P4 to P7 to the LCD's data pins D4 to D7. Each character goes to the LCD in two halves of four bits, the high half first, each latched by a pulse on E; the letter H, 0x48, goes as 0100 then 1000.

Four of the outputs reach the LCD's data pins, D4 to D7, so the LCD runs in its 4-bit mode: each character goes over in two halves, high half first. The library sets each half on the data pins, then pulses E to latch it. RS tells the LCD whether the byte is a character to show or a command, such as clear or move the cursor, and P3 switches the backlight. That is why lcd.backlight() needs the right address too.

The LCD keeps the text in its own memory, 40 characters for each line, and shows the first 16. Each character is a 5 × 8 dot pattern from its built-in set of 208, or one of 8 you design. The sketch only says where to write and what.

Where to write is the LCD's address counter. setCursor() sets it, line 1 counting from address 0x00 and line 2 from 0x40, and each character written moves it on by one. The figure under the grid follows it in the running circuit.

The LCD's 16 columns and 2 lines, numbered from 0 as setCursor(column, row) takes them. The top line shows Hello, world!, written after setCursor(0, 0); the second line shows 2 s, written after setCursor(0, 1).
Where the next character goesExample: the circuit two seconds inRun the circuit at the top, and this follows the LCD as the sketch writes.

The address counter is at 0x43: the next character goes to line 2, column 3.

Limits

16×2 I²C LCD
Text2 lines of 16 characters on screen; each line holds 40 in the LCD's memory
Characters208 built in (on ROM A00, ASCII letters, digits and most punctuation, plus Japanese katakana and symbols), and 8 of your own, 5 × 8 dots each
SpeedMost commands take the LCD up to 37 µs, and clear and home up to 1.52 ms, at its typical 270 kHz clock; the library waits 2 ms after sending clear or home
Addresses0x20 to 0x27 on a PCF8574 backpack, 0x38 to 0x3F on a PCF8574A: up to eight of a kind on one bus
I²C clockUp to 100 kHz on a PCF8574
Library boardsLiquidCrystal I2C 1.1.2 lists AVR boards, such as the Uno and Nano; it builds for the ESP32 too, with a warning

To change a number often, write over just that number with setCursor() and a few spaces rather than clearing the screen each time: a clear blanks the whole screen for a moment, and the library waits 2 ms after it.

16×2 I²C LCD or something else?

ShowsWiringLibraryChoose it for
16×2 LCD with an I²C backpack2 lines of 16 characters4 wires: power and I²CLiquidCrystal I2CReadable text with the fewest wires
16×2 LCD without a backpackThe sameSix Uno pins (RS, E, D4 to D7), plus a 10 kΩ potentiometer for the contrastLiquidCrystal, built into the Arduino IDENo I²C address to find, if you have the pins to spare
20×4 LCD with an I²C backpack4 lines of 20 characters4 wires: power and I²CLiquidCrystal I2C, as lcd(0x27, 20, 4)More text, with the same wiring and code
128×64 SSD1306 OLEDText at any size and graphics, 128 × 64 pixels4 wires: power and I²CAdafruit SSD1306Graphics or large text. See the OLED guide

16×2 I²C LCD FAQ

How do I find my I²C LCD's address?

With its address inputs A0 to A2 all high, a backpack answers at 0x27 if its chip is a PCF8574 and 0x3F if it is a PCF8574A, so try those first. To be sure, upload the I²C scanner sketch with the LCD connected and set the Serial Monitor to 9600 baud: it lists every address that answers. Put that address in lcd(…), upload this sketch again and set the Serial Monitor back to 115200.

How do I print a degree sign on the LCD?

Print character 223: lcd.print((char)223);. On the common character set, ROM A00, it's a small raised circle; on the European set, ROM A02, the degree sign is 176. The DHT11 project uses it.

Can I use a 20×4 LCD?

Yes, if it has an I²C backpack. Give the library its size, and use rows 0 to 3 in setCursor():

In place of the lcd line at the top of the sketch
LiquidCrystal_I2C lcd(0x27, 20, 4);  // address, columns, rows
How do I turn the backlight off?

Call lcd.noBacklight(); lcd.backlight() turns it on again. The text stays, but it is hard to read without the light.

Anywhere in the sketch, after lcd.init()
lcd.noBacklight();  // off
lcd.backlight();    // on again
Can I connect two I²C LCDs to one Arduino?

Yes, if their addresses differ. Each backpack's chip has three address inputs, A0 to A2, held high on the board; connect one of them to GND on the second backpack (DFRobot's has jumpers for this), give each LCD its own LiquidCrystal_I2C line with its address, and wire both to A4 and A5.

Does the I²C LCD work with an ESP32?

Yes, through a logic level converter. A backpack whose SDA and SCL pull-ups go to its own supply, as on the main header of DFRobot's DFR0063, holds them at 5 V when it runs on 5 V, more than an ESP32's pins take; the converter keeps the ESP32's side at 3.3 V. The ESP32 guide shows the wiring.

Simulating the 16×2 I²C LCD in Cirkit Designer

Cirkit's LCD answers the LiquidCrystal I2C library over the Uno's real I²C pins, as a PCF8574 backpack on an HD44780: P0 to RS, P1 to RW, P2 to E, P3 to the backlight and P4 to P7 to D4 to D7, in 4-bit mode. It keeps 40 characters a line and 8 characters of your own, and takes a fixed 37 µs or 1.52 ms for each command, the HD44780's times at its typical clock. Its address is 0x27 unless you change it in the part's properties, to any of 0x20 to 0x27 or 0x38 to 0x3F. It needs 2.7 to 5.5 V between VCC and GND, and it is tested on the Arduino Uno and the ESP32-S3.

It draws printable ASCII, the degree sign (223) and a full block (255); other codes show as a box, and the backslash and ~ draw as on a computer, not as ¥ and an arrow as on a ROM A00 chip. There is no contrast to adjust: the text is always clear. It doesn't model current, temperature or the timing of the I²C signals themselves.

References