Interfacing LCD With Arduino
Interfacing LCD 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.
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.
If we are having to read connections according to above schematic, you can check this table for
connections between Arduino and 16×2 LCD.
D4 – D7 9, 10, 11, 12
E 7
16X2 LCD Arduino
RS 4
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
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.
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);
}
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.