Lesson 1.11: LCD display
A text display makes a project self-contained: you no longer need the computer and the serial monitor to know what's going on.
Parts
Libraries
- LiquidCrystal — bundled with the IDE
How it works
The 1602 LCD has an HD44780 controller that receives commands and characters. The RS pin says which of the two you're sending: LOW for a command —clear, move the cursor—, HIGH for a character.
Data can go as 8 bits or as two 4-bit halves. In 4-bit mode, D4–D7 plus RS and E are enough: six pins in total. E (enable) is the pulse that tells the display «read now».
RW selects between writing and reading; since we only write, it's tied to GND. The potentiometer on V0 sets the contrast, not the brightness: set wrong, the display looks empty even when it's working.
Wiring
| Component | Connects to | Note |
|---|---|---|
| LCD, VSS (1) | GND | |
| LCD, VDD (2) | 5V | |
| LCD, V0 (3) | Potentiometer, middle pin | Potentiometer outer pins to 5V and GND |
| LCD, RS (4) | Pin 12 | |
| LCD, RW (5) | GND | |
| LCD, E (6) | Pin 11 | |
| LCD, D4 (11) | Pin 5 | |
| LCD, D5 (12) | Pin 4 | |
| LCD, D6 (13) | Pin 3 | |
| LCD, D7 (14) | Pin 2 | |
| LCD, A (15) | 5V | Through a 220 Ω resistor if your module doesn't have one |
| LCD, K (16) | GND |
Code
// Greeting on the first row and seconds since power-up on the second.
#include <LiquidCrystal.h>
// RS, E, D4, D5, D6, D7
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup() {
lcd.begin(16, 2); // 16 columns, 2 rows
lcd.print("Hello, world!");
}
void loop() {
lcd.setCursor(0, 1); // column 0 of the second row
lcd.print(millis() / 1000);
lcd.print(" s");
delay(200);
}What the code does
- The LiquidCrystal constructor takes the pins in the order RS, E, D4, D5, D6, D7.
- begin(16, 2) tells it the display size; without it, setCursor() doesn't know where the second row starts.
- setCursor(column, row) counts from 0: (0, 1) is the start of the second line.
- The display has no accented letters: its character set is the controller's. Write without them or create your own characters with createChar().
Step by step
- Place the display and the potentiometer on the breadboard and bring 5V and GND to the rails.
- Wire the power, backlight and contrast, and check that turning the potentiometer shows blocks on the first row.
- Connect RS, E and D4–D7, and upload the code.
- Adjust the potentiometer until the text is easy to read.
Result
The first row reads «Hello, world!» and the second counts the seconds since the board started.
If it doesn't work
- If you only see blocks on the first row, the display has power but receives no data: check RS, E and D4–D7.
- If you see nothing, turn the potentiometer from one end to the other: contrast is the most common cause.
- If the backlight doesn't come on, check A and K; on some modules they're labelled LED+ and LED−.
- If odd characters appear, D4–D7 are crossed.