You are on page 1of 6

UG104: Praxis I

Asian Institute of Technology


Handout: Arduino-Servo Motor Control

September 2013
Undergraduate Program
Instructors: Waqar Shahid, Matthew N. Dailey

Servo Motor Control Using Arduino

Introduction
In this tutorial, you will learn about servo-motor control using the Arduino.
Servos work well with remote control models of cars, airplanes, and boats, and they are also popular
in building robotic systems, for example, arms, legs, and scanners, etc. The components of servo motors
are packaged in a very compact, low-cost unit. They come in two types: position controlled servos and
continuous rotation servos.
A position controlled servo contains a D.C small motor, a gear box for speed reduction, a sensor (normally
a potentiometer) to measure the position of the output gear, and an electronic circuit that controls the
motor to make the output gear to control, precisely, the speed, mechanical position, and angular movement
of the rotor. The servo system is an automatic system that uses error-sensing negative feedback to correct
performance.
Continuous rotation servos are common in hobby electronics. They present the same interface to the
micro-controller or remote control receiver as a regular (position controlled) servo but control the motors
rotational velocity rather than its absolute position.
All servo-motor units have three wires: power, ground, and the control signal wire. Most position
controlled servos rotate from 0 to 180 degrees, but some may rotate 0360 degrees, and others have no limit.
The circuit helps to decode the output gear position/velocity, amplify the error between the current and
requested position/velocity, and drive the motor to rotate to the precisely.

Figure 1: Example Use of Servo Motor in helicopter

Figure 2: g:Example Use of Servo Motor in Robotic Arm


The control-pin of the servo motor is attached to a micro-controller to generate pulses of dierent
lengths a specic frequency. The length of the pulse controls the output gear position. This can be achieved

using pulse width modulation (PWM). Figure 3 shows the servo-motor mechanism block diagram. Figure 4
shows a sample of the PWM output required for the control signal.
The minimum pulse, the maximum pulse, and the repetition rate are important parameters for a servo
motor. When a typical pulse is applied, the servo will move to the position and hold that position or move
at the desired velocity. If an external force is applied in opposite direction to the servo rotor while the servo
is rotating or holding a position, the servo will resist that force. The maximum amount of force the servo
can exert or resist is the torque rating of the servo. It is recommended that pulse be repeated to instruct
the servo to stay in position.
For most servo motors, the minimum pulse is 1ms, the maximum pulse is 2ms, and the repetition
frequency is 50 Hz. A 1ms pulse sent at at a rate of 50 cycle per second will keep the servo at zero degree
or at a velocity of 0, while a 2ms pulse at 50 Hz will make it rotate to 180 degrees and hold the position or
make it rotate clockwise at the maximum velocity.
Another parameter that varies from position servo to position servo is the turn rate. This is the time
taken by the servo to change from one position to another. The worst case turning time is when the servo
is holding at the minimum rotation and it is commanded to go to maximum rotation.

Figure 3: Servo Motor Block Diagram

Figure 4: Servo Motor Block Diagram


PWM-voltage converter: The control signal pulse is fed to a PWM to Voltage Converter that charges
a capacitor at a constant rate while the pulse is high. When the pulse goes low the charge on the capacitor
is fed to the output via a suitable buer amplier. This produces a voltage proportional to the length of the
applied pulse. The output voltage is buered and does not decay signicantly between control pulses so the
length of time between pulses is not critical.
Position Sensor: The current rotational position of the servo motor output shaft is read by a potentiometer that produces a voltage proportional to the absolute angle of the output shaft. The potentiometer
then feeds its value to the error amplier that compares the current position with the desired position from
the pulse width to voltage converter.
Error Amplier: The error amplier is an operational amplier with negative feedback that produces
an output voltage proportional to the dierence of the input voltages. The greater the dierence the greater
the voltage. The error amplier output is used to drive the motor; If it is positive the motor will turn in one
direction, if it is negative then the motor will run in the other. When the dierence between the desired and
current position is large, motor moves fast. When the dierence is less, the motor moves slow. Therefore,
turning the motor to its desired position. All servo have three wires: Black or Brown is for ground. Red is
for power (4.8 V to 6 V). Yellow, Orange, or White is the control signal wire.
Servo-Library-Arduino This library allows an Arduino board to control RC (hobby) servo motors. It
supports up to 12 motors. To use the library, you need to include the le server.h. Then you can create

an object of type Servo using the type Servo followed by the object name. You can invoke the following
methods on the Servo object to perform your task:
attach(): attach a pin to a servo.
write(): rotate a servo by giving a desired angle value (writing).
writeMicroseconds(): control servos by pulse-widths in microseconds.
read(): read the current value (angle in degrees) of servos
attached(): check if a pin is attached to a servo.
detach(): detach a pin to a servo

With the library you can control the motor both using the desired angle in degree or by controlling the
PWM pulse length. The functions are write() and writeMicroseconds() respectively.

Continuous servo motor


