Week 6 Servomotors
Week 6 Servomotors
rotate with great precision. If you want to rotate an object at some specific
angles or distance, then you use a servo motor. It is just made up of a simple
motor which runs through a servo mechanism. If motor is powered by a DC
power supply then it is called DC servo motor, and if it is AC-powered motor
then it is called AC servo motor. In this lecturer , we will be discussing only
about the DC servo motor working. Apart from these major classifications,
there are many other types of servo motors based on the type of gear
arrangement and operating characteristics. A servo motor usually comes with a
gear arrangement that allows us to get a very high torque servo motor in small
and lightweight packages. Due to these features, they are being used in many
applications like toy car, RC helicopters and planes, Robotics, etc.
Servo motors are rated in kg/cm (kilogram per centimeter) most hobby servo
motors are rated at 3kg/cm or 6kg/cm or 12kg/cm. This kg/cm tells you how
much weight your servo motor can lift at a particular distance. For example: A
6kg/cm Servo motor should be able to lift 6kg if the load is suspended 1cm
away from the motors shaft, the greater the distance the lesser the weight
carrying capacity. The position of a servo motor is decided by electrical pulse
and its circuitry is placed beside the motor.
1. Controlled device
2. Output sensor
3. Feedback system
The color coding of your servo motor might differ hence check for your
respective datasheet.
All servo motors work directly with your +5V supply rails but we have to be
careful on the amount of current the motor would consume if you are planning
to use more than two servo motors a proper servo shield should be designed.
Servo motor works on PWM (Pulse width modulation) principle means its
angle of rotation is controlled by the duration of applied pulse to its Control
PIN. Basically servo motor is made up of DC motor which is controlled by a
variable resistor (potentiometer) and some gears. High speed force of DC
motor is converted into torque by Gears. We know that WORK= FORCE X
DISTANCE, in DC motor Force is less and distance (speed) is high and in
Servo, force is High and distance is less. The potentiometer is connected to the
output shaft of the Servo, to calculate the angle and stop the DC motor on the
required angle.
Servo motor can be rotated from 0 to 180 degrees, but it can go up to 210
degrees, depending on the manufacturing. This degree of rotation can be
controlled by applying the Electrical Pulse of proper width, to its Control pin.
Servo checks the pulse in every 20 milliseconds. The pulse of 1 ms (1
millisecond) width can rotate the servo to 0 degrees, 1.5ms can rotate to 90
degrees (neutral position) and 2 ms pulse can rotate it to 180 degree.
All servo motors work directly with your +5V supply rails but we have to be
careful about the amount of current the motor would consume if you are
planning to use more than two servo motors a proper servo shield should be
designed.
To learn more about servo motor working principle and practical uses, please
check below applications where controlling of servo motor is explained with the
examples:
As discussed earlier, Arduino Due is an ARM controller based board designed for electronic
engineers and hobbyists. This DUE board can be used for making CNC machines, 3D printers,
robotic arms etc. All these projects have a common feature that is Position Control. Any of these
projects needs accuracy towards their position. Accurate positions in these machines can be achieved
by Servo Motors. In this session we are going to control the position of a Servo Motor
with Arduino Due. We have already covered the Servo Motor Interfacing with Arduino
Uno and Servo Motor Interfacing with 8051 Microcontroller.
Servo Motors:
Servo Motors are known for their accurate shaft movement or position. These are not proposed for
high speed applications. These are proposed for low speed, medium torque and accurate position
application. These motors are used in robotic arm machines, flight controls and control systems.
Servo motors are also used in some of printers and fax machines.
Servo motors are available at different shapes and sizes. We will be using SG90 Servo Motor in this
tutorial. SG90 is a 180 degree servo motor. So with this servo we can position the axis from 0 to 180
degrees.
A Servo Motor mainly has three wires, one is for positive voltage, another is for ground and last one
is for position setting. The RED wire is connected to power, Brown wire is connected to ground and
YELLOW wire (or WHITE) is connected to signal.
A Servo Motor is a combination of DC motor, position control system and gears. In servo, we have a
control system which takes the PWM signal from signal pin. It decodes the signal and gets the duty
ratio from it. After that it compares the ratio to the predefined positions values. If there is a difference
in the values, it adjusts the position of the servo accordingly. So the axis position of the servo motor
is based on the duty ratio of the PWM signal to the SIGNAL pin.
The frequency of PWM (Pulse Width Modulated) signal can vary based on type of servo motor. The
important thing here is the DUTY RATIO of the PWM signal. Check this for PWM with Arduino
Due. However in this case, we need not even worry about with the Duty Ratio selection. In Arduino
we have a special function; upon calling it we can adjust the position of servo, just by stating the
angle. We will talk about that in the Working Section below.
Before Interfacing Servo Motor to Arduino Due, you can test your servo with the help of
this Servo Motor Tester Circuit. Also check these projects to Control Servo by Flex Sensor or by
Force Sensor.
Components:
Hardware: Arduino Due, power supply (5v), Servo motor.
For, details on How to download and install this software, visit the first tutorial Getting Started with
Arduino Due.
1. #include <Servo.h>
2. Servo myservo;
3. myservo.attach(servo_signal_pin_attached_to);
4. myservo.write(needed_position_ angle);
The header file “#include <Servo.h>” includes all the special functions we need, upon calling it we
no longer have to worry about the frequency of PWM nor about the DUTY RATIO of signal. With
this the user can enter needed position of servo directly without any fuzz.
Secondly a name is to be chosen for the servo by using “Servo myservo”, here myservo is the name
chosen, so while writing for position we are going to use this name, this feature comes in handy
when we have many servos to control, we can control as many as 12 servos by this.
With the Arduino Due having 12 PWM channels, we need to tell DUE where the signal pin of
servo is connected or where it needs to generate the PWM signal. To do this we
have “myservo.attach (2);”, here we are telling the DUE that we have connected the signal pin of
servo at PIN2.
All left is to set the position, we are going to set the position of servo by using “myservo.write(45);”,
by this command the servo hand moves 45 degrees. If we change ‘45’ to ‘175’, the servo axis angles
to 175 degrees and stays there. After that, whenever we need to change the position of servo we just
need to call the command “myservo.write(needed_position_angle);”.
In the program, we are going to increment and decrement the angles by using loops. So the servo
sweeps from 0 to 180, then from 180 to 0 and so on. The Servo Motor Control by Arduino Due is
best explained in step by step of C code down below.
Code
#include <Servo.h>
void setup() {
void loop() {
for (angle = 0; angle <= 180; angle += 1) { // goes from 0 degrees to 180 degrees, in
steps of 1 degree
for (angle = 180; angle >= 0; angle -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(angle);
Servo motors are available at different shapes and sizes. A servo motor will have mainly there wires,
one is for positive voltage another is for ground and last one is for position setting. The RED wire is
connected to power, Black wire is connected to ground and YELLOW wire is connected to signal.
A servo motor is a combination of DC motor, position control system, gears. The position of the
shaft of the DC motor is adjusted by the control electronics in the servo, based on the duty ratio of
the PWM signal the SIGNAL pin.
Simply speaking the control electronics adjust shaft position by controlling DC motor. This data
regarding position of shaft is sent through the SIGNAL pin. The position data to the control should
be sent in the form of PWM signal through the Signal pin of servo motor.
The frequency of PWM (Pulse Width Modulated) signal can vary based on type of servo motor. The
important thing here is the DUTY RATIO of the PWM signal. Based on this DUTY RATION the
control electronics adjust the shaft.
As shown in figure below, for the shaft to be moved to 9o clock the TURN ON RATION must be
1/18.ie. 1ms of ON time and 17ms of OFF time in a 18ms signal.
For the shaft to be moved to 12o clock the ON time of signal must be 1.5ms and OFF time should be
16.5ms. This ratio is decoded by control system in servo and it adjusts the position based on it. This
PWM in here is generated by using ARDUINO UNO.
Circuit Components
Hardware: ARDUINO UNO, power supply (5v), 100uF capacitor , buttons (two pieces), 1KΩ
resistor (two pieces), Servo motor (which needed to be tested).
The things which we need to do for getting accurate position of servo are:
1. #include <Servo.h>
2. Servo sg90servo;
3. Sg90.attach(servo_signal_pin_attached_to);
4. Sg90.write(needed_position_ angle);
First we need to set frequency of PWM signal and for that we should call “#include <Servo.h>”
header file, on including this header file in the program, the frequency gets set automatically and we
get to use some special conditions, which enables the user to enter needed position of servo directly
without any fuzz.
Now we need to define a name for the servo “Servo sg90sevo”, here ‘sg90servo’ is the name chosen,
so while writing for potion we are going to use this name, this feature comes in handy when we have
many servos to control, we can control as many as eight servo by this.
Now we tell the UNO where the signal pin of servo is connected or where it needs to generate the
PWM signal. To do this we have “Sg90.attach(3);”, here we are telling the UNO we connected the
signal pin of servo at PIN3.
All left is to set the position, we are going set the position of servo by using “Sg90.write(30);”, by
this command the servo hand moves 30 degrees, so that’s it. After that whenever we need to change
the position of servo we need to call the command ”Sg90.write(needed_position_ angle);”. In this
circuit we will have two buttons one button increases the position of servo and the other is for
decreasing the position of servo.
The Arduino Servo Motor control tutorial is explained in step by step of C code given below.
Code
volatile int i=0;//initializing a integer for incrementing and decrementing duty ratio.
void setup()
void loop()
if (digitalRead(0)==LOW)
if (i<180)
delay(30);
} }
if (digitalRead(1)==LOW) {
if (i>0)
{
i--;// if pin1 is pressed and degrees is
greater than 0
delay(30);} }