A 16x2 LCD lets you show text on a small screen with 2 lines and 16 characters each. The LiquidCrystal library helps you easily control this screen from your Arduino.
0
0
16x2 LCD with LiquidCrystal library in Arduino
Introduction
You want to display sensor readings like temperature or humidity.
You want to show messages or instructions on a small device.
You want to debug your Arduino by showing variable values.
You want to create a simple user interface with menus.
You want to display time or status updates in a project.
Syntax
Arduino
LiquidCrystal(rs, enable, d4, d5, d6, d7); lcd.begin(16, 2); lcd.print("text");
rs, enable, d4, d5, d6, and d7 are Arduino pins connected to the LCD.
lcd.begin(16, 2) sets the LCD size to 16 columns and 2 rows.
Examples
Basic setup with pins 12, 11, 5, 4, 3, 2 connected to the LCD. Prints "Hello World" on the screen.
Arduino
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); lcd.begin(16, 2); lcd.print("Hello World");
Moves the cursor to the first column of the second line and prints text there.
Arduino
lcd.setCursor(0, 1); lcd.print("Line 2 text");
Clears the screen before printing a new message.
Arduino
lcd.clear(); lcd.print("New message");
Sample Program
This program sets up the LCD and prints "Hello, Arduino!" on the first line. The screen stays showing this message.
Arduino
#include <LiquidCrystal.h> // Initialize the library with the numbers of the interface pins LiquidCrystal lcd(12, 11, 5, 4, 3, 2); void setup() { lcd.begin(16, 2); // Set up the LCD's number of columns and rows lcd.print("Hello, Arduino!"); // Print a message to the LCD } void loop() { // Nothing to do here }
OutputSuccess
Important Notes
Make sure your LCD is wired correctly to the Arduino pins you specify.
Use lcd.setCursor(col, row) to move where you print text.
Use lcd.clear() to erase the screen before printing new text.
Summary
The LiquidCrystal library controls 16x2 LCDs easily.
Initialize with pin numbers and call lcd.begin(16, 2).
Use lcd.print() to show text and lcd.setCursor() to move the cursor.
