Hi! Welcome to an Arduino tutorial from Robojax my name is Ahmad Shamshiri.
in this tutorial I’m going to show you how to control two or more servo using single
potentiometer as you can see two servo is connected to Arduino uno when I rotate this
both of these servos rotate together and being controlled with the same potentiometer
you can get the code for this project by clicking the link below the video in the description
which will take you to robojax.com slash learn slash Arduino let's get started with this
I’ve seen a lot of requests for money robotics application that people need to control multiple
servers with a single potentiometer some applications in some area so I wrote the
code and tested it and I’m presenting it and we are connecting two servos to Arduino uno Mega,
whatever you have and the number of servo that you can connect depends on your
Arduino and here this on Arduino uno pen three which has this ~ tilde this means it is PWM pulses
modulation enabled. you can connect 5 6 9 10 11 the other pens such as pen 2 4 7 8 11 13 are not
possible modulation enable pin and they will not be able to control the servo then Arduino mega
from pin 2 to 13 these are all pulses modulation and 10 for this 4 45 and 46 three pins here these
are all passive modulation enable pin so you can connect more servos and if you're using Arduino
mini uno nano these are the pens that are pulsed modulation enable pen if you're using an Arduino
mega pen 2 to 13 and pin 44 to 46 are the pulses modulation pens servo motors are a type that
they can go from 0 to 180 and if you call this let's say 0 and if you call this position
0 then I can go 180 on the other side it goes 180 degrees where you can control it
using Arduino to send to be sent to any angle that you want because they each need power we need to
use breadboard which I’m going to show you in the wiring how to create extra pens so you can connect
multiple servos three four five or whatever number of pins that your board is allowing
and then we need to use a potentiometer this can be used this is a variable resistor
or people call it potentiometer as well that the value will change we are reading
the voltage and proportionally converting it to angle to speed to control the servers
Wiring explained
now let me show you the wiring because each server needs separate power 5 volts the servo
has three wires and I brought them here so you know the middle wire red color this is five volts
because it's at the middle you will never make mistake and the darker wire usually black or
brown is the ground and this is five volts and this is a signal that receives and controls
the servo to be sent to any angle two servo or three servo whatever number of servo you have
you have to create separate five volts for those and remember each servo consumes a little current
an Arduino can handle certain number perhaps these servos are very light power you can go up
to five of this with Arduino but more than that you need to supply separate five volts for this
Getting 5V and GND from Arduino for breadbaord
from here from this five volts I’ve put two wires
to this point and now because this is red all these lines from here
to head this is five volts and because the other one is ground and this is all ground
and here if you need more five volts I put here a jumper you can now connect three more servos
here positive negative positive negative positive negative and I put the jumper again this five volt
is now moved here so we have four more here positive and negative for five volts so the
same way you can create more connections if you want now for the servo number one
this red I’ve used the same color so I’m just inserting them here
now this receives power and if you call this servo number one
let's connect it to the first pen that I’ve used in this case in my case I use pin number five
which has this tilde and then for the other servo the same way get the positive the red on this side
and brown on this side so I’ve connected now the next one and then the orange which is a signal
connected to pin six or whatever num pen you have so that's my one servo and that is the other
servo and for the potentiometer you can use any potentiometer between 10 kilo ohm to 100 kilo ohms
it's not that critical even 500 km will work fine now it has three pens the right is the maximum
when you rotate it goes to the maximum so connect the right one to five volts which I have here
and then the left one the left pen connected to the ground because we have here ground
do not make mistake the middle cannot be connected to ground or to the positive
because when you rotate it will create short circuit a separate video explaining that
the link for that video is below the video in the description and the middle wire goes to analog
zero I have defined it in the in the code and if you wish you can define some other analog pins
Code explained (sorry for the incorrect title)
now let me explain the code if you click on file example and then scroll down all the way until
alphabetically you reach the servo and click on knob this is the sketch that I’ve started
with and I’ve modified it line it says include the servo header file this is a class it's part
of Arduino id when you have this program you already have this and here we get that class
uh name and we create an object called servo one and then we also create servo two if you want
servo three and four just continue underneath and put two or three or four whatever you have
and then pot pin or potentiometer pen this is analog zero you can put zero or you can put
analog zero if you want that's the analog pin that is used and then we are defining the pens for the
Arduino to be attached to the servos so I’m connecting in my example the servos to pin
five and six and Arduino mini uno nano these are the pens that are pulsed modulation enable
pen if you're using an Arduino makeup and two to 13 and pin 44 to 46 are the pulse and modulation
pens so just put the number and put comma and this will be your servo 1 this will be salvo 2 and if
you have servo 3 let's say if it is connected to pin 9 just put it like that and continue
this variable is used to hold the value for the potentiometer when you rotate it do not touch
it and here inside the setup from here to here this is a setup function which runs only once
we initialize the serial monitor with 9600 bot and this is a communication
baud rate if you click on tools and serial monitor or press ctrl shift and m or on the
right side if you click on this icon you will see the serial monitor with which
prints the text at the moment it is zero let me rotate the potentiometer so you can see it
and if I expand it on the right side and the lower section you see this number 9600 this number must
match otherwise you will not be able to see that text and this is just printing that text at the
beginning of uh starting Arduino another line also prints this text and then in this line we attach
pen we say servo one attach and then servoPin zero this is an array when we say servoPin
and then zero and then the next one for servo 2 we say 1 this 0 means go to this array and pick the
item number 0 this is 1 and then if you have comma something else it will be 2 3 4. so you
would use the number here starting with zero and go up and then the first one second third
fourth and so forth so these are attaching the servo to those pens otherwise this will not work
inside the loop the code that is inside the loop will run continuously non-stop unless you stop it
inside the code so we first using analog read path pen which we defined at the top analog 0
we read the value which is from 0 to 1023 it is actually 2 to the power 10 this 2 to the power
10 which become 1024 minus 1 because we start with zero so we get that value and we say map the value
which is from 0 to 1023 and map it between 0 to 1080 which means do the calculation if the
value is 0 the calculated value will be 0 and if it is 1023 make it 180 and this is the angle
for our servo and the result of this calculation will be stored back to this variable which means
the val will be updated here and then here we say servo one dot write which means write
this value to this server one so now server one will move and without any delay we send also the
same value to server two so if you have more servos just press enter and then put the same
line and copy it and make number three and four and five whatever you have this line just prints
the value for us when I open the serial monitor that number is this number when I now rotate this
servo the servo rotates and also the value changes with potentiometer
and then parent ln creates a new line so it goes to the next line and prints the value again for us
in between the loop we give some time 15 millisecond if you do not have the delay
the servo will not be able to move because it's a mechanical device and it needs time to move
from one location to the next angle location so you have to have a time here that is the minimum
but you can increase it for example you can make it hundred two hundred or even one second
when you rotate the potentiometer it waits that amount of time before the change is applied
Demonstration
and let me demonstrate it the servers are powered up and I’ve also included the serial monitor
so you can see the values here now it's at angle 0 and when I rotate this
as you can see it shows the angle 34 or 33
so 135 and this is almost this is 180.
if you see the fluctuation that's because of the potentiometer it it's not fixed so
that's why this number goes up and down because we are mapping it I’ve explained it in the code
When you rotate the potentiometer to clockwise CW, the servo rotates to counterclockwise CCW.
To change the direction or rotation of servo, switch the 5V and GND wires of potnetiometer.
and I will put it at the middle but
let me just reboot by pressing this push button and see what happens
when Arduino reboots whatever position this potentiometer is it will go at that so if I
put it and put it at a maximum as soon as it boots it goes to that angle 180
and when I reboot it before it boots if I put it at zero as soon as it boots it goes to the zero
so the initial position depends on where you put the variable resistor or this potentiometer
thank you for watching a video tutorial from Robojax if you learned something and found
this video useful please thumb up 👍 as this will help my video in the search algorithm of YouTube .
if you have comment or question please post it in the comment section below
I try to answer and reply and also make sure to subscribe so you get updates of my upcoming videos
I would like to thank my patrons from Patreon.com
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 need parts, please purchase them from the link below, it will help m.
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
so when I press this is a single button when I press it on here it prints the angle
the servo moves I can move it and it also shows you the exact location of the servo
I’ve used I’ve used