Laboratory-7
Laboratory-7
Discussion
A servomotor is a rotary actuator or linear actuator that allows for precise control of angular or
linear
position, velocity and acceleration. It consists of a suitable motor coupled to a sensor for position
feedback. It also requires a relatively sophisticated controller, often a dedicated module designed
specifically for use with servomotors.
Servomotors are not a specific class of motor although the term servomotor is often used to refer
to a
motor suitable for use in a closed-loop control system.
A servomotor is a closed-loop servomechanism that uses position feedback to control its motion
and
final position. The input to its control is a signal, either analogue or digital, representing the position
commanded for the output shaft.
The motor is paired with some type of encoder to provide position and speed feedback. In the
simplest case, only the position is measured. The measured position of the output is compared to the
command position, the external input to the controller. If the output position differs from that required, an
error signal is generated which then causes the motor to rotate in either direction, as needed to bring the
output shaft to the appropriate position. As the positions approach, the error signal reduces to zero and the
motor stops.
57
A servomotor consumes power as it rotates to the commanded position but then the servomotor
rests.
Stepper motors continue to consume power to lock in and hold the commanded position.
Procedures:
#include <Servo.h>
58
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(100); // 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(100); // waits 15ms
for the servo to reach the position
}
}
2. After uploading, disconnect first the gizDuino microcontroller to the laptop/computer. Prepare servo
motor and follow the connections of Figure 8.3 below.
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 Arduino board. The signal pin is typically
yellow, orange or white and should be connected to pin 9 on the Arduino board.
4. After connecting all the necessary configurations, connect again the gizDuino microcontroller
to the laptop/computer by a usb-to-serial cord.
5. Observe what is happening on the servo motor. Answer guide questions.
59
6. With the same connections on a seven segment display, upload this source code below the
figure:
#include <Servo.h>
void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop()
{ for(pos = 0; pos < 180; pos += 10) // goes from 0 degrees to 180 degrees
{ // in steps of 10 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(1000); // waits 1s for the servo to reach the position
}
60
for(pos = 180; pos>=1; pos-=10) // goes from 180 degrees to 0 degrees
{ myservo.write(pos); // tell servo
to go to position in variable 'pos' delay(1000); // waits 1s
for the servo to reach the position
}
}
Guide Questions:
1. Describe what is happening on the servo motor on the first source code.
Answer:
In the first source code, the servo motor is being controlled to move to specific angles using
programmed instructions. The code starts by attaching the servo to a pin on the microcontroller (like an
Arduino), then sends angle values to it using the servo.write() function. For example, the code might tell
the servo to move from 0° to 90°, then to 180°, pausing between each move using a delay function. This
cycle might repeat continuously. The motor doesn’t rotate like a DC motor—instead, it turns to and holds
a set angle precisely, which is great for applications like moving arms or controlling levers.
I noticed that the servo motor doesn’t rotate in full circles. Instead, it moves back and
forth within a limited range, usually from 0° to 180°. The movement is controlled and stops exactly at
the angle specified in the code. If the code tells it to move from 0° to 90°, it will rotate to 90° and stay
there until it receives another instruction. I also observed that the speed of rotation is affected by how
fast or slow the delay is set in the code. A short delay makes the servo move quickly to the next
position, while a longer delay makes the movement slower and easier to watch. Overall, the rotation is
smooth, and the motor holds its position firmly after reaching the angle.
3. Can we use other pins aside from pin 9 for the attaching servo motor?
Answer:
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
Review Questions:
1. What is the difference between myservo.write and myservo.attach?.
Answer:
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
2. In the source code, how does the servo motor rotates?
Answer:
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
3. What is the maximum turn a servo motor can have?
Answer:
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
Challenge Yourself:
1. Program a gizDuino microcontroller such that the servo motor will rotate every 5 degrees with a gap 1
second form 0 degree to 180 degrees but every time it reaches 60 degrees it will stop for 10 seconds
before proceeding. Present your output to your instructor if done
Conclusion:
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
_____________________________________________________________________________________
61