Sub topic 3
Printing text on a 16x2 LCD with LiquidCrystal
This chapter wires a 16x2 character LCD to an Arduino in 4-bit parallel mode and prints text with the built-in LiquidCrystal library. It assumes you can upload a sketch, wire a breadboard with power, ground, and signal jumpers, and that you know the setup()/loop() structure. You don't need to install anything. LiquidCrystal ships with the Arduino IDE.
What the display contains
A 16x2 character LCD shows 2 rows of 16 characters. Each character cell is a 5x8 pixel grid. Almost every module of this type uses the Hitachi HD44780 controller chip, or a compatible clone, and the LiquidCrystal library speaks that chip's protocol.
The controller keeps the text in its own memory, called DDRAM. Once you write a character, it stays on screen until you overwrite or clear it. Your sketch doesn't need to refresh the display. This is the main difference from the OLED chapters later in the tutorial, where your sketch keeps a frame buffer.
Why 4-bit mode
The HD44780 has 8 data pins, D0 to D7. It also works with only D4 to D7. In 4-bit mode the library sends each byte as two 4-bit halves (nibbles). That costs a little speed, which you won't notice on a 16x2 screen, and it saves four Arduino pins. The control lines are RS, E, and optionally RW. This leaves you 6 signal pins in total.
| Signal | Job |
|---|---|
| RS (register select) | Low means the byte is a command. High means it is character data. |
| E (enable) | A pulse tells the controller to read the data pins. |
| RW (read/write) | Low means write. The library only writes, so you tie this pin to ground. |
| D4 to D7 | The 4 data lines. |
Wiring the module
Most modules have a 16-pin header. The numbering below is the standard one, but check the labels printed on your board.
| LCD pin | Name | Connect to |
|---|---|---|
| 1 | VSS | GND |
| 2 | VDD | 5 V |
| 3 | V0 | Middle pin of a 10 kΩ potentiometer (contrast) |
| 4 | RS | Arduino pin 12 |
| 5 | RW | GND |
| 6 | E | Arduino pin 11 |
| 7 to 10 | D0 to D3 | Not connected |
| 11 | D4 | Arduino pin 5 |
| 12 | D5 | Arduino pin 4 |
| 13 | D6 | Arduino pin 3 |
| 14 | D7 | Arduino pin 2 |
| 15 | A (backlight anode) | 5 V, through a resistor of about 220 Ω unless the board already has one |
| 16 | K (backlight cathode) | GND |
Connect the two outer pins of the potentiometer to 5 V and GND. Its middle pin goes to V0.
Your first sketch
This sketch prints a fixed line on row 0 and a running seconds counter on row 1.
#include <LiquidCrystal.h>
// RS, E, D4, D5, D6, D7
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup() {
lcd.begin(16, 2);
lcd.print("Hello, world!");
}
void loop() {
lcd.setCursor(0, 1);
lcd.print(millis() / 1000);
}Upload it and turn the potentiometer slowly. At one end of its range you should see the text appear. Contrast is the first thing to adjust on any new module.
What each call does
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);creates the object. The argument order is RS, E, D4, D5, D6, D7. This form omits RW, which tells the library you have grounded it. If you have wired RW to an Arduino pin, use the 7-argument formLiquidCrystal(rs, rw, enable, d4, d5, d6, d7).lcd.begin(16, 2)sets the geometry (columns, then rows) and runs the HD44780 initialization sequence. Without it the display stays uninitialized and shows nothing useful.lcd.print(...)writes at the current cursor position and advances the cursor. It accepts strings, integers, and floats because the library inherits from Arduino'sPrintclass.lcd.setCursor(col, row)moves the cursor. Both values start at 0, sosetCursor(0, 1)is the first column of the second row.
The constructor only stores the pin numbers. The pin modes are set by begin(). Because of this, you can create the object at global scope, as the sketch does, without running into initialization-order problems.
Printing different kinds of data
Because the library uses Print, you can format values the same way you do with Serial.
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup() {
lcd.begin(16, 2);
lcd.setCursor(0, 0);
lcd.print(F("Temp: ")); // string stored in flash, not RAM
lcd.print(23.456, 1); // float with 1 decimal: 23.5
lcd.print((char)223); // degree symbol on most ROMs
lcd.print('C');
lcd.setCursor(0, 1);
lcd.print(F("Raw: 0x"));
lcd.print(255, HEX); // prints FF
}
void loop() {}The F() macro keeps string literals in flash memory on AVR boards such as the Uno. This matters on a board with only 2 KB of RAM.
The degree symbol is character code 223 in the most common ROM variant (A00). The character set above code 127 varies between ROM variants. If you see a different symbol, your module uses another ROM. The first 127 codes are ASCII for letters, digits, and punctuation, with a few exceptions. For example, code 92 is a yen sign on some ROMs rather than a backslash.
Leftover characters
The display keeps whatever you wrote last. If a value shrinks from 100 to 99 and you print at the same position, the screen shows 990. The last 0 was never overwritten.
There are two common fixes. You can call lcd.clear(), which wipes both rows and returns the cursor to the top left. It is slow, about 1.5 ms, and when you call it every pass of loop() it makes the display visibly flicker. Or you can overwrite only the field that changes by padding it with spaces:
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int value = 120;
void setup() {
lcd.begin(16, 2);
lcd.print(F("Count:"));
}
void loop() {
lcd.setCursor(7, 0);
lcd.print(value);
lcd.print(F(" ")); // blank out digits left from a longer number
value--;
if (value < 0) {
value = 120;
}
delay(200);
}When the count passes from 100 to 99, the trailing spaces erase the old last digit. This pattern works for any field with a known maximum width. The chapter on updating readings returns to it in more detail.
Useful library methods
| Method | Effect |
|---|---|
| `clear()` | Clears all text and moves the cursor to (0, 0). |
| `home()` | Moves the cursor to (0, 0) without clearing. |
| `noDisplay()` / `display()` | Hides or shows the text without erasing it. |
| `cursor()` / `noCursor()` | Shows or hides the underline cursor. |
| `blink()` / `noBlink()` | Turns the blinking block cursor on or off. |
| `scrollDisplayLeft()` / `scrollDisplayRight()` | Shifts the whole display window by one position. |
| `autoscroll()` / `noAutoscroll()` | Shifts the display on each new character instead of moving the cursor. |
| `leftToRight()` / `rightToLeft()` | Sets the direction the cursor moves after each character. |
| `createChar(slot, bytes)` | Stores a custom character. A later chapter covers it. |
How the screen memory is laid out
Each row of the controller's memory is 40 characters long, but the glass shows only 16 of them. The visible window starts at the left of each row.
This has three consequences:
- If you print more than 16 characters on row 0, the extra characters are stored but not shown. They do not wrap onto row 1. You can see them by calling
scrollDisplayLeft(). scrollDisplayLeft()moves the window over the 40-character rows. Both rows scroll together. Text you wrote earlier can reappear when you scroll far enough.- To scroll a message longer than 16 characters across one row, you normally keep the string in your sketch and reprint 16-character slices of it. This is easier to control than hardware scrolling.
Choosing pins and the cost of sharing them
You can use any digital pins for the six signals. The pins 12, 11, 5, 4, 3, 2 in the example come from the Arduino documentation and have no special meaning. Two things limit the choice:
- Avoid pins 0 and 1 on an Uno if you use
Serial, since they carry the USB serial traffic. - If you plan to add other hardware, such as an SPI shield or an SD card, check which pins it uses (pins 10 to 13 on an Uno) before you fix the LCD's pins.
If this takes too many pins for your project, the next chapters show an I2C backpack that cuts the six signal pins to two.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Top row shows solid blocks, bottom row is empty | The controller is powered but not initialized. | Check the RS, E, and D4 to D7 wires against the constructor order. Make sure `begin()` runs. |
| Backlight is on but nothing else shows | Contrast is set wrongly. | Turn the potentiometer through its whole range. |
| Random or wrong characters | One data wire is loose or swapped. | Check D4 to D7 against the pin order in the constructor. |
| Text works only after you press reset | The module's power-up timing is off, or the supply is weak. | Power the module from a stable 5 V supply. Add a short `delay(50)` before `begin()` if the problem persists. |
| Extra characters after a number shrinks | Old characters were never overwritten. | Pad the field with spaces or use `clear()`. |
| Second row empty on a new module | `begin()` was called with 1 row, or the module has a different layout. | Call `lcd.begin(16, 2)`. |
| Backlight is very dim or the board resets | The backlight draws more current than the pin or resistor supports. | Feed the backlight from 5 V through the correct resistor, not from a data pin. |
Timing and blocking
LiquidCrystal is synchronous. Each call waits until the controller has finished, so lcd.print() blocks for a short time. A full 16-character update takes a few milliseconds in 4-bit mode. For normal sensor readouts this is invisible. If you run time-critical code, such as reading a fast encoder or generating a precise pulse, update the display at a limited rate. A common pattern is to update every 200 to 500 ms, using millis() instead of delay(), and to write only when the value has changed.
Exercise
A countdown prints 100, then 99, at setCursor(7, 0), and the screen shows 990 after the change. Name the cause and give two ways to fix it.
Show answer
The controller keeps old characters until they are overwritten, and 99 is two characters while 100 was three, so the final 0 stays. Fix it by printing trailing spaces after the number (for example lcd.print(" ")) so the old digit is overwritten, or by calling lcd.clear() before each update. Padding avoids the flicker that clear() causes.
Exercise
Your sketch calls lcd.print("This is a long message") on row 0 of a 16x2 display. What do you see, and where do the remaining characters go?
Show answer
The display shows the first 16 characters, "This is a long me". The rest ("ssage") is written into the controller's hidden memory beyond column 16 of row 0. It does not wrap to row 1. You can bring it into view with lcd.scrollDisplayLeft(), or you can print slices of the string yourself.