PAGE 1 | GREEN ENERGY HANDBOOK © 2 0 0 8 - 2018 M I C H A E L E T T E R S H A N K
https://www.robotscience.co.za
GREEN ENERGY: WORKSHOP 8.2: POSITION CONTROL SERVO MOTORS EXPLAINED
In this series we are going to build a desktop scale sun tracking solar panel. Building a full size solar tracking system would be fun and
exciting, but for most youth STEM education programmes that’s going to be very costly. We can still learn a great deal about green
energy by building smaller models of real word systems, and one day when we have to build a real system we can do so using the
knowledge and experience we gained in this workshop and hopefully avoid making the same mistakes.
So, armed with a steel ruler, knife, hot glue gun and some electronic parts, lets get started. The first thing we are going to need to
understand is how servo motors work. If you look at the illustration below, you will see that a hobby servo consists of a small hard
plastic box, with a horn [attachment point] protruding out the top. The servo horn rotates from 0 through 180 degrees according to a
signal that it receives through the attachment lead, which consists of three wires being positive [usually red] negative or ground
[usually black or brown] and the signal wire [usually white or yellow or orange].
In the illustration above [on the right] and below you can see what’s inside the box, being an electric motor, a variable resistor that
links into the bottom of the output shaft, an electronic control circuit and a gearbox with some fine-toothed gears. Watch out, as
rough handling a hobby servo and twisting on the horn can break the gears inside and render it useless.
In our model we are going to use two position servos to move the solar panel through the X and Y axes [1.] to follow the sun [or the
light on your cellphone] from sunrise in the East to sunset in the West and [2.] track the sun as it rises to its highest point at midday.
Position servos are controlled with a continuous stream of pulses from your microcontroller, switching on [HIGH] every 20
milliseconds [20,000 microseconds]. The duration of the HIGH [on] pulse [t] can range from 1,000 microseconds to 2,000
microseconds and that’s what determines the angle of the output shaft. The wait between pulses [T] is fixed at 20 milliseconds or
20,000 microseconds. The output shaft can move through 180 degrees. The servo motor will hold a position for as long as the
controller continues to receive the high pulse instructions which is taken care of by the microcontroller.
PAGE 2 | GREEN ENERGY HANDBOOK © 2 0 0 8 - 2018 M I C H A E L E T T E R S H A N K
https://www.robotscience.co.za
WARNING: DO NOT TWIST THE SERVO BY ITS DRIVE SHAFT … THAT WILL STRIP THE GEARS
This table illustrates the HIGH [on] pulse duration and how that corresponds to position of the servo horn.
Microseconds
POSITION OF SERVO HORN
value = (1000)
------------------------------------------------------
0
value = (1250)
------------------------------------------------------
45
value = (1500)
------------------------------------------------------
90
value = (1750)
------------------------------------------------------
135
value = (2000)
------------------------------------------------------
180
SERVO [MOVEMENT] TEST CODE FOR ARDUINO MICROCONTROLLER WITH SERVO CONNECTED TO D10
#include <Servo.h>
Servo myservo; // create servo object to control a servo
int pos = 0; // variable to store the servo position
void setup()
{
myservo.attach(10); // attaches the servo on pin 9 to the servo object
}
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
}
}
If you are using a different microcontroller, a quick Internet search will turn up some code you can use to test your servo. This Fritzing
illustration shows how you can connect your servo to the Aduino UNO.