01205302 Digital Circuits and Microcontrollers Laboratory
UNIT 7
Servo Motor
Section ▢ 11 ▢ 250 ▢ 450
Name . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ID. . . . . . . . . . . . . . . . . . .
Instructor Part
Lab point (20) Problem point (10)
What is Servo Motor?
(The following materials are from https://howtomechatronics.com)
A servo motor is a closed-loop system that uses position feedback to control its motion
and final position. There are many types of servo motors and their main feature is the
ability to precisely control the position of their shaft.
In industrial type servo motors the position feedback sensor is usually a high precision
encoder, while in the smaller RC or hobby servos the position sensor is usually a simple
potentiometer. The actual position captured by these devices is fed back to the error
detector where it is compared to the target position. Then according to the error the
controller corrects the actual position of the motor to match with the target position.
Hobby servos are small in size actuators used for controlling RC toys cars, boats, airplanes
etc. They are also used by engineering students for prototyping in robotics, creating
robotic arms, biologically inspired robots, humanoid robots and so on.
How Servo Motors Work?
There are four main components inside of a hobby servo, a DC motor, a gearbox, a
potentiometer and a control circuit. The DC motor is high speed and low torque but the
gearbox reduces the speed to around 60 RPM and at the same time increases the torque.
2
The potentiometer is attached on the final gear or the output shaft, so as the motor
rotates the potentiometer rotates as well, thus producing a voltage that is related to the
absolute angle of the output shaft. In the control circuit, this potentiometer voltage is
compared to the voltage coming from the signal line. If needed, the controller activates
an integrated H-Bridge which enables the motor to rotate in either direction until the two
signals reach a difference of zero.
A servo motor is controlled by sending a series of pulses through the signal line. The
frequency of the control signal should be 50Hz or a pulse should occur every 20ms. The
width of pulse determines angular position of the servo and these type of servos can
usually rotate 180 degrees (they have a physical limits of travel).
3
Generally pulses with 1ms duration correspond to 0 degrees position, 1.5ms duration to
90 degrees and 2ms to 180 degrees. Though the minimum and maximum duration of the
pulses can sometimes vary with different brands and they can be 0.5ms for 0 degrees
and 2.5ms for 180 degrees position.
Metal gear Servo MG995 Description
MG995 servo is a simple, commonly used standard servo for your mechanical needs such
as robotic head, robotic arm. It comes with a standard 3-pin power and control cable for
easy using and metal gears for high torque.
Mechanical Specification
Size 40.4*19.9*37.5mm
Weight 58g
Gear type 5 metal gear
Limit angle 180°±5°
Bearing DUAL BB
Horn gear spline /
Horn type Metal
Case Engineering plastics(Polyamide)
4
Connector wire FP: 240mm±5mm JR: 300mm±5mm
Motor DC motor
Splash water resistance No
Electrical Specification
Operating voltage 4.8V
Idle current 5mA
No load speed 0.17sec/60°
Runnig current 350mA
Peak stall torque 9.0kg.cm
Stall current 1500mA
Control Specification
Command signal Pulse width modification
Amplifier type Digital controller
Pulse width range 500-2500usec
Neutral position 1500usec
Running degree 180°±3°(when 500~2500usec)
Dead bandwidth 4 usec
Rotating direction Counterclockwise (when 500~2500usec)
5
Learning Unit
(The following materials are from arduino.cc)
1. Sweep Circuit
Servo motors have three wires: power, ground, and signal. The power wire is typically red,
and should be connected to the 5V pin on the Arduino board. The ground wire is typically
black or brown and should be connected to a ground pin on the board. The signal pin is
typically yellow or orange and should be connected to PWM pin on the board. In these
examples, it is pin number 9. For the Sweep example, connect the servo motor to +5V,
GND and pin 9. Sweeps the shaft of a RC servo motor back and forth across 180 degrees.
Fig. 1-1 Servo motor sweep circuit
Sketch 1
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards
int pos = 0; // variable to store the servo position
void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
6
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 >= 0; 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
}
}
Q1-1. Explain how function myservo.attach() and myservo.write() work.
Q1-2. Explain sketch 1 corresponding to its result.
Check
7
2. Knob Circuit
For the Knob example, wire the potentiometer so that its two outer pins are connected
to power (+5V) and ground, and its middle pin is connected to A0 on the board. Then,
connect the servo motor to +5V, GND and pin 9.
Fig. 2-1 Servo motor sweep circuit
Sketch 2
#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() {
// reads the value of the potentiometer (value between 0 and 1023)
val = analogRead(potpin);
// scale it to use it with the servo (value between 0 and 180)
val = map(val, 0, 1023, 0, 180);
myservo.write(val); // sets the servo position according to the scaled value
delay(15); // waits for the servo to get there
}
8
Q2-1. Explain sketch 1 corresponding to its result.
Check
9
Problem
P1. Use the LCD with keypad shield to control and display the angle of servo motor.
Sketch P1
Problem 1 (10)
10
Reference
[1] Dejan howtomechatronics.com, How to Control Servo Motors with Arduino –
Complete Guide , https://howtomechatronics.com/how-it-works/how-servo-motors-
work-how-to-control-servos-using-arduino/, Feb, 2022
[2] makeblock.com, Micro bit lesson — Using the Ultrasonic Module MG995 Standard
Servo Specification https://www.makeblock.com/project/mg995-standard-servo,
Feb, 2022
[3] Arduino.cc Servo Motor Basics with Arduino,
https://docs.arduino.cc/learn/electronics/servo-motors, Feb, 2022
11