Before we assemble the rover and test it out, we need to learn about this type of a motor
which is called a servo motor. We have previously learnt about DC motors, so what is the use
of a servo motor?
DC motors are extremely simple – as soon as you apply a suitable voltage across their
terminals they will start spinning and continue spinning until the voltage is removed. But say you
had something like this, and all you wanted to do was rotate it about 45 degrees like
so. If you wanted to do this with a DC motor then you would have to track the time it would
take to go from 0 to 45 degrees when a certain voltage is applied to the motor. You would then have to
apply that particular voltage for the exact time. This is not exactly simple and it gets
more complicated as the position accuracy will depend on the amount of load applied
to the motor, the voltage and so on.
This is where a servo can be extremely useful. It consists of a DC motor along with some
additional circuitry which gives you more control over the rotation. The circuitry is
also smart enough to detect its current position and apply any corrections if needed. This
is an example of a very commonly used servo which is also called an SG90 or micro servo.
We will take it apart and we will also test it out by writing a sketch. You do get different
types of servos but they all follow an extremely similar principle. The main difference would
be the torque output which would also mean that some may require a higher current for
operation. These servos work between 4.8-6V DC, and consume a low enough current which
means that we can directly use the 5V power supply from an Arduino board. These servos
are also commonly used in RC model planes to position control surfaces as they are light
weight and extremely easy to use.
When you purchase a servo, you would get a couple of things like this. You have the servo
itself along with a couple of accessories that can be attached to it. These three attachments
are called servo horns and they are used to link different components to the servo depending
on the final application. I am going to slide this one over and screw it in place so that
you can visualize the servo position. Servo motors like this one are designed to rotate
from 0 to 180 degrees. They have a total of 3 terminals – 2 for power and one for the
control signal.
The control signal itself is a PWM signal with a frequency of 50Hz. The 50Hz frequency
corresponds to a time period of 20mS, which means that these pulses have to be sent every
20ms as can be seen here. The pulse width determines the final position of the servo.
A pulse width of 1ms corresponds to 0 degrees, 1.5ms corresponds to 90 degrees while 2ms
corresponds to 180 degrees.
Servo motors are extremely easy to use and that is the reason why they are widely used
in robotics applications.
I have an identical servo here that I am going to take apart. We will then try to understand
how it works. It is held together by four screws and you also need to free up the bottom
half by removing the stickers. We can then remove the top half to reveal the gear mechanism
which can be easily removed. Finally, we can push away the remaining components to give
us something like this. What we have here is a DC motor that is extremely similar to
the one we looked at previously, this green part is a potentiometer which we have also
looked at, we then have the PCB which contains the controller IC and finally, we have the
gear mechanism which links everything together and also increases the final torque just like
in the DC motor seen here.
This is how I will represent the sections of the servo. The potentiometer is used for
feedback and the output voltage from it will depend on the current servo position. The
control signal that is applied to the servo is send to the control IC along with the output
voltage from the potentiometer. The output from the control IC is connected to the DC
motor. If there is a difference between the current servo position and the input control
signal then the DC motor will rotate to reduce the difference. This will continue until the
different is 0 or within a small limit.
This is called a closed loop as a potentiometer is used to measure the output position and
this is then used to determine and minimize any errors. The controller keeps comparing
the current output to the control signal and if the output position has changed for any
reason then it will rotate the motor to minimize and eliminate this difference. This means
that you can be sure that the output is at the angle that you have set it to, as long
as you do not overload the servo. Now that we know how the servo works, let’s quickly
control it using an Arduino Nano.
Arduino has a built-in servo library that makes it easy for us to control a servo. We
start by including the library, creating an object and then specifying the pin which we
will be using as a control pin. Remember that we need a PWM signal so we need to use a PWM
compatible pin. In the main loop we simply use a for loop to rotate the servo from 0
to 180 degrees and wait for 20ms after each angle. We then go from 180 to 0 degrees. You
can also directly move between angles as seen here. One thing to keep in mind is that the
servo takes some amount of time to rotate. This servo takes 120ms to rotate 60 degrees,
so make sure you give it enough of time to complete the rotation. Upload the sketch to
the board and use this reference diagram if needed.
You will see the servo in action. Going from 0 to 180 degrees, then the reverse direction
from 180 to 0 degrees and finally, rotating in steps of 60 degrees and back to 0.
There’s one more thing you need to keep in mind when it comes to using servos.
Let me modify the sketch to rotate to 0 degrees and then 180 degrees.
You will notice that the servo makes a bit of noise when it is at the end positions.
This is because of manufacturing tolerances which prevent the servo from reaching the
end positions. The control IC is still sending a signal to the motor and it is trying to
reach the final value which is the noise that you hear.
In summary, although the servo is designed to rotate between 0 to 180 degrees, in reality
it may have an offset which will only allow it to rotate from 5 degrees to 185 degrees
or something similar. That is the reason why we generally trim the servo to eliminate this
oscillation at the end values. When we trim a servo using a remote-control unit all we
are doing is saying that the lowest angle that the servo can achieve is not 0 degrees
but 5 degrees for example. This is something you need to keep in mind when you are writing
code to control a servo and also when you are designing hardware that will attach to
the servo.
And that is a quick introduction to servos. We will use a servo to rotate the ultrasonic
sensor and we will add this to the rover in a future video. Do subscribe to stay updated,
if you haven’t already. Thank you for watching and I will see you in the next one!