Hello on a 16×2 LCD
Print text and a running timer on a character LCD driven in 4-bit mode with LiquidCrystal.
Displays · Beginner
In a real product: Vending machine (Consumer electronics): A character LCD shows prices and messages.
Character LCDs based on the HD44780 controller are everywhere: printers, microwaves, lab instruments. They show a grid of characters (here 2 rows of 16) and take commands and text over a parallel bus. The LiquidCrystal library talks to them.
What you'll learn
- Wiring an HD44780 LCD in 4-bit mode (6 signal pins instead of 10)
lcd.begin(),lcd.print()andlcd.setCursor()- Overwriting changing text in place
How it works
RS (pin 12) chooses between commands and characters, and E (pin 11) is a strobe: the controller reads the data lines on its falling edge. In 4-bit mode only D4–D7 (pins 5, 4, 3, 2) are used, and each byte is sent as two halves. RW is tied to GND because the sketch only ever writes. VDD/VSS power the logic and A/K the backlight.
lcd.begin(16, 2) sets the controller up. setCursor(0, 1) moves to column 0 of row 1 (the second line), and each loop overwrites the uptime there.
On a real LCD the contrast pin (V0) goes to a potentiometer; without it the text may be invisible.
Try this
- Scroll a long message across the top line with
lcd.scrollDisplayLeft(). - Make a custom character (a heart, a battery) with
lcd.createChar(). - Show a sensor reading, such as the temperature from the NTC project.
sketch.ino
// A 16x2 LCD in 4-bit mode: RS 12, E 11, D4-D7 on pins 5, 4, 3, 2.
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup() {
lcd.begin(16, 2);
lcd.print("Hello, world!");
}
void loop() {
lcd.setCursor(0, 1); // column 0, second line
lcd.print("Uptime: ");
lcd.print(millis() / 1000);
lcd.print(" s");
delay(200);
}