Hi, Welcome To Arduino Step by Step Course by Robojax
My name is Ahmad Shamshiri. I will be your teacher in this lecture.
in this lecture I’m going to show you how we can control the position of a servo motor
using a potentiometer and display the value on the lcd 1602 or this 2004 lcd screen
for the servo motor we need five volts for this lcd we need five volts and also for this
uh potentiometer I’m using the five volts for all of this we need separate connection so
one way would be to use a breadboard bring the five volts to one of these pin and then use it
so you will have extra wire to here and from here you will be connecting it
I’ve created additional five volts which I’ve talked to you before in the previous lectures
let me explain the code I’ve added the display code on top of the servo and potentiometer
so up to this point everything is exactly the same and below this I’ve added this this section wire.h
and liquidcrystal.h these two are needed for lcd screen this is part of Arduino this has to be
downloaded which I have explained it in a previous video that is explaining how to use the lcd screen
after that we are using the instance of that class creating this instance uh with this address
16 character is the length of the characters on the screen 2 is the number of lines
and then we define integer vcc3 because this was for potentiometer and we need another five volts
this is vcc3 and pen4 has been defined for that and degree pass this is this degree symbol
position variable that is needed later on so this is extra on top of the servo and potentiometer
and inside the setup this was exactly the same as servo and potentiometer but this has been added
this portion and I’ve used pin mode vcc3 as an output so we define ah set the pin as an
output and using digital right vcc 3 as high this vcc3 which was pin 4 now is set high so we have
extra 5 volts for lcd and then lcd dot begin just initializes the screen this turns the backlight on
this portion is exactly the same as server and potentiometer and I’ve added this portion which
I’m explaining this line clears the screen this line prints this text inside this double quotation
servo with part potentiometer and then we go to the next line character 0 of line 1 and we
print this text called angle and after this if we count this you can see that this is 0 1 2
3 4 5 and then we leave one space 6 and we go the character seven of line one
and the value that is the angle we printed here using lcd dot print but after this we make it
nicer by putting the degree symbol on the screen and because the degree can be displayed between
0 to 180 it can be single digit double digit 3 digits for that reason the degree symbol should
be closed to the digit and when it's one or two digits the space should be properly adjusted
so the degree is attached to the number if it's one digit the degree should be closer two digits
should it should be one more on the right so here we are taking care of that and we check the
value the value that you are printing if it is bigger than 99 which means if it's three digits
then we say the degree symbol position should be at position 10. else f otherwise if it's bigger
than nine which means the digit then we set the position nine so we subtract one so the
symbol moves to the left else means otherwise if this is false and this is false it means it's
single digits so it has to go to position eight one more less and then we set the cursor to the
degree position which is this one automatically adjusted and at line one and we use this
casting which converts a type of this to character so 2 2 3 will be converted
to character and will be printed so this is responsible for displaying the degree symbol
ground is connected to this ground for the lcd and vcc I have defined pin four
and used here as a vcc for this and as before sda is connected to a4 is cl is
connected to a5 and here I will also provide you with this schematic in case if you need it
and here is a demonstration of this code I am opening the serial monitor so you can see
the angle the angle is zero displayed on the serial monitor and in here
when you see the value fluctuates the reason is that the
value that we are reading from the potentiometer is fluctuating it goes
a little up and down and that causes this to be between the two values
and if you want to remove this fluctuation when you're reading the value in here
this value is one so if you divide this and reduce the resolution to five
just divide this by five also 1023 divided by five
then you will see that that fluctuation is reduced significantly
if you make it 10 or 6 or 7 it will be absolutely stable but you will lose the resolution