1-Servo Motor Control Using Arduino
1-Servo Motor Control Using Arduino
Harsh Panchal
There are many types of servo motors. Servo motors are classified 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 as
they don’t need more current to drive.
Usually servo motors comes with arms (metals or plastic) that is connected to the object required
to move (see figure below to the right). As per servo definition (www.arduio.cc), a Servo is[1]:
“Servos have integrated gears and a shaft that can be precisely controlled with the control PWM
signal. Standard servos allow the shaft to be positioned at various angles, usually between 0 and
180 degrees. Continuous rotation servos allow the rotation of the shaft to be set to various
speeds.”
Operation:
The servo shaft position is measured by the potentiometer, is continually compared to the
commanded position from the control (i.e., the radio control). Any difference gives rise to
an error signal in the appropriate direction, which drives the electric motor either forwards or
backwards, and moving the output shaft to the commanded position. When the servo reaches this
position, the error signal reduces and then becomes zero, at which point the servo stops moving.
Any changes in servo position that commanded, whether due to command
changes, or mechanically pushed from its set position, the error signal will re-appear and cause
the motor to restore the servo output shaft to the position needed.
Most of all servo motors are proportional servos, where this commanded position can be
anywhere within the range of movement. Early servos, and a precursor device called an
escapement, could only move to a limited number of set positions.
The above Figure 1 shows the pin diagram for servo motor SG90. The orange or yellow wire
accept the control signal which is a pulse-width modulation (PWM) signal produced by Arduino
board. Servo accepts the signal from Arduino Microcontroller which 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 of the motor turns to.
3. Connect the red wire to the power (+5V) and black wire to the ground on the Arduino
Microcontroller on power pins. The control signal is provided through the Arduino Mega
from pin number 9. Check your program and verify your pin number with actual pin
connected on the Arduino.
4. Now connect your Arduino mega through USB wire which is provided with your
Arduino controller.
5. Now open Arduino Sketch program and prepare program which was discussed in class.
6. Download your Arduino sketch program in the Arduino Mega using upload button and
then wait until it finish uploading.
7. Servo motor will make rotation according to angle set in sketch program.
8. Show your sketch program and show your working setup to professor.
1. Connect servo motor according to the wiring diagram given in Figure-4. The signal pin
is typically yellow or orange and should be connected to pin 9 on the Arduino board.
2. The potentiometer should be wired so that its two outer pins are connected to power
(+5V) and ground, and its middle pin is connected to analog input 0 on the Arduino
mega.
3. Now open Arduino sketch program to write program to control servo motor using
potentiometer (10k).
4. Prepare program according to discussed in class. Debug your program to check for error
if any.
5. Upload your program to Arduino Microcontroller using upload button.
6. To check the function of servo, change potentiometer positon to test speed.
7. Show your result to professor for grading purpose.
To work with Servo, you need to copy servo library to library folder of Arduino (C:\Program
Files (x86)\Arduino\libraries). This library allows an Arduino board to control RC servo motors.
Servos have integrated gears and a shaft that can be precisely controlled. Standard servos allow
the shaft to be positioned at various angles, usually between 0 and 180 degrees. Continuous
rotation servos allow the rotation of the shaft to be set to various speeds.
The Servo library supports up to 12 motors on most Arduino boards and 48 on the Arduino
Mega. On boards other than the Mega, use of the library disables analogWrite() (PWM)
functionality on pins 9 and 10, whether or not there is a Servo on those pins. On the Mega, up to
12 servos can be used without interfering with PWM functionality; use of 12 to 23 motors will
disable PWM on pins 11 and 12.
RC Servos are used to convert electrical signal into polar or linear movement. A simple example
is the steering system of an RC car. When signal is transmitted from the control to the car, this
signal is decoded and sent to a servo. According to this signal, the servo will rotate its drive shaft
for some degrees, and this rotation is translated into wheel steering.
Servos are very handy due to the one reason. The reason is that, it is very easy to drive them
with a simple PWM circuit. They can achieve from low to higher torques, enough to move
almost everything needed in an RC model, they are very compact and reliable, and most of all,
they come with very low prices according to their specifications.
Types of RC Servos
There are many manufacturer for RC servos. The cost will depends on the following important
points.
Precision: How accurately will the servo translate the input signal into drive shaft
position
Speed: how fast can this translation be made
Strength: What torque can achieve during rotation
Break strength: With how much load can be the servos' drive shaft be loaded without
losing its position
Motion type: How does the drive shaft moves. it could be circular (like motors) or linear
(like pistons)
Size and Weight: An important consideration when used in small planes or helicopters.
Typically, smaller servos have lower torque.
Bearing type: Standard servos have bushings supporting the main shaft, heavy duty
servos typically have one or two ball bearings supporting the main shaft.
Gearbox type: Most of servo uses nylon gears in the gearbox. For heavy duty servos,
there are also metallic gears. Carbonite gears are rather new to the market and offers
higher strength than nylon gears (5 times stronger or more) and very high durability, but
they are still expensive. The titanium gears are most durable. They are much lighter than
the metallic gears and in some cases they are more durable than them. Thus, they can
achieve higher torques and speeds.
Motor type: The standard motor is a three pole ferrite motor. For some high speed
servos, five poles core-less motors are used. For heavy duty servos, heavy duty core-less
motors are used.
When the input voltage is applied to the analog pin, the analog signal appear at the analog pin
specified in the program. The Arduino board contains a 6 channel (8 channels on the Mini and
Nano, 16 on the Mega), 10-bit analog to digital converter. This means that it will map input
voltages between 0 and 5 volts into integer values between 0 and 1023. This yields a resolution
between readings of: 5 volts / 1024 units or, .0049 volts (4.9 mV) per unit. The input range and
resolution can be changed using analogReference().
Exercise: 1
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created
int pos = 0; // variable to store the servo position
void setup()
{
myservo.attach(9); // attaches the servo on pin 9
}
void loop()
{
for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
#include <Servo.h>
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 (value
between 0 and 1023)
val = map(val, 0, 1023, 0, 179); // scale it to use it with the servo (value
between 0 and 180)
myservo.write(val); // sets the servo position according to the
scaled value
delay(15); // waits for the servo to get there
}
References:
1. www.arduino.cc
2. http://en.wikipedia.org/wiki/Servo_(radio_control)
3. http://www.cs.uregina.ca/Links/class-info/207/Lab6/pot_accel.html
4. http://people.interaction-ivrea.it/m.rinott