You are on page 1of 6

Servo Motors Control & Arduino

Unlike dc motors, with servo motors you can position the motor shaft at a specic
position (angle) using control signal. The motor shaft will hold at this position as
long as the control signal not changed. This is very useful for controlling robot
arms, unmanned airplanes control surface or any object that you want it to move
at certain angle and stay at its new position.
Servo motors may be classied according to size or torque that it can withstand
into mini, standard and giant servos. Usually mini and standard size servo motors
can be powered by Arduino directly with no need to external power supply or
driver.
Usually servo motors comes with arms (metals or plastic) that is connected to the
object required to move (see gure below to the right).
How Does the Servo Motor Works?
Your servo have 3 wires:
Black wire: GND (ground) ! RED wire:+5v ! Colored wire: control signal
Future Electronics Egypt Ltd. (Arduino Egypt).
The third pin accept the control signal which is a pulse-width modulation (PWM)
signal. It can be easily produced by all micro- controllers and Arduino board.
This accepts the signal from your controller that tells it what angle to turn to. The
control signal is fairly simple compared to that of a stepper motor. It is just a
pulse of varying lengths. The length of the pulse corresponds to the angle the
motor turns to.
For detailed and simplied explanation of Pulse Width Modulation (PWM),
Please click here.
The pulse width sent to servo ranges as follows:
Minimum: 1 millisecond ---> Corresponds to 0 rotation angle.
Maximum: 2 millisecond ---> Corresponds to 180 rotation angle.
Any length of pulse in between will rotate the servo shaft to its corresponding
angle. For example, 1.5 ms pulse corresponds to rotation angle of 90 degree.
This is will explained in gure below.
The piimaiy chaiacteiistic in selecting a motoi is torquc. Toigue ueteimines how much
woik the motoi can uo. Typically, highei toigue motois aie laigei anu heaviei anu
uiaw moie cuiient than lowei toigue motois.
Biushless motois usually aie moie poweilul anu ellicient loi a given size than Liusheu
motois, Lut they ieguiie moie complicateu electionic contiol. Vheie the peiloimance
Lenelit ol a Liushless motoi is uesiieu, components calleu c|cctronics spccd contro|-
|crs intenueu loi hoLLy iauio contiol use can Le easily contiolleu Ly Aiuuino Lecause
they aie contiolleu much like a seivo motoi.
Stepper Motors
Steppeis aie motois that iotate a specilic numLei ol uegiees in iesponse to contiol
pulses. The numLei ol uegiees in each step is motoi-uepenuent, ianging liom one oi
two uegiees pei step to 30 uegiees oi moie.
Two types ol steppeis aie commonly useu with Aiuuino: Lipolai (typically with loui
leaus attacheu to two coils) anu unipolai (live oi six leaus attacheu to two coils). The
auuitional wiies in a unipolai steppei aie inteinally connecteu to the centei ol the coils
(in the live-leau veision, each coil has a centei tap anu Loth centei taps aie connecteu
togethei). The iecipes coveiing Lipolai anu unipolai steppeis have uiagiams illustiating
these connections.
Iigurc 8-2. Rc|ationship bctwccn thc pu|sc width and thc scrvo ang|c, thc scrvo output arn novcs
proportiona||y as thc pu|sc width incrcascs jron 1 ns to 2 ns
8.0 Introduction | 263
Future Electronics Egypt Ltd. (Arduino Egypt).
Inside the Servo Motor
Did ever wonder how the servo motors looks from inside?. Have a look at the
corresponding picture. A servo motor was taken apart to show the internal parts.
You can see a regular dc motor connected to a gear box and a potentiometer that
give the feed back for angle position.
This is represented by the diagram below.
Future Electronics Egypt Ltd. (Arduino Egypt).
Motor Control Using Arduino
Standard servo motor control using Arduino is extremely easy. This is
because the Arduino software comes with a sample servo sketch and servo
library that will get you up and running quickly
1. Connect the black wire from the servo to the Gnd pin on the Arduino
2. Connect the red wire from the servo to the +5V pin on the Arduino
3. Connect the third wire (usually orange or yellow) from the servo to a digital
pin on the Arduino
Future Electronics Egypt Ltd. (Arduino Egypt).
Discussion
This example sweeps the seivo Letween 0 anu 1S0 uegiees. You may neeu to tell the
liLiaiy to aujust the minimum anu maximum positions so that you get the iange ol
movement you want. Calling Servo.attach with optional aiguments loi minimum anu
maximum positions will aujust the movement:
myservo.attach(9,1000,2000 ); // use pin 9, set min to 1000us, max to 2000us
Because typical seivos iesponu to pulses measuieu in micioseconus anu not uegiees,
the aiguments lollowing the pin numLei inloim the Seivo liLiaiy how many micio-
seconus to use when 0 uegiees oi 1S0 uegiees aie ieguesteu. Not all seivos will move
ovei a lull 1S0-uegiee iange, so you may neeu to expeiiment with youis to get the iange
you want.
The paiameteis loi servo.attach(pin, min, max) aie the lollowing:
pin
The pin numLei that the seivo is attacheu to (must Le 9 oi 10)
min (optiona|)
The pulse wiuth, in micioseconus, coiiesponuing to the minimum (0-uegiee) angle
on the seivo (uelaults to 5++)
max (optiona|)
The pulse wiuth, in micioseconus, coiiesponuing to the maximum (1S0-uegiee)
angle on the seivo (uelaults to 2,+00)
Powei ieguiiements vaiy uepenuing on the seivo anu how much toigue is neeueu to
iotate the shalt.
Iigurc 8-3. Connccting a scrvo jor tcsting with thc cxanp|c Swccp s|ctch
8.1 Controlling the Position of a Servo | 265
Important Notes:
1- It is not a good idea to connect a motor of any kind directly to the Arduino
because it usually requires more power than the board can provide.
2- In our example, the servo is being used to demonstrate code and is not
encountering any resistance. Note that you should use a standard or small size
if you are uncertain, check the servo's no load current rating (it should usually be
under 150mA).
3-You may need an external source of 5 or 6 volts when connecting multiple
servos. Four AA cells work well if you want to use battery power. Remember that
you must connect the ground of the external power source to Arduino ground.
Here is the example Sweep sketch distributed with Arduino;:
#include <Servo.h>
Servo myservo; // create servo object to control a servo
int angle = 0; // variable to store the servo position
void setup() {
264
| Chapter 8: Physical Output
}
myservo.attach(9); // attaches the servo on pin 10 to the servo object
void loop() {
}
for(angle = 0; angle < 180; angle += 1) // goes from 0 degrees to 180 degrees
{
myservo.write(angle); delay(20);
// in steps of 1 degree // tell servo to go to position in variable 'angle' // waits 20ms
between servo commands
} for(angle = 180; angle >= 1; angle -= 1) // goes from 180 degrees to 0 degrees {
}
myservo.write(pos); delay(20);
// tell servo to go to position in variable 'pos' // waits 20ms between servo
commands
Future Electronics Egypt Ltd. (Arduino Egypt).
Controlling Servos with a Potentiometer or Sensor
This can be used for example if you want to control the pan and tilt of a camera
or sensor connected to the servos. It is almost the same code like the above
example with the addition of code to read the voltage on a potentiometer. This
value is scaled so that the position of the pot (from 0 to 1023) is mapped to a
value between 0 and 180 degrees. The only difference in the wiring is the
addition of the potentiometer; please see gure below for hardware connection
Arduino Sketch:
#include <Servo.h> Servo myservo; // create servo object to control a servo
int potpin = 0; // analog pin used to connect the potentiometer
int val;! // variable to read the value from the analog pin
void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop()
{
val = analogRead(potpin); ! ! // reads the value of the potentiometer
val = map(val, 0, 1023, 0, 179);!! ! // scale it to use it with the
servo!
myservo.write(val);! ! ! ! // sets position to the scaled value
delay(15);! ! ! ! ! // waits for the servo to get there
}
Future Electronics Egypt Ltd. (Arduino Egypt).
Iigurc 8-1. Contro||ing a scrvo with a potcntionctcr
Discussion
Anything that can Le ieau liom analogRead (see Chaptei 5 anu Chaptei 6) can Le useu
loi example, the gyio anu acceleiometei iecipes in Chaptei 6 can Le useu so that the
angle ol the seivo is contiolleu Ly the yaw ol the gyio oi angle ol the acceleiometei.
8.3 Controlling the Speed of Continuous Rotation Servos
Problem
You want to contiol the iotational uiiection anu speeu ol seivos mouilieu loi contin-
uous iotation. Foi example, you aie using two continuous iotation seivos to powei a
ioLot anu you want the speeu anu uiiection to Le contiolleu Ly youi sketch.
Solution
Continuous iotation seivos aie a loim ol geai ieuuceu motoi with loiwaiu anu Lack-
waiu speeu aujustment. Contiol ol continuous iotation seivos is similai to noimal
seivos. The seivo iotates in one uiiection as the angle is incieaseu liom 90 uegiees; it
iotates in the othei uiiection when the angle is uecieaseu liom 90 uegiees. The actual
uiiection loiwaiu oi Lackwaiu uepenus on how you have the seivos attacheu. Fig-
uie S-5 shows the connections loi contiolling two seivos.
This example sweeps the seivos liom 90 to 1S0 uegiees, so il the seivos weie connecteu
to wheels, the vehicle woulu move loiwaiu at a slowly incieasing pace anu then slow
8.3 Controlling the Speed of Continuous Rotation Servos | 267

You might also like