In the lab we will use the continuous rotation servos. It will change the direction and speed according to
the pulse width sent to the motor. If a pulse train with 1.5ms pulse-width is sent then the motor will stop.
If pulse-width is 1ms, then the motor will rotate in CCW direction at full speed. The motor keeps rotating
in CCW direction with pulse-widths from 0.8ms - 1.5ms but its speed is reduced more if the pulse-width is
closer to 1.5ms. If pulse width of 2.2ms is sent, the motor will rotate in CW direction at full speed and its
speed is reduced more if pulse-width is closer to 1.5ms. Table 1 summarizes the commands for continuous
servo motors.
commands write
myServo.write(0)
myServo.write(90)
myServo.write(180)

commands writeMicroseconds
myServo.writeMicroseconds(800)
myServo.writeMicroseconds(1500
myServo.writeMicroseconds(2200)

Action
Servo will rotate CCW with full speed.
Servo will stop.
Servo will rotate CW with full speed.

Hardware required
Arduino board
RC (hobby) servo motor
Potentiometer 10K
Bread board
Connection wires
LED (01), button, resistor 50 , resistor 10K

Experiment-1
The servo will rotate in CCW direction at full speed for 1 second, then it stops for 1 second. It changes
direction to CW and rotate at full speed for 1 second, then stops for 1 second and so on.
Implement the circuit as shown in the diagram of Figures 5, 6.
Right the following program and observe the output.
If your servo is moving at 1.5ms that means it requires manual calibration.
ajust the potentiometer so that your motor does not move at 1.5ms.

#include <Servo.h>
// Create a Servo object to control each servo.
// A maximum of 8 Servo objects can be created.
Servo myServo;
void setup()
{
// Attach the Servo object's control signal to pin 9
myServo.attach(9);
}
void loop()
{
// 0 degrees or maximum counterclockwise velocity
myServo.writeMicroseconds(800);
delay(1000);
// 90 degrees or 0 velocity
myServo.writeMicroseconds(1500);
delay(1000);
// 180 degrees or maximum clockwise velocity
myServo.writeMicroseconds(2200);
delay(1000);
// 90 degrees or 0 velocity
myServo.writeMicroseconds(1500);
delay(1000);
}

Experiment-2
The servo will rotate in CCW with increasing speed. When the speed gets full value, it gradually reduces
till zero. At this point, the direction is changed to CW. The servo then increases speed to full then reduces
to zero and the direction is changed to CCW.
Implement the circuit as shown in the diagram of Figures 5, 6.
Write the code mentioned below for experiment-2.
Observe the output as mentioned above.

Assignment

Problem statement: Assuming the grip provided along with the servo is attached to the line-following
ground robot. Write a program that your grip hold an object lying in front of the robot, picks it up, turn the
object, turn the object back , release the object

Figure 5: Servo Motor Block Diagram

Figure 6: Servo Motor Block Diagram

Code-Experiment-2
Code-1: Arduino Example program for Servo
#include <Servo.h>
//create servo object to control a servo
//a maximum of eight servo objects can be created
Servo myServo;
int pos = 0;
void setup()
{
myServo.attach(9);
}//attach control signal to pin 9
void loop()
{
//servo goes from not moving (90) to full speed in CCW direction (0)
//decreasing the value by 1 each loop
for(pos = 90; pos > 1; pos -= 1)
{
myServo.write(pos);//tell servo to go to position in variable 'pos'
delay(25);
}//waits 25 ms for the servo
//servo goes from moving full speed in CCW direction (0) to not moving (90)
//increasing the value by 1 each loop
for(pos = 0; pos < 90; pos += 1)
{
myServo.write(pos);// tell servo to go to position in variable 'pos'
delay(25);
}// waits 25 ms for the servo
//change direction to CW
//servo goes from not moving (90) to full speed in CW direction (180)
//decreasing the value by 1 each loop
for(pos = 90; pos < 180; pos += 1)

{
myServo.write(pos);//tell servo to go to position in variable 'pos'
delay(25);
}// waits 25 ms for the servo
//servo goes from moving full speed in CW (180) to not moving (90)
//increasing the value by 1 each loop
for(pos = 180; pos > 90; pos -= 1)
{
myServo.write(pos);//tell servo to go to position in variable 'pos'
delay(25);
}
}// waits 25 ms for the servo

Acknowledgement
Images are taken from Mr. H.H. Manh Handouts for Servo Motors. Thanks to Lab Supervisor Mr.H.H.Manh
from Mechatronics AIT for sharing his handout

References
1. http://en.wikipedia.org/wiki/Proximity_sensor.
2. http://profesores.fi-b.unam.mx/m3615m/datasheet-sen136b5b.pdf.
3. http://www.seeedstudio.com/wiki/Ultra_Sonic_range_measurement_module.
4. http://arduino.cc.
5. Arduino Programming Notebook by - by Brian W. Evans.
6. Beginning Android ADK with Arduino by Mario Bohmer
7. http://arduino.cc/en/uploads/Main/arduino_Uno_Rev3-02-TH.zip.
8. http://arduino.cc/en/uploads/Main/Arduino_Uno_Rev3-schematic.pdf.
9. http://arduino.cc/en/Main/ArduinoBoardUno.
10. Computational Principles of Mobile Robotics by from Dudek and Jenkin.

You might also like