You are on page 1of 10

Experiment No: 04

Experiment Name: 16×2 LCD Interfacing with Arduino

Introduction:
In this tutorial, we will learn how to interface LCD with Arduino. We will be using Arduino Uno,
but the same code and concepts work for other Arduino development boards also. Firstly, 16×2
LCD interfacing with Arduino will be discussed. The first question that must come to our mind is
that why we need to interface LCD with Arduino? There are many applications in embedded
projects in which wants to display different types of data on LCD. For example, it is used to display
different sensors data such as temperature, humidity, pressure, light intensity, voltage, current and
many others. Hence, the use of liquid crystal displays is very popular and common in Arduino
project. It is also recommended to learn LCD interfacing at the start.

Liquid Crystal Display Introduction:


As its name suggests LCDs are electronics devices that are used to display texts, custom characters,
and numbers. Although, we can also display pictures on LCDs. But results will be not comparable
to graphical LCDs (GLCDs). GLCDs are used to display images. Coming back to LCDs, they are
available in different sizes and features. For example, 16×2, 20×2, 16×1 are different sizes
available.

16×2 LCD Introduction


There are two types of pins on the whole 16×2 LCD module. Some pins are used to send to 16×2
LCD and some are command pins. In other words, every pin has a role in controlling a single pixel
on the display.

16 x 2 LCD has sixteen columns and two rows. That means, it can display sixteen characters per
row, and it has two such rows. Similarly, 20×4 LCD has four rows and 20 columns. That means,
it can display 20 characters per row.
Pinout Diagram
The diagram shows the pin configuration of 16×2 display. It has sixteen pins.

D0 – D7: Pin number 7-14 are data bus lines that are used to send data from Arduino which you
want to display on LCD. With these 8 data lines, data can be transferred either in an 8-bit format
or in a 4-bit format. In a 4-bit format, only upper four bits (D4-D7) are used to send data from
Arduino to LCD. The full byte is transmitted in two successive transmissions. A 4-bit format is
used to save GPIO pins of Arduino. Because fewer GPIO pins of Arduino will be required to
transfer data.

Contrast Select (VEE): Pin3 will connect with power and ground through 3 pin
potentiometers. It will help to control the contrast of PIXELS according to the 16X2 LCD
light. 10K ohm variable resistor is connected with the VEE pin to adjust light contrast.
Variable resistor one side is connected with 5 volts and the other side is connected with
ground. The third terminal is connected with the VEE pin.
RS: This pin is known as a register select pin. It helps to toggle the command/data register.
R/W: The signal on Pin5 will decide whether it is going to read from LCD or write on it.
EN: Enable pin will help to transfer the instruction from the data pins and another command
pin to the LCD. It acts as permission to internal registers.
VSS: It’s a ground pin for common grounds.
VDD: The power pin will use for voltage input to the 16X2 LCD.
In Arduino, we don’t need to worry about controlling these data lines and control registers.
Because Arduino provides rich library resources. LCD library comes with Arduino IDE by default
when we install it.

16×2 LCD Interfacing Schematic Diagram


This picture shows the connection diagram of Arduino with 16×2 LCD.

If we are having to read connections according to above schematic, you can check this table for
connections between Arduino and 16×2 LCD.

16X2 LCD Arduino

D4 – D7 9, 10, 11, 12

E 7
16X2 LCD Arduino

RS 4

VEE POT (Middle Leg)

VSS Ground

VDD +5V

D+ +5V

D- Ground
More information on 16×2 LCD working and pin configuration available on this link:

• 16×2 LCD

Arduino 16×2 LCD Library Functions


In this section, we will discuss different LCD control routines that are available in Arduino
IDE. Most importantly, the same library function is used for all types of parallel LCDs
such as 16×1, 16×2, 20×1, 20×4, etc.

Firstly, we should include a header file. This header file contains prototypes and function
definitions that are used to control display.

Code:

#include <LiquidCrystal.h>
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
void setup()
{
lcd.begin(16, 2);
lcd.print("ABC");
}

void loop()
{
// put your main code here, to run repeatedly:

As expected, above code displays “ABC”. The lcd.print() function is used to send data. The most
important thing to consider in this code is the position of the displayed text. As we can see from
the figure, it starts printing text from the first location of LCD that is the first row and first column
such as (0,0). Basically, lcd.print() start to display text from the current cursor position of LCD.
Now let’s understand how to control and set cursor location.

LCD Cursor Setting


As you know that 16×2 LCD has 16 columns and two rows. One position can display only one
ASCII character. Also, one location consists of one row and one column. For example, (0,0) means
the first row and first column, (0,1) means the first row and second column, and similarly, (1,0)
means the second row and first column. This picture shows the position of each location according
to rows and columns of 16×2 LCD.
Arduino 16×2 LCD Interfacing Example
As shown in the above circuit diagram, this circuit will display “BILAL MALIK” in the first row
of LCD and “microcontrollers” in the second row of LCD. It is also shown in the above diagram,
pin number 9-12 of Arduino UNO R3 is connected with LCD. you just have to write these pin
numbers in LCD library functions arguments which tell Arduino that which pin of Arduino is
connected with which pin of LCD. The code to display the above text which is showing on LCD
is given below.

// It include Liquid crystal display library in your code


#include <LiquidCrystal.h>
// This function assigns Arduino microcontroller about connection of LCD with Arduino. Pins
should be connected in following manner :
// LiquidCrystal(RS, EN, D4, D5, D6, D7)
LiquidCrystal lcd(4, 7, 9, 10, 11, 12);

void setup()
{
// following function set up the LCD columns and rows:
lcd.begin(16, 2);
}

void loop() {
lcd.setCursor(0,0); // set the cursor position
lcd.print("BILAL MALIK"); //print the string on cursor position
lcd.setCursor(0,1);
lcd.print("Microcontrollers");
lcd.noDisplay(); // No display on LCD for 500ms
delay(500);
lcd.display();
delay(500);
}

Scrolling Text on 16×2 LCD using Arduino


In the last section, we have learned to display simple text on LCD using Arduino. Now let’s
move to some advanced examples. In this section, we will discuss examples of scrolling text on
LCD. That means moving text towards left and right direction.

Arduino LCD Text Scrolling Functions

Arduino LCD library supports two functions that are used to scroll text on LCD. These are
routines are:

• lcd.scrollDisplayLeft();
• lcd.scrollDisplayRight();
As their name suggests, lcd.scrollDisplayRight(); moves the text one cursor position towards
right from current text position and lcd.scrollDisplayLeft(); moves the text one cursor position
towards left.

For example, in this picture LCD shows text “Micro Lab” on the 6th column and 1st row
location.
Now if we call, lcd.scrollDisplayLeft(); function inside the code, it will move the text one
position left and output will look like this:

Schematic Diagram
Make connection with Arduino and 16×2 LCD according to this schematic diagram:
#include<LiquidCrystal.h>
LiquidCrystal lcd(12,11,5,4,3,2);
void setup() {
lcd.begin(16,2);
lcd.setCursor(5, 0);
lcd.print("Micro Lab");
}

void loop()
{
for(int i=0; i<5; i++)
{
lcd.scrollDisplayLeft();
delay(600);
}
for(int i=0; i<5; i++)
{
lcd.scrollDisplayRight();
delay(600);
}

}
How Code Works?
This code is quite similar to the last example except for the LCD scrolling part inside the loop()
function.

First, we set the cursor position to location (5,0) using cursor setting routine.

lcd.setCursor(5, 0);
After that, it prints the text “Micro Lab” on the current cursor position.

lcd.print("Micro Lab");
As you know that if we call scrolling text left or right function of Arduino, they will move the
text one one position towards left or right. Therefore, we used a loop to call these functions more
than one time.

First, we call lcd.scrollDisplayLeft() 5 times using for loop iteration 5 times. Therefore, it moves
the text towards left for 5 positions.

for(int i=0; i<5; i++)


{
lcd.scrollDisplayLeft();
delay(600);
}
After that, we call lcd.scrollDisplayRight() 5 times using for loop iteration 5 times. Therefore, it
moves the text towards the right for 5 positions.

for(int i=0; i<5; i++)


{
lcd.scrollDisplayRight();
delay(600);
}

You might also like