welcome to Arduino Step by Step by Robojax
in this lecture we are going to learn how you can control two or more servo motors
using two potentiometers with Arduino you will have independent
servo controller for let's say for servo one or servo two together or one of them
get the code from www.Robojax.com
My name is Ahmad Shamshiri
I’ve seen comments under the video asking to control
more than one servo motors so here I’m going to show you how to control
two or up to six servo motors with Arduino uno of course with Arduino mega you can do more so
this is just simple example and you can apply the same principle that I will show you in this video
you can control as much as the analog input of your Arduino will allow you will be able to do it
to quickly explain the servo motor servo motor is a type of motor that does not do 360 rotation
in this case this can go up to 180 degrees and
and it is sold to a different kind of this attachment that you can control airplane wing and
many other applications that you want to move something you can do it
with servo motor and the silver motor has three wire this is always the case
that the darker color is something you will see it black will be the ground
and then the positive is at the middle because uh in many applications and flight controller
and stuff you will insert it and this middle is always positive so even if you make mistake
your servo will be protected in the lighter color this is orange and some applications you will see
this will be white this is the signal and this so these two are the power and the orange is a signal
to control this and the signal that it receives to control this is pulses modulation signal
and duty cycle of that signal will make this go either 180 degrees or zero
and here is the wiring diagram for controlling two servo motors using
two potentiometer I have potentiometer or variable resistor number one for this
servo motor number one and for this servo motor number two I have potentiometer number two because
this potentiometer needs five volts on ground and this one as well and these servo motors I
have connected from this five volts to this point at the breadboard this is a breadboard this side
is the red line I’ve connected this five volts here so I have five volts available everywhere and
then from ground I put another wire to this point on the side that has a blue line so this is all
my ground now for this potentiometer the right pin is connected to five volts the left pin
is connected to the ground the same for this one to five and the left is connected to the ground
the middle pin of the potentiometer number one is connected to analog zero
so it can be read and the server number one will be controlled and the middle pin of the
second potentiometer is connected to analog one which is controlling the server number two
the both servos has a black wire which is connected to the ground this is also connected
to the ground the middle wire is red five volts it has been connected to this five volts and the red
wire of this one also connected to five volts this one has orange and orange orange is the
signal for servo number one it is connected to pin three which is a pulse modulation enable pin and
for the servo number two it is connected to number five which is also process modulation enabled pin
let me explain now the wiring because I need multiple five volts I have to use a breadboard
and for ground as well from ground I’ve taken one wire I’ve connected here this blue is now my
ground available I have multiple grounds here and from five volts
also I’ve taken this orange wire connected in this side which is red
with a plus sign so these are all my five volts this will be connected to ground in here
anywhere on this and the middle one is five volts I will connect it to five volts here and then this
is a signal this will go to this pin three pin three is password modulation enable pin you see
that tilde and five is also possible modulation six you cannot connect it to any other pins
now for the potentiometer for this because we want on the right side to be the maximum on the left to
be the minimum 0 and this is 180 degrees because the right is maximum put this to the positive
so this is my five volts I’m connecting it somewhere and five volts and the
left side I will connect it to the ground somewhere here at the ground on this ground
and the middle pin must not be connected to 0 or 5 volts it must not be otherwise
you will make short circuit when you rotate it and this is connected to analog zero
servo one is done same way for servo two I have three wires the dark one is connected to ground
somewhere here the red is five volts I will connect it to five volts
and the orange goes to the next positive modulation which is pin five and I’ve defined it
in code also in pin five I can put it in any other pins with the pulses modulation enabled symbol
this is potentiometer for the servo 2 the positive the right one is connected to 5 volts
the left one is connected to the ground and the middle pin will be connected to analog one
at this point
let me explain the code this is the code that I have opened it by clicking at file examples
servo and then knob after that I’ve updated it I’ve left the original
credit to the creator I’ve just updated it I’ve modified it so it can work with this
purpose and the servo.h is part of Arduino ide you don't need to download or include
anything this line include means the servo header or servo class will be included
and this portion is for server one we're getting the servo class and we create an instance of it
and we call it server one and then we assign a pin as a constant integer we call it servo one
part pin or potentiometer pin this is analog 0 and then another constant which cannot be changed is
integer servo pin this is the pin that sends signal the servo pin will be perfect modulation
enable pin like pin 3 5 9 and other pins which I have just shown you otherwise it will not work and
then the servo one value is the value that when you rotate the knob the value will be assigned
later on in the code so this was for servo one and we do the same exact thing here with
this time we create an instance of the server we call it server2 and then I just change the one to
two here and assign another pin this was analog zero this this is analog one again serve open two
server two pin is pin five it's also possible modulation enable pin and then the value
one has been changed to two inside the setup we initialize or attach a pin to servo one using
servo one dot attach servo one pin and then servo two there is also two so if you want to create
three four or as long as you have analog input then just copy and paste it and change this one
to two and for example for three just change this three three three and assign for example
unlock two and another pin here and also in here the same way servo two attach servo to pin and
then if you have three just copy and paste it and change it to three and three and so forth
this portion is to control the servo one first we use analog read we read the analog servo one
potentiometer pin the value which is from zero to one thousand twenty three will be stored here
and this variable we call it server servo one value which you have defined above and then using
the map because this is from 0 to 1023 we get this value here we map it which means we convert
or change it the value for this which is from 0 to 1023 we convert it to angle between 0 and 1180
because the servo our servo can go from 0 to 180 and when the potentiometer value is 0 the angle
will be 0 and if it's 1023 it will be 180 degrees and the other values are in between these two
so this is doing the calculation in terms of angle something between 0 to 180 will be stored here
now we get we use servo1 dot write and then we pass the servo 1 value here and the value will
go to the server 1 and servo will move to that angle which we have set using potentiometer
and the same code is copied here for servo two if you pay attention all the values
are two and if you have three just copy and paste it and make one more line here
for three this 15 millisecond is the time that allows the servo to move to the desired location
if it is too slow you can increase the speed by reducing this value or
you can increase it to reduce the speed of movement of servo and the loop will continuously
read the potentiometer and move the servo to the new location
and now let me demonstrate it when you initially start the Arduino the
position of this depends on where you have set your potentiometer
and they move very smoothly sometimes when you move this you will see some vibration here but
this will not move I don't know if you notice a little more movement will be seen and that is
due to fluctuation in the power supply or internally from Arduino but
the same way this one will affect very minor
but it will not move it just shakes and goes back to the location
and if you change this to 5 from 15 let's see what happens now it has been updated to 5.
now the vibration has stopped
other than that it perfectly responds
that's now responding perfectly and for that if you need uh server number three or
four or five just add more potentiometer and your service where you can connect up to six
servos here because we have six inputs six uh pulses modulation enabled pens
thank you for watching this was how to control two or more servo motors using Arduino