Introduction
In this tutorial we will learn How to interface 16×2 LCD display using Arduino UNO. LCD displays available in various sizes 8×1, 16×1, 16×2, 16×4, 20 Char x 4 Lines. These all displays can be interfaced using this tutorial. We are more focusing on 16×2 LCD. It is commonly used.
Interfacing a character LCD to an Arduino UNO adds a nice element of readability to your project. Many of the best Arduino projects around the world sport LCD displays. These LCDs can be used to display information from the Arduino or any sensor connected to it. For example, you can create a temperature monitoring system which displays the temperature on your Arduino. You can make your own speedometer that displays your speed on the LCD! Depending on what you want to build, an LCD is a highly useful output device for your Arduino.
What we are learning in this tutorial?
-
Pin configuration of LCD.
-
How to connect LCD with Arduino UNO?
-
How to move cursor on LCD?
-
How to make custom characters?
LCD Pinout
All HD44780 (JHD16x2) driver based LCDs have 14 or 16 line interface. Backlight connections are connected to +5V supply through 330 Ohm current limiting resistor. In most cases we don’t read the display so it is better to connect R/W LCD pin to GND. For contrast setting, connect LCD contrast Pin 3 to ground through a 1K Ohm resistor. It gives optimum value of contrast. We don’t need to adjust the contrast every time using variable resistor so it is better to use fixed resistor, saves cost and space on PCB.
The LCDs have a parallel interface, meaning that the microcontroller has to manipulate several interface pins at once to control the display. The interface consists of the following pins:
A register select (RS) pin that controls where in the LCD’s memory you’re writing data to. You can select either the data register, which holds what goes on the screen, or an instruction register, which is where the LCD’s controller looks for instructions on what to do next.
A Read/Write (R/W) pin that selects reading mode or writing mode
An Enable pin that enables writing to the registers
8 data pins (D0 -D7). The states of these pins (high or low) are the bits that you’re writing to a register when you write, or the values you’re reading when you read.
There’s also a display constrast pin (Vo), power supply pins (+5V and Gnd) and LED Backlight (Bklt+ and BKlt-) pins that you can use to power the LCD, control the display contrast, and turn on and off the LED backlight, respectively.
The process of controlling the display involves putting the data that form the image of what you want to display into the data registers, then putting instructions in the instruction register. The LiquidCrystal Library simplifies this for you so you don’t need to know the low-level instructions.
The Hitachi-compatible LCDs can be controlled in two modes: 4-bit or 8-bit. The 4-bit mode requires seven I/O pins from the Arduino, while the 8-bit mode requires 11 pins. For displaying text on the screen, you can do most everything in 4-bit mode, so example shows how to control a 2×16 LCD in 4-bit mode.
Connecting LCD to Arduino UNO
Before wiring the LCD screen to your Arduino UNO or Genuino board we suggest to solder a pin header strip to the 14 (or 16) pin count connector of the LCD screen, as you can see in the image above.
To wire your LCD screen to your board, connect the following pins:
- LCD RS pin to digital pin 12
- LCD Enable pin to digital pin 11
- LCD D4 pin to digital pin 5
- LCD D5 pin to digital pin 4
- LCD D6 pin to digital pin 3
- LCD D7 pin to digital pin 2
Additionally, wire a 1k resistor in between GND and LCD screens VO pin (pin3). A 220 ohm resistor is used to power the backlight of the display, usually on pin 15 and 16 of the LCD connector
Connect the LCD as shown in circuit diagram.
Arduino Commands for LCD control
Arduino software comes with “LiquidCrystal” Library. It contains many functions to control the LCD.
Let’s see one by one.
1. First step is to include the library using following command.
#include <LiquidCrystal.h>
2. Initialize the library with the numbers of the interface pins. This library uses only 6 IO pins and always used in 4-bit mode.
LiquidCrystal lcd(RS,E,D4,D5,D6,D7)
As per our connections command becomes
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
3. Set up the LCD’s number of columns and rows depending upon your LCD it is 16×2, 16×4, 20×4 etc.
lcd.begin(Col,Rows);
Our LCD is 16×2 so command becomes
lcd.begin(16, 2);
4. Clear display
Lcd.clear();
5. Move cursor to home position.
lcd.home();
6. Move cursor to LCD position.
This command is very useful when we are displaying any sensor value. We can quickly move the cursor to any position where we want to display text or value.
setCursor(Col, Row)
Row and col starts from zero, for line one first character it is
lcd.setCursor(0,0);
For line two coursor at first character it is
lcd.setCursor(0,1);
7. Display text on LCD.
This is most important command using this we actually display text on LCD, it is similar to Serial.print command.
lcd.print(“Hello World..”);
There are many other commands only above commands are most commonly used.
These are the cursor and display control commands. Listed below.
Cursor, noCursor, blink, noBlink, display, noDisplay, autoscroll, noAutoscroll, leftToRight, rightToLeft, scrollDisplayLeft, scrollDisplayRight, createChar
8. Custom character command
createChar(dataArray);
byte smiley[8] = { 0b00000, 0b00000, 0b01010, 0b00000, 0b00000, 0b10001, 0b01110, 0b00000 }; // create a new character lcd.createChar(1, smiley); //Prints custom character on LCD lcd.write(1);
Arduino Code for 16×2 LCD interface
/* LiquidCrystal Library - Hello World www.circuits4you.com Demonstrates the use a 16x2 LCD display. The LiquidCrystal library works with all LCD displays that are compatible with the Hitachi HD44780 driver. There are many of them out there, and you can usually tell them by the 16-pin interface. This sketch prints "Hello World! and circuits4you.com" to the LCD and shows smiley. The circuit: * LCD RS pin to digital pin 12 * LCD Enable pin to digital pin 11 * LCD D4 pin to digital pin 5 * LCD D5 pin to digital pin 4 * LCD D6 pin to digital pin 3 * LCD D7 pin to digital pin 2 * LCD R/W pin to ground */ // include the library code: #include <LiquidCrystal.h> // initialize the library with the numbers of the interface pins LiquidCrystal lcd(12, 11, 5, 4, 3, 2); byte smiley[8] = { 0b00000, 0b00000, 0b01010, 0b00000, 0b00000, 0b10001, 0b01110, 0b00000 }; void setup() { // set up the LCD's number of columns and rows: lcd.begin(16, 2); // create a new character lcd.createChar(1, smiley); // Print a message to the LCD. lcd.write(1); //Print custom character smiley lcd.print("hello, world!"); lcd.write(1); //Print custom character smiley } void loop() { // set the cursor to column 0, line 1 // (note: line 1 is the second row, since counting begins with 0): lcd.setCursor(0, 1); // print the message at line 2 lcd.print("circuits4you.com"); }
The above code displays “Hello World” and “circuits4you.com” on LCD. We have seen how to display text and custom character on LCD, also we have learned how to move cursor on LCD. You can try other commands to display cursor and text scrolling.