Welcome. My name is Ahmad Shamshiri and I am presenting this tutorial from Canada
Hi, welcome to an Arduino tutorial from Robojax. in this video I'm going to show you how we can
control a servo motor using a potentiometer and Arduino .as you can see here on this side
it shows the value the first value is a value for potentiometer the second is the
angle of the servo so when I rotate it to the right as you can see it's reading 149 and the
angle is 26 degrees so when I reach to the full maximum to the right the angle is 180
and we are reading a hundred and eighty so it moves between 0 and 180. you can get the
code for this project by clicking either language the description below will take
you to www. robojax.com/learn/arduino let's get started with this.
Wiring explained. If you need wiring diagram, you can purchase my Arduino course at Udemy
explaining now the wiring. this is a 10 kilo ohm (you can use up to 500 k ohm) potentiometer and the potentiometer has three
pins here one two and three. so the middle one is the variable value the two side you
will connect it to five volts and to the ground GND. doesn't matter which way but the way that you
connect this to five and zero will determine the rotation direction. for example right now
where if I rotated clockwise the value increases and decreases but if you swap the connections it
will be reversed but doesn't matter but make sure that the middle one from here goes to an analogue
zero A0. that's the middle one these two other wires so I'm removing this so these two other wires as
you can see it here the left one the black one is connected to five volts and the red is connected
to the ground so you can swap those to change the direction of rotation of this. the servo has
three wires here and the darker wire is always the ground GND so from here we have a ground here
connected to the ground pin and then the middle one is five volts so this has been connected to
the five volts here. this sends the signal to control the servo and this is connected to
pin 9. so the signal comes from here these two pins are for powering the server and the
server you can get different type of servo different size of the servo this is plastic
gear you can get metal gear and also heavier bigger but this piece can be just removed and
you can put it so you can control with this servo some lids, door fly airplane and in
robotic cars you can use it as steering to steer the tires that you are car to the left and right.
Arduino Code Explained
let me explain now the code. to get to open the code in your Arduino IDE. just click File->Examples and
then under the Servo click knob. and this is the file that you're getting I've just updated with
a little extra stuff that make it work. and also it prints some information. here that's extra so
let me explain that. this line shows including the servo.h that's a header file you don't need
to download and install it from anywhere that's included with your Arduino IDE. and then after that
we are initializing an object called it myservo with a type server and then analog 0
A0 is the pin that is reading the potentiometer and then integer val, is the internal value that
needs for the calculation. inside the setup() from here to here we are just initializing with 9600
baud the (serial monitor) screen and that 9600 is this value on this side. it must match otherwise you will not be able
to read this value. and then that object that has been created servo.attached() will attach the
pin 9 of digital so we are using the pin 9 as I showed you would be connected to servo. and side
the loop() from here to here potpin, which is potentiometer pin, this is a pin is A0 so we are
reading in A0 it says analogRead and the value will be stored in the "Val" variable so now this will
have the value which will be from 0 to 1023 (2 to the power 10) so we have 1024 steps. and then this is printing it here
as you can read here hundred seventy-three. and this line is printing a little space that space
between this value. and then we get that value the map job of the map is to get the value the
this value which is from between 0 to 1023 and then map it or calculated or converted between 0
and 180 so 0 will be 0, 1023 which is a maximum value will be 180. and the other values are in
between proportionally and the calculation will be done here. and the result will be stored in the
Val here again so that value enough changes. and then here myServo.write() will write
this value we put the value here if you put 50 so this will write the 50 if you write 30
whatever what we are making it automated so the value comes here and this will move the
servo to that right angle . for example currently outside the 30 degrees. and then we will print
the value of 30 that is executed by servo. we also printing it. and job of this line is to print new
line. LN, that's new line. and then we wait 15 millisecond and then the loop continues like
that. that's why you see the screen is going very fast. but if you want to slow it down so
you can make it 200 millisecond and you can see now it goes 1/5 of a second. so now it's already
moving except this barbed scrollbar you will not notice because the values are not changing but
if I make some changes here you will read you will see the values. and so this was the code.
Demonstration: Controlling Servo
here now so that's the maximum as you can see here the shoe 180. if here we
are we are seeing the delay here it jumps even though if I go fast you
see some non smoothness but if you want to make it smooth just in decrease this value
make this fifty or ten or whatever some smaller value that will be responding faster. thank you
for watching so that's what how you use the servo with a potentiometer to control it please make
sure to subscribe 👍 so you can get updates of my upcoming videos and also if you have comment
or question please post it at the description below I'll try to answer and reply. take care
I did my part to help you learn. Now it is your turn.
You can subscribe to my channel, thumb up 👍 the video and type a comment by saying thank you.
If you don't skip ads it will also help you .
My PayPal Account link is in the description if you want to make cash donation. Thank you