 
Hello, Dejan Nedelkovski here from HowToMechatronics.com
In this tutorial we will learn how to
connect the
Liquid Crystal Display or LCD to the
Arduino board. LCD like these are very popular
and broadly used in electronic projects
as they're good for displaying
information
like a sensors data from your
project
and also there are very cheap. We can
start this tutorial with the
pin-out of the LCD. It has 16
pins and the first one from left to
right is the
Ground pin. The second pin is the
VCC which we connect to the 5 Volts
pin on the Arduino board. Next is
the
Vo pin on which we can attach a
potentiometer
for controlling the contrast the display
Next comes the RS
pin or Register Select pin which is
used for
selecting whether we will send commands
or data to the LCD. For example
If the RS pin is set on a
low state or 0 volts then we are sending
commands to the
LCD like: set the cursor to a specific
location,
clear the display, turn of the display
and so on.
And if the RS pin is set on a high
state
or 5 volts then we are sending
data or characters to the LCD. Next comes
the
R/W pin which
selects the mode whether we will read
or write to the LCD. Here the write 
mode is
obvious and it is used for writing or sending commands
and data to the LCD. The read mode
is used by the LCD itself when executing
the program
which we don't have a need to discuss
about it in
this tutorial. Next is the
E pin which enables the writing to the
registers or the next 8
data pins from D0 to D7
So through this pins we are sending the
8 bits data when we are writing to the
registers or for example if we want to
see
the latter uppercase A on the
display
We will send 0100
0001 to the registers
according to the ASCII table. And
the last two pins A and K or
anode and cathode are for the LED
back light. Okay so after all we don't have
to worry much about how the
LCD works as the Liquid Crystal Library
takes care for almost everything. From
the
Arduino official website you can find
and see the functions of the library
which enable easy use of the LCD.
We can use the library in a
4 or 8-bit mode. In this tutorial we
will use it in
4-bit mode or we will just use 4 of
the 8 data pins. Before we write the
code
Let's see the circuit schematic first. I
will use an
Arduino Mega Board, 1K Potentiometer and the
LCD. So we will use just
six digital input pins from the
Arduino Board. The LCD's
registers from
D4 to D7 will be connected
to the Arduino's digital pins from
4 to 7. The Enable pin
will be connected to pin number 2
and the RS pin will be connected to
pin number 1. The RW pin
will be connected to Ground, and the Vo
pin will be connected to the 
potentiometer.
Okay so now we can write some code for
the LCD
First thing we need to do is to
include the Liquid Crystal Library
we do it from here: Sketch>Include Library
>Liquid Crystal
Okay then we will create
a Liquid Crystal object.
 
like this. I will name it LCD
and as a parameter we will set the
pins number on the Arduino
respectively to the circuit schematic. So
the first parameter
is for the RS pin and as we connected
to the digital be number 1
I will write here 1. The second
parameter is the
Enable pin and
that one we connected to the digital pin
number 2 on the Arduino board
And the next
 
the next parameters are for the
registers.
So first is for the register D4
and that's connected to pin number 4.
Then the D5,
D6 and D7
so just like this. Okay
Next is the
setup where we will initialize the
LCD and set dimensions
So we do it like this
lcd.begin() and set the
dimensions of the LCD. 16 by 2.
Okey
And now it the loop we will
write our main program.
First let's print something
on the LCD using the print function
like this: lcd.print()
 
I will print "Arduino"
and make a  delay of 3 seconds.
So that we can notice it.
Okay now using the
setCursor() function we can change
the location on the display where the
next text
will be written. So
lcd.setCursor()
And the first parameter is for the
column
and the second is for row. So I will
change the
cursor to the
second column and
first row. So this actually will be
the second row as this number
stars from
0. So 0,1 that will be the
second row.
and I will write something again
using the print function
I will write LCD tutorial.
And I will set
3 seconds delay again.
 
 
Okay after these 2 texts, we will clear
the display
using the clear() function.
lcd.clear()
Okay.
now using the blink() function we can set
a
blinking cursor like this:
lcd.blink()
 
I'll add some delays
so we can notice it
I will
change again the location of the cursor
So we can see that
blinking cursor
on a different location
like this and again some delay.
3 secods.
OK we can also turn off this blinking
cursor using the noBlink()
function. This will turn on the blinking
cursor
and we can also set the different
cursor
like underscore cursor using the lcd.
cursor() function.
I will add some delays so we notice it
and using the noCursor() function we can
turn off the underscore cursor as well.
Okay and now I will clear once again the
display using the clear() function.
And that will be our main program
which will
repeats all the time. And now let's see
how it works. So here's the
first text and after some delay 
the second text on a different location
Here's the blinking cursor and again
after some delay the blinking cursor
on a different location and the
underscore cursor.
It is also possible to write custom
character
to the LCD. It supports up to
8 characters of 5x8 pixels.
We specified the appearance of each
custom character
by an array of 8 bytes.
Okay, so for that we have to define an
array of bytes like this
I will name it "slash" as our
custom character will look
like slash.
So here, this B stands for a binary
formatter
and the five numbers are actually
the pixels. The five pixels
and we need 7 more of these
So toal 8, so that we get that format
of
5x8 pixels.
Okay and now what we will change
here from 0 to 1 that will create our
custom
character. So
I will change it like this
and that will look as a slash.
Now that one we have to
initialize or
create that custom character in the setup
using the createChar() function like
this.
And here we need two parameters
The first parameter is a
number from 0 to 7
or we have to reserve one of the 8
support that characters
so here I will just type 7 and as a
second parameter
it goes the name of the
array of bytes we defined above. So
that will be "slash". And in the loop
will write our main program.
I'll make it look more interesting and
make that character moves
along the LCD. So we will use a for loop
for changing the location the cursor.
So it will look like this.
 
 
 
Okey, that's the for loop and now using the
setCursor() function like this:
lcd.setCursor()
And
here in the setCursor() function we
will use the
"i" variable from the loop as a column
parameter.
 
And now we have to write that character
using the
lcd.write()
We will write the character we defined
above. So these two numbers
have to be the same. I'll make
1 second delay
and
clear
the display after each
iteration of the for loop.
Okay, that's our program let's
see how it works.
Here's our
custom character moving along the
column so the LCD.
 
Thanks for watching and for more
Tutorials
visit my officialwebsite HowToMechatronics.com
