Welcome to Arduino Step by Step Course by Robojax
I Ahmad Shamshiri will be your teacher in this course.
we're going to see how we can control the position of a servo motor something like this
using a variable resistor or potentiometer we already know how to measure the voltage of this
uh variable resistor potentiometer using analog input and also we have seen how can we set the
servo at any angle between 0 to 180 degrees now we are going to control the servo using
this uh potentiometer now because we are using two devices this is this needs five volts and I
want to connect this to five volts because I have only one five volts in my Arduino I have to define
a pin as an additional five volts and if you're using this type of Arduino you don't have to do it
you can just connect it to the second five volts this is one and that's the second one
here is the schematic diagram for the servo wet potentiometer the servo has a positive which has
been connected to five volts the negative is connected to the ground the pulse is connected
to pin three and this is our variable resistor from one side it's connected to five volts this
d2 have been defined as five volts in our program the other side of this is connected to the ground
the middle one which is changing we are reading it at analog zero and here is the wiring diagram
from the server the signal pin is connected to pin three because this has a pulse width modulation
the positive is connected to five volts the middle one the last one is connected to the ground
from potentiometer the left side pin is connected to the ground
the middle one which is the value that we are reading is connected to analog zero and the
right pin is connected to pin two this is a five volts or vcc that we have defined in our code
let me show you the wiring the servo has the three wires that I have shown you before
the left wire is the ground have been connected to the ground of Arduino the middle wire is red
connected to five volts and the last wire is orange here it has been connected to pantry
this is my potentiometer the left pin because I want this one when I move it to the left
I want it to be zero so this is connected to ground and the right pin has been connected
to pin 8 which acts as our vcc2 or extra 5 volts and the middle pin the red one
have been connected to analog0 where we read the value of this
let me explain the code as before this is part of the library we are defining an instance of
this class and this is an object my servo you can change it to some other name if you wish
and after this we define a part pin or potentiometer pin analog 0 so instead of
a 0 we will be using this in our code and then we define servo pin pen 3 which the signal pin
of the servo will be connected and I’m creating vcc extra 5 volts at pin 8 for potentiometer
this Val is a value of reading potentiometer and this will be updated
inside the code inside the setup we define the serial monitor with 9600 bot
and to display the text on the screen for us and then we use this object myserver.attach
to attach the servo pin which if we define it as pantry the servo is being controlled using
pulse width modulation at pin 3 and this is our vcc we say vcc2 as an output so pin
8 is now act as an output it's defined here and then we go by writing digital right vcc high
this means the pin 8 will be always high and high means 5 volts so we have extra 5 volts in here
inside the loop using analog read part pin this is the potentiometer pin we are reading analog 0 pin
the value will be stored in Val this is our variable which we define above and then we now
have to translate the value that you are reading between 0 to 1023 to an angle and this is a
function internal function of Arduino it's called map so it takes five arguments for the first
argument is the value that you want to change and the value is this value which we are reading from
potentiometer you can enter also manually a number between 0 to 1023 so you get the value and you say
the value you have to also mention it what is the minimum and max error minimum is zero
maximum is 1023 and you want to map it to another value between 0 to 180 and which is our angle
what it does internally is when it's 0 it gives you 0 angle when it's 1023 it will give you 180
and the other value in between any value between 0 and 1023 will be between 0 to 180 proportionally
the result of this calculation will be stored in the same Val now the valve will be replaced so
we don't have to define another variable and then we use this function called write my servo.write
and then we pass this value inside this so whatever is the angle it says 30 40 degrees will
be here and the servo will move to that location and then we use serial print to print the value
for us and we use serial parent ln to create a new line is just enter extra line for us and then
we wait for 200 milliseconds the loop will continue reading it every 200 milliseconds
here is a demonstration of the code the same code that I just shown you
now it shows that my potentiometer is at the minimum and the angle also this is printing is
zero and also we re we see that the server is also at zero and we can define our zero whichever we
want if I rotate this to the right you will see that the servo is moving proportionally
and this is my maximum value and if you see here the maximum angle is 179
it shows for us so maybe one degree is left
and you see when it when it clicks that delay is because of this 200 millisecond
you see when I move it
jumps slowly so if you want this one to be very fast make it 50.
now I’ve changed this to 50 and it's reading
every 50 millisecond now the response will be very faster as you can see we get very smooth movement