Servomotor Control
Servomotor Control
Servo motors are great devices that can turn to a specified position.
Usually, they have a servo arm that can turn 180 degrees. Using the Arduino, we can
tell a servo to go to a specified position and it will go there. As simple as that!
Servo motors were first used in the Remote Control (RC) world, usually to control the
steering of RC cars or the flaps on a RC plane. With time, they found their uses in
robotics, automation, and of course, the Arduino world.
Here we will see how to connect a servo motor and then how to turn it to different
positions.
2. A servo motor
3. Jumper wires
A servo motor has everything built in: a motor, a feedback circuit, and most important, a
motor driver. It just needs one power line, one ground, and one control pin.
1. The servo motor has a female connector with three pins. The darkest or even black one
2. Connect the power cable that in all standards should be red to 5V on the Arduino.
3. Connect the remaining line on the servo connector to a digital pin on the Arduino.
Step 2: Code
If the servo motor is connected on another digital pin, simply change the value of
servoPin to the value of the digital pin that has been used.
Servos are clever devices. Using just one input pin, they receive the position from the
Arduino and they go there. Internally, they have a motor driver and a feedback circuit
that makes sure that the servo arm reaches the desired position.
But what kind of signal do they receive on the input pin?
It is a square wave similar to PWM. Each cycle in the signal lasts for 20 milliseconds
and for most of the time, the value is LOW. At the beginning of each cycle, the signal is
HIGH for a time between 1 and 2 milliseconds. At 1 millisecond it represents 0 degrees
and at 2 milliseconds it represents 180 degrees. In between, it represents the value
from 0–180. This is a very good and reliable method. The graphic makes it a little easier
to understand.
Remember that using the Servo library automatically disables PWM functionality on
PWM pins 9 and 10 on the Arduino UNO and similar boards.
Code breakdown
The code simply declares the servo object and then initializes the servo by using
the servo.attach() function. We shouldn't forget to include the servo library. In the loop(),
we set the servo to 0 degrees, wait, then set it to 90, and later to 180 degrees.
Controlling servos is easy, and here are a few more tricks we can use:
Arduino has a built-in function servo.write(degrees) that simplifies the control of servos.
However, not all servos respect the same timings for all positions. Usually, 1 millisecond
For better control, we can use the servo.writeMicroseconds(us) function, which takes
1,000 microseconds.
More servos
In order to use more than one servo, we need to declare multiple servo objects, attach
different pins to each one, and address each servo individually. First, we need to
declare the servo objects—as many as we need:
// Create servo objects
Servo Servo1, Servo2, Servo3;
Then we need to attach each object to one servo motor. Remember, every servo motor
Servo1.attach(servoPin1);
Servo2.attach(servoPin2);
Servo3.attach(servoPin3);
Connection-wise, the grounds from the servos go to GND on the Arduino, the servo
power to 5V or VIN (depending on the power input), and in the end, each signal line has
to be connected to a different digital pin. Contrary to popular belief, servos don't need to
normal servo goes to a specific position depending on the input signal, a continuous
the signal. For example, the Servo1.write(0) function will make the servomotor spin
counter-clockwise at full speed. The Servo1.write(90) function will stop the motor