There are many motors to pick from, but it’s important to pick the right one for the job. If your project requires precise positioning, a servo motor is usually the best option. Instruct them where to point, and they’ll do it for you. It’s as simple as that!
They’re useful in a variety of robotics projects, such as steering an RC model’s front wheels or pivoting a sensor on a robotic vehicle.
What is a Servo and what makes it precise?
Servos are motors that allow you to precisely control physical movement because they generally move to a position rather than continuously rotating. They are simple to connect and control because the motor driver is built right into them.
Servos contain a small DC motor connected to the output shaft through gears. The output shaft drives a servo horn and is also linked to a potentiometer (pot).

The potentiometer provides position feedback to the error amplifier in the control unit, which compares the current position of the motor to the target position.
In response to the error, the control unit adjusts the motor’s current position so that it matches the desired position.
In control engineering, this mechanism is known as a servomechanism, or servo for short. It is a closed-loop control system that uses negative feedback to adjust the motor’s speed and direction to achieve the desired result.

How Do Servo Motors Work?
You can control the servo motor by sending a series of pulses to it. A typical servo motor expects a pulse every 20 milliseconds (i.e., the signal should be 50Hz).
The length of the pulse determines the position of the servo motor.

- A short pulse of 1 ms or less will rotate the servo to 0 degrees (one extreme).
- A pulse duration of 1.5 ms will rotate the servo to 90 degrees (middle position).
- A pulse duration of 2 ms or so will rotate the servo to 180 degrees (other extreme).
Pulses ranging from 1ms to 2ms will rotate the servo to a position proportional to the pulse width. The animation below will help you understand the relationship between pulses and position.

It is important to note that there is no standard for the exact relationship between pulses and position, so you may need to tweak your sketch to adjust for the range of your servo.
Also, the pulse duration can vary between brands; for example, it might be 2.5ms for 180 degrees and 0.5ms for 0 degrees.
Servo Motor Pinout
Servo motors typically have three connections, as outlined below.

GND serves as a common ground for the motor and the logic.
5V is a positive voltage that powers the servo.
Control is an input for the control system.
The color of the wires varies between servo motors, but the red wire is always 5V and GND is either black or brown. The control wire is usually orange or yellow.
Wiring Servo Motor to Arduino UNO
Let’s hook the servo motor to the Arduino.
We will be using an SG90 Micro Servo Motor in our experiments. It operates on 4.8-6VDC (5V typical) and can rotate 180 degrees (90 in each direction).
It draws about 10mA when idle and 100mA to 250mA when moving, so we can power it with the Arduino’s 5-volt output.
If your servo consumes more than 250mA, consider using a separate power supply for it.
Connect the red wire to the Arduino’s 5V and the black/brown wire to ground. Finally, attach the Orange/Yellow wire to PWM enabled pin #9.
The following table lists the pin connections:
Servo Motor | Arduino | |
5V | 5V | |
GND | GND | |
Control | 9 |
The image below shows how to connect SG90 servo motor to the Arduino.

Arduino Example 1 – Sweep
To get started, we will use one of the Arduino IDE’s built-in examples. Navigate to the Examples sub-menu. Select the Servo and load the Sweep sketch.

Go ahead and upload the sketch. The shaft of your servo motor will begin to sweep back and forth across 180 degrees.
#include <Servo.h>
int servoPin = 9;
Servo servo;
int angle = 0; // servo position in degrees
void setup() {
servo.attach(servoPin);
}
void loop() {
// scan from 0 to 180 degrees
for(angle = 0; angle < 180; angle++) {
servo.write(angle);
delay(15);
}
// now scan back from 180 to 0 degrees
for(angle = 180; angle > 0; angle--) {
servo.write(angle);
delay(15);
}
}
Code Explanation:
Controlling servos is not a simple task, but fortunately, the Arduino IDE already includes a wonderful library called Servo. It contains simple commands that can be used to quickly instruct the servo to rotate to a specific angle.
Therefore, at the beginning of the sketch, we include this library.
#include <Servo.h>
Then we declare the Arduino pin to which the servo motor’s control pin is connected.
int servoPin = 9;
The line below creates a servo object.
Servo servo;
You can actually define up to eight servos in this way; for example, if you had two servos, you would write:
Servo servo1;
Servo servo2;
The variable angle
is used to store the servo’s current angle in degrees.
int angle = 0;
In the setup function, we use the following command to link the servo
object to the control pin of our servo:
servo.attach(servoPin);
The loop function has two for
loops. The first loop will rotate the motor in one direction, while the second will rotate it in the opposite direction.
The servo.write(angle)
function instructs the servo to update its position to the specified angle.
servo.write(angle);
Troubleshooting
Sometimes, your servo may misbehave, especially if you decide to operate it directly from the Arduino. The reason for this is that the servo consumes a significant amount of power, particularly during startup, which can cause the Arduino board to reset.
To solve this problem, place a relatively large electrolytic decoupling capacitor (470µF – 1000µF) across the input power. Make sure the capacitor’s longer lead is connected to 5V and the negative lead is connected to GND.
The capacitor stores electric charge, so when the motor starts, it draws power from both the Arduino supply and the capacitor, ensuring a smooth flow of current.

Arduino Example 2 – Controlling Servo with a Potentiometer
Our next example involves including a potentiometer so that we can manually adjust the servo’s position. This project can be extremely helpful when controlling the pan and tilt of a sensor connected to a servo.
Wiring
We’ll reuse the wiring from the previous example, but this time we’ll add a 10KΩ potentiometer. Connect one end of the pot to ground, the other to 5V, and the wiper to analog input A0.

Arduino Code
The code for making the servo follow the position of the knob is simpler than the code for making it sweep.
#include <Servo.h>
int potPin = 0;
int servoPin = 9;
Servo servo;
void setup() {
servo.attach(servoPin);
}
void loop() {
int reading = analogRead(potPin);
int angle = map(reading, 0, 1023, 0, 180);
servo.write(angle);
}
Code Explanation
Notice that a new variable named potPin
has been introduced.
int potPin = 0;
We start the loop function by reading the value from analog pin A0.
int reading = analogRead(potPin);
The analogRead()
function returns a value between 0 and 1023. However, we must scale it down because the servo can only rotate 180 degrees.
One method is to use the Arduino map() function, which remaps a number from one range to another. The line below converts the reading to degrees between 0 and 180.
int angle = map(reading, 0, 1023, 0, 180);
Finally, we use the write()
command to update the servo’s position to the angle selected by the potentiometer.
servo.write(angle);