• Usha Saha


Updated: Sep 23, 2020

The current era is the period of technology that would make things easy and simple. This article of interfacing a servo motor with #Arduino is also one such example. A servomotor can drive or revolve an object. That is if one wants to rotate an object at some specific angles or distance, then the servo motor comes in use. Servo motors find applications in toy cars, RC helicopters, and planes, Robotics, Machine, etc. The article covers the working and configuration of servo motor with Arduino in detail.

List of components used:

1. Servo motor

A servo motor is actually a rotary actuator which gives a precise control in terms of its angular position, acceleration, and velocity. A servo motor works on the principle of PWM (Pulse width modulation). This implies that depending on the width of the pulse that is applied the rotor will turn in the desire position. For this interfacing, an SG90 servo motor is used. The white piece in the picture is a rotor and is controlled by a bunch of gears inside. If a DC source powers the motor then it is called a DC servo motor and if powered by an AC source then it is called an AC servo motor.

SC90 Servo Motor Image
SG90 Servo Motor Image

2. Arduino Board

Arduino Board can read inputs such as light on a sensor, a finger on a button, or a Twitter message and convert it to an output such as activating a motor, turning on an LED, publishing something online. All of this can be done by sending a set of instructions to the microcontroller on the board by the use of the Arduino #programming language.


Circuit Diagram

The circuit below shows the connection of an #SG90 servo motor with the Arduino board. There are three wires of the servo motor. The brown wire is the ground wire, the red one goes to the 5 volts, and the orange one is called the #PWM wire which is connected to the digital line.

Circuit Diagram with SC90
Circuit Diagram with SG90


This is the code that has been injected into the Arduino.

#include <Servo.h>

In the first line, the servo library is included.

Servo servo_test; 

The Servo motor is initialized.

int angle = 0;

In the third line, an integer is initialized namely angle, and set to zero in the beginning.


In the next step under void setup, communication is set up with the Arduino. Conveying that PWM or the signal pin of the servo motor is attached to pin 2 of the argument.

for(angle = 0; angle < 180; angle += 1) 

Under the next void loop, there are two for loops one for the rotation of the servo motor in one particular direction and the other for the same in the opposite direction. Since the angle has been already initialized in the third step, therefore, the first for loop is executed.


For the next step, the rotor of the servo motor is commanded to move at a particular angle.


In the next step, a delay of 15ms is added.


Once the first for loop is completed, that is by increasing the angle by +1 for each step when the angle finally reaches 180. For the next step, a delay of 1 sec is added, and the program jump in the second for loop.

for(angle = 180; angle>=1; angle-=5)

In the second for loop, a kind of reverse operation is done. That is an angle of 180 is set up and every time is reduced by 5.


Again the rotor is made to move in a particular direction.


In the next step, a delay of 5ms(millisecond) is executed.


Once the second for loop is completed, that is when the angle reaches zero again a delay of 1 sec is executed, and the process moves back to loop number one. And this process continues.

#include <Servo.h>
Servo servo_test;        //initialize a servo object for the connected 
int angle = 0;
void setup() 
// put your setup code here, to run once:
servo_test.attach(2);      // attach the signal pin of servo to pin2 of 
void loop() {
// put your main code here, to run repeatedly:
for(angle = 0; angle < 180; angle += 1)    // command to move from 0 
                                              degrees to 180 degrees 
servo_test.write(angle);                 //command to rotate the servo 
                                           to the specified angle
for(angle = 180; angle>=1; angle-=5)     // command to move from 180 
                                            degrees to 0 degrees 
servo_test.write(angle);              //command to rotate the servo to 
                                         the specified angle

Download the codes from here:

servo code in notepad
TXT • 972B


A circuit diagram image presenting the view before the codes are executed.

Circuit image  at switch off state
Circuit image at switch off state

Once the codes are connected, the rotor of the #servo motor (one in white color ) starts to move in a particular direction at a slow speed since the angle is reduced by 1. while coming back the rotor moves at a fast speed as the angle here is reduced by 5. In this way, the rotor keeps moving in a too and fro motion. One can try for different degrees of angle for a better understanding.

Circuit image  at switch  ON state
Circuit image at switch ON state

Video explanation of the interfacing for a better understanding.

The article covers the basic interfacing of servo #motor with Arduino. With this essential concept, one can build may other projects as servo motor finds numerous applications in home electronics, toys, robotics, airplane, etc.

See also:

  1. Interfacing Relay Module with Arduino

  2. Interfacing Water/Rain Sensor with Arduino

  3. Interfacing servo motor with RPi