Controlling a Liquid Crystal Display (LCD) from an Arduino allows tons of possibilities for Projects like DIY CNC machines or 3-D Printers to give you local display readouts on a simple screen that can provide useful status feedback about your projects!
In this video we go in detail on how to both physically build and wire the project, and how to write the code to control the LCD screen!
Thanks for watching, don't forget to leave a like and subscribe to the channel, and comment what you want to see next!!
welcome back to the Master Tech and in
today's video I'm going to show you how
to control a liquid crystal display or
LCD screen using an Arduino
[Music]
so for this project I'm using a 16 pin
LCD screen my particular screen has 16
characters per row and two total rows
you can find these kinds of screens
online if you need one just by searching
16x2 LCD I'll include links to an
Arduino starter kit that's really great
for building all the projects we've done
the channel so far but I'll also leave
some itemized links if you're only
looking for one piece or part used in
this project you can find those in the
description below this video as well as
the code that you'll be seeing and the
wiring schematic for this build so in
addition to the LCD and the Arduino the
only other physical components you'll
need for this build are a 10 kilo ohm
resistor a 220 Ohm resistor a
potentiometer a push button and then of
course a lot of jumper wires you can
solder this if you want to make it a
permanent build but I'll be building it
on a breadboard so for this build start
by running 5 volt power and ground for
from your Arduino to one power rail of
your breadboard now jumper 5 volt power
to pin two of your LCD one side of the
potentiometer one side of the push
button and pin 15 of the LCD but going
through your 220 Ohm resistor now
connect ground to LCD pins 1 5 and 16.
and the second leg of the potentiometer
also connect ground to the other side of
your push button going through your 10
kilo ohm resistor now it's time to do
the Arduino digital pin connections and
while you don't have to use the same
pins that I use in this build it's
important to understand that when we get
to the code portion of this project next
the order does matter so if you don't
use the exact pins as me make sure you
write down what I used and then what
you'll be using instead so that when we
get to the code you're able to modify
accordingly I'm going to start by
connecting LCD pin 4 to Arduino digital
pin 2. then LCD pin 6 to our Arduino pin
3. now LCD pin 11 to Arduino pin 8 LCD
12 to Arduino 9 LCD 13 to Arduino 10 and
LCD 14 to Arduino 11. again that order
does actually matter for proper control
so make sure to either do it the way I
did or write down what you did instead
so when we get to the code you're ready
for it now I'm going to connect the
ground side of my push button to Arduino
pin 13. and lastly take the third pin of
your potentiometer and connect that to
LCD pin 3 which is going to allow us to
control the contrast of our LCD screen
and I know we went through that pretty
quick but again the wiring schematic
will be with the code in the GitHub Link
in the description of this video so if
you need that be sure to check that out
and of course you can let me know in the
comments if you have any questions about
what you see here now let's dive into
the code start by including the liquid
crystal.h package this is a pre-built
Arduino module that already knows how to
convert text into our six pin control
that we want to send to the screen next
Define a liquid Crystal item and pass it
the six pins that we used in our
physical build remember mine went in
order two three eight nine ten eleven so
if you used different pins than I did
you need to update this line accordingly
next Define what pin your switch is on
again mine is on 13 and then make
integer values for our current switch
State our previous switch State and then
the reply that we want to have displayed
currently now in the setup code which
runs once on initial project boot up so
when you power your machine on or first
upload the code to it tell the LCD to
begin and then tell it how many rows and
columns it's going to have next set the
switch pin up as an input and then use
your LCDs dot print command to set row
Zero's initial message then move your
lcd's cursor down to row 1 but column
zero so it goes column then row so 0 1
and then set whatever you want the
second rows message to be because
numbering starts from zero this is row
zero and Row one on a two row LCD screen
and that's it for the setup function now
we need to look at what happens in the
loop function which runs over and over
the whole time our Arduino is powered
let's start every Loop by just reading
the state of our switch pin then let's
put in an if statement to see if our
switch pin is different than the
previous state of the switch pin which
would tell us that it's either been
pressed in or released those are the
only two times that the switch pin is
going to change State then add another
if statement to check if switch state is
low this means that we'll only change
the state of the screen when the push
button gets released Now set up an if
else Checker to see what state of your
reply variable it's in and if you're
less than your maximum number of reply
eyes then just add one to the variable
but if you're at the upper limit and for
my project I'm going to do eight total
messages so if I get to the eighth
message and I press the button again I
want to send it back to zero now outside
of that if else statement but still
inside our when button gets pressed and
released code we're going to start by
clearing out the previous message on the
LCD with LCD clear then set the cursor
back to column 0 row zero and the way I
have it set up is that for all of my
messages I'm going to keep a constant
just a Master Tech in the top row so I'm
going to write this outside of the case
switching which we'll do next but if you
want both rows to change then you'll
just take these next three lines of code
and stick that inside the case switching
that we're about to cover but so I'm
going to write La Master Tech to that
top row and then I'm going to move my
cursor down to the second row and now
it's time to look at switching between
every independent message based on the
state of our reply variable we which we
control with that push button so in C
plus plus a switch case is basically
like a huge if else statement where you
just say ahead of time what variable
you're looking at for your if else's and
then it's just
um rather than needing to type in else
if this variable is equal to this value
it kind of knows okay in the case that
my reply variable is a zero write this
one write this two write this so in
every case zero through seven we're
going to say case our number and then
what we want to LCD dot print so
whatever line or message we want to say
for that number and then just add break
to the end to signify that that case is
done and you can go on to writing the
next case and then that's going to be it
for the two if statements so make sure
you have your closed parentheses closed
curly brackets after those and the last
line of code we need in this that needs
to run every single Loop is set the
previous switch State equal to whatever
the current switch state is this is how
we'll make sure that we only change once
per button press and we always keep
track of whether or not the button has
changed States then you can just close
out the loop section of your code you're
done with the programming and you're
ready to upload this to your Arduino if
all of the wiring was correct and your
code is correct then you should get
something that looks a lot like this so
when you boot it up your initial message
should be showing and as you press that
push button it should start cycling
through all of your cases the
potentiometer we added to this build
controls the contrast of the LCD screen
so try playing with it and you should be
able to dial in a setting that looks
pretty good for your application so
obviously there are tons of ways to use
this pretty basic build let me know in
the comments below what your idea for
how you want to incorporate an LCD into
your Arduino projects looks like and so
I hope this video was what you were
looking for I hope you found it useful
and I hope this channel has been
providing a lot of helpful resources as
you get into Arduino know and home
automation don't forget to leave a like
on the video subscribe to the channel if
you found it helpful consider checking
out the patreon link in the description
of this video as well a huge thank you
to my patreon supporters you guys make
this channel grow and I am so grateful
for you thank you everyone for watching
good luck with your projects I'll see
you next time bye