Sub topic 3
Printing Text on a 16x2 LCD with LiquidCrystal
This chapter assumes that you can upload an Arduino sketch, use setup() and loop(), wire a breadboard, and read a wiring table. It also assumes an Arduino Uno or another 5 V board and a standard 16x2 HD44780-compatible LCD.
A 16x2 LCD has two rows with 16 character positions per row. The HD44780 controller accepts commands and character data through several digital pins. In 4-bit mode, the Arduino sends each byte in two parts, so only four data lines are needed.
Wiring the LCD in 4-bit mode
The LCD needs power, a contrast voltage, a register-select signal, an enable signal, and four data signals. Tie the R/W pin to ground because this example only writes to the display.
Use a 10 kΩ potentiometer to control contrast. Connect its two outer terminals to 5 V and ground, and connect its middle terminal to V0 or VO.
| LCD pin | Name | Connection |
|---|---|---|
| 1 | VSS | Ground |
| 2 | VDD | 5 V |
| 3 | V0/VO | Potentiometer wiper |
| 4 | RS | Arduino pin 12 |
| 5 | R/W | Ground |
| 6 | E | Arduino pin 11 |
| 11 | D4 | Arduino pin 5 |
| 12 | D5 | Arduino pin 4 |
| 13 | D6 | Arduino pin 3 |
| 14 | D7 | Arduino pin 2 |
| 15 | LED+ | 5 V through a resistor if required |
| 16 | LED- | Ground |
The LCD's pin numbers are usually printed on its header or listed in its datasheet. Check the module before applying power. Some display boards include a backlight resistor, while others require an external resistor.
The data flow is:
Creating the LiquidCrystal object
LiquidCrystal is included with the Arduino IDE, so no separate library installation is normally required. Its constructor receives the Arduino pins connected to RS, E, D4, D5, D6, and D7, in that order.
#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);
}lcd.begin(16, 2) configures the controller for 16 columns and 2 rows. Call it before sending text.
lcd.print() writes characters beginning at the current cursor position. After lcd.begin(), the cursor starts at column 0, row 0. The first call therefore writes on the top row.
lcd.setCursor(column, row) selects a position. Both values start at zero, so lcd.setCursor(0, 1) selects the first character of the second row. The expression millis() / 1000 converts the number of milliseconds since startup into whole seconds.
Avoiding common layout problems
The LCD does not automatically erase characters that were previously printed. For example, printing 123 and then printing 9 leaves 923 unless the old characters are overwritten. When displaying values with changing digit counts, print padding spaces or clear the field first.
Also remember that lcd.clear() clears the display and moves the cursor home, but repeatedly calling it in a fast loop can cause visible flicker. For simple static text, write it once in setup(). For changing text, update only the required field and control the update interval.
Exercise
Which six Arduino pins are passed to the LiquidCrystal constructor in this example, and what does lcd.setCursor(0, 1) select?
Show answer
The constructor receives pins 12, 11, 5, 4, 3, and 2 for RS, E, D4, D5, D6, and D7. lcd.setCursor(0, 1) selects column 0 on row 1, which is the first character position on the second row.