There are many applications that require precise control of the motor’s angle, they are used in robotic joints, solar tracking systems, automatic door openers, they are basically used for anything that requires precise rotational movement.
Servo motors are vastly different from regular motors; as they don’t just rotate when they have power connected to them. But, they only move when they receive a signal that tells them how much they should move, while being connected to a power source of course.
This signal is sent as an electric pulse, and the length of this pulse is translated by the servo motor into the angle that it should rotate. Servo motors also have high resistance to external forces, meaning that they try to keep their current angle and resist any change to it.
Servo motors are rated in kg/cm (kilogram per centimeter), and most hobbyist servo motors are rated at 3 kg/cm, 6 kg/cm, and 12 kg/cm. This rating represents the motor’s torque for a pulley at the radius of 1cm, and it gets weaker as the radius increases.
How a Servo Motor Works
Servo motors consist of four main parts which are called the servo mechanism, and they are:
- The DC motor
- The gear assembly.
- The potentiometer.
- The control circuit.
The servo’s rotation source is the DC motor. But, a DC motor has high speed, and low torque. Which aren’t what you need a servo for, because you need your servo motor to be strong and steady, and this where the gear assembly plays its part, as it works to reduce the DC motor’s speed and increase its torque, which is exactly what we need.
Now that our servo motor is strong, we need the ability to accurately control its rotation, and this is the job of the potentiometer and control circuit. The potentiometer is connected to the last gear of the output shaft, and it generates voltage as the shaft rotates. Then this voltage is compared to the voltage of the servo’s control circuit, which is the voltage that we sent to the servo motor from our microcontroller. Then it keeps rotating until the voltage generated by the potentiometer is the same as the one we sent to the control circuit, and when the difference between them reaches 0, the servo motor stops, and waits the next signal to take an action.
Controlling Servo Motors
A servo motor operates at 50Hz, which means that it expects a signal every 20 ms, and the length of this signal determines how much the servo motor should rotate. The servo expects this signal to be a PWM signal, which is a type of digital signals, which you can control the width “length” of. And the degree that the servo motor rotates depends on this length, which can vary between 1ms and 2ms:
- 1000 microseconds(1ms) → 0°
- 1500 microseconds(1.5ms) → 90°
- 2000 microseconds(2ms) → 180°
Simple Example with Servo Motor
Now we know how the servo motor operates, but what we still don’t know how to use it. For that, we will create a simple example where we use PICO to rotate our servo motor from 0° to 180° and vise versa.
First of all, let's look at our servo motor’s pinouts:
- Red wire → Positive lead.
- Brown or black wire → Negative lead.
- Yellow or orange wire → Signal lead
Now, let's start wiring!
1. Connect a male-male pin header to a breadboard.
2. Connect the servo motor pins with the pin header.
3. Connect the negative (GND) lead of the servo motor to the negative lead of the external battery.
4. Connect the positive lead of the servo motor to the positive lead of external battery.
5. Connect the servo motor signal wire to the D3 pin of the PICO.
Then don’t forget to connect the GND of the external battery to the GND of the PICO
Here’s the final wiring diagram:
One of the greatest things about the open source community is the talented people who extend the programming platform's functionality by writing some additional software for us to use, as they make libraries that extend Arduino's IDE functionality.
In our case, we use the “Servo.h” library that gives us the ability to use extra functions that help us control the servo motor easily without a hassle. For example, it helps me move the motor shaft to a specific angle without having to program the pulses manually, just write the angle which you need the servo to move to and it will do the hard work for you.
All you need to do is to use this library and import it to your code, then all the library functionality will be available to use.
The aim of this program is to move the servo motor shaft from angle 0 to angle 180 then return back from angle 180 to angle 0 again.
We are using two for loops, one for increasing the motor shaft angle position (from 0 to 180), and the second loop is for returning back the motor shaft to 0 (from 180 to 0), and the servo's current angle will be continuously printed on the serial monitor, and that is it!