Introduction to Interface Servo Motor with Arduino
How to Use Arduino for Servo Motor Control: A Step-by-Step Guide Servo motors play a key role in robotics, automation, and electronic projects, providing precise control over angular
or linear position, velocity, and acceleration. Coupled with an Arduino, controlling a servo motor becomes straightforward, enabling hobbyists and professionals to create innovative projects. This tutorial will guide you through the basics of servo motors, setting up your Arduino for servo control, and building a simple project using the Arduino IDE. By the end, you’ll have a working understanding of servo motor control using Arduino and a complete setup for your own projects.In this tutorial, we’ll explore the functionality of servo motors and how to interface them with the Arduino UNO. You’ll learn to control their movement using both the built-in Servo library in the Arduino IDE and through manual coding without the library. Additionally, we’ll cover how to create a Servo Tester.
What is a Servo Motor?
A servo motor is a rotary actuator designed to rotate or move to a specific position with high precision. It allows for accurate control of angular position, velocity, and acceleration, making it essential in robotics, automation, and other electronics applications. A servo motor consists of the following key components:

- Small DC Motor: Provides the basic rotary motion.
- Gear Train: Reduces the motor’s speed while increasing torque for precise control.
- Feedback Sensor: A potentiometer or encoder monitors the current position of the motor.
- Control Circuitry: Compares the current position with the desired position and adjusts the motor’s movement accordingly.
- Output Shaft: Transfers the motor’s motion to the load.
The feedback sensor continuously reports the motor’s current position to the control system. The control system, using a mechanism known as a servomechanism (or closed-loop control system), compares the current position to the desired position. If there is any discrepancy, the control system adjusts the motor’s position to correct the difference, ensuring the motor accurately reaches and maintains the desired position.

Servo Motor Working

A servo motor operates on the principle of a closed-loop control system, where feedback is used to ensure precise control over position, speed, and acceleration. The working mechanism of a servo motor involves several key components and steps:
- Command Input: A control signal is sent to the servo motor, typically in the form of a Pulse Width Modulation (PWM) signal, which encodes the desired position or speed.
- DC Motor and Gear Train: The internal DC motor generates rotational motion, while the gear train reduces its speed and increases torque for more controlled and powerful movement.
- Feedback Sensor: A sensor, such as a potentiometer or encoder, continuously measures the current position of the motor’s output shaft.
- Control Circuit: The control circuitry compares the measured position from the feedback sensor with the desired position (input signal). If there is a difference (error), the control circuit adjusts the motor’s movement to reduce this error.
- Correction: The motor keeps adjusting its position until it minimizes the error and the output shaft reaches the desired position.
- Servomechanism (Closed-Loop Control): This entire process of comparing the current position to the target position and making adjustments in real-time is called servomechanism. The closed-loop feedback system ensures high precision, making servo motors ideal for tasks requiring accurate position control.
By constantly adjusting itself based on feedback, the servo motor achieves precise control, making it invaluable in applications like robotics, automated systems, and radio-controlled devices.

1. Understanding Servo Motors
Servo motors are essential components for precise control in various applications, from robotics to radio-controlled vehicles. Unlike regular motors, servo motors are equipped with a feedback system that provides information about their position. This feedback allows the motor to rotate to a specified angle.

Types of Servo Motors:
- Positional Rotation Servo: Controls the position in a range of 0 to 180 degrees.
- Continuous Rotation Servo: Allows for full 360-degree rotation and is ideal for projects requiring constant motion.

Servo motors work using Pulse Width Modulation (PWM), which involves sending timed electrical pulses to determine the motor’s position. A signal pulse duration of 1 millisecond moves the motor to one end, while a 2-millisecond pulse moves it to the other end. Servo motors are integral to projects requiring precise and reliable control.
2. Getting Started with Arduino
Arduino is an open-source electronics platform that makes hardware and software accessible to everyone. The Arduino IDE (Integrated Development Environment) is a tool used for writing, compiling, and uploading code to Arduino boards. By using the IDE, you can easily control devices such as servo motors.
Setting up Arduino to control a servo motor involves programming it using simple instructions. Arduino’s versatility makes it the ideal platform for beginners and experts alike to develop projects quickly.
- To begin:
- Download the Arduino IDE from the official website.
- Connect your Arduino to the computer via USB.
- Make sure the correct Arduino board is selected from the Tools menu.
3. Required Components
For this project, you’ll need the following components:
- Arduino Board (e.g., Arduino Uno)
- Servo Motor (SG90 or similar)
- Jumper wires
- Breadboard (optional)
- External power supply (if needed)
Ensure that the servo motor is compatible with Arduino and capable of receiving control signals through PWM. The setup allows you to create a range of servo motor control projects, from simple movements to more complex robotics applications.
4. Wiring the Servo Motor

Wiring the servo motor to your Arduino is simple. You only need three connections:
- Power (VCC): Connect the power wire of the servo motor (usually red) to the 5V pin on the Arduino.
- Signal (PWM Control): Connect the control wire (usually yellow or white) to a PWM-enabled pin on the Arduino (e.g., pin 9).
- Ground (GND): Connect the ground wire of the servo motor (usually brown or black) to one of the GND pins on the Arduino.

Make sure to double-check your wiring before powering the Arduino to avoid damaging the components. Incorrect wiring could lead to erratic behavior or no movement from the servo motor.
5. Arduino Libraries for Servo Control
The Servo library in the Arduino IDE simplifies controlling servo motors. This library allows you to manage up to 12 motors on most Arduino boards and up to 48 on the Arduino Mega.
To include the Servo library:
- Open the Arduino IDE.
- Navigate to Sketch > Include Library > Servo.
- The library will now be available for use in your code.
The Servo library provides functions such as .attach()
to assign a PWM pin to the servo motor and .write()
to set the motor angle (0–180 degrees). This makes servo motor control straightforward, even for beginners.
6. Example Code in order to move a Servo Motor back and forth
Here’s a basic code example to move a servo motor back and forth using Arduino.
/*
Interfacing Micro Servo Motor with Arduino without using servo library
by www.PiEmbSysTech.com
*/
#include <Servo.h>
#define SERVO_PIN 10
Servo myservo; // create servo object to control a servo
int pos = 0; // variable to store the servo position
void setup() {
myservo.attach(SERVO_PIN); // attaches the servo on pin 9 to the servo object
myservo.write(pos); // go to position zero first
delay(2000); // wait for some time
}
void loop() {
delay(1000); // delay of 1 second before start
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15 ms for the servo to reach the position
}
delay(1000); // delay of 1 second before changing direction of rotation
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15 ms for the servo to reach the position
}
}
- This code:
- Includes the Servo library.
- Sets the servo motor to pin 10.
- Moves the motor to 180 degrees and then back to 0 degrees in a loop.
7. Applications of Servo Motors with Arduino
Servo motors are widely used in applications that require precise control over position. Here are some popular applications where Arduino and servo motors shine:
- Robotics: Servo motors control joint movements in robot arms.
- RC Vehicles: Used to control steering mechanisms.
- Automated Doors: Servo motors automate the opening and closing of doors.
- Camera Pan/Tilt Systems: Servo motors move cameras to desired angles.
- DIY Projects: Many hobbyists use Arduino and servo motors for small DIY projects involving automation or movement.
With the versatility of Arduino and the precision of servo motors, the possibilities are endless for integrating them into real-world applications.
8. Troubleshooting Common Issues
When working with servo motors, you may encounter a few common issues:
- Servo Jittering: This can happen due to noise in the power supply. Make sure to provide a stable power source, especially if you are using multiple servos.
- Servo Not Moving: Check your wiring and ensure the servo motor is connected correctly. Also, verify that the correct pin is assigned in the code.
- Servo Overheating: Continuous heavy loads can cause the motor to overheat. Provide breaks in your code to prevent overheating.
What is SG-90 Servo Motor?
The SG90 Servo Motor is one of the most popular and commonly used servo motors, especially in hobby projects, DIY electronics, and educational robotics. It is a compact and lightweight servo, making it ideal for small applications where space and weight are constraints. People widely use the SG90 because of its affordability, ease of use, and versatility.
Key Features of SG-90 Servo Motor:
- Size and Weight: The SG90 is a micro servo, typically measuring about 22.2mm x 11.8mm x 31mm and weighing around 9 grams.
- Rotation Range: It has a rotation range of 0 to 180 degrees, allowing for precise positioning within that angle.
- Torque: Despite its small size, the SG90 provides a torque of about 1.8 kg/cm at 4.8V, which is sufficient for light-duty tasks like controlling levers, arms, or wheels.
- Operating Voltage: It operates between 4.8V to 6V, making it compatible with most microcontrollers, including Arduino and Raspberry Pi.
- PWM Control: The motor is controlled using Pulse Width Modulation (PWM), where the width of the pulse determines the position of the servo.
Applications of SG-90 Servo Motor:
- Robotics: Used in robotic arms and legs for movement.
- RC Vehicles: For controlling steering and throttle in cars or planes.
- DIY Projects: Ideal for various Arduino and Raspberry Pi projects where small and precise movements are required.
The SG90 servo motor is a reliable and easy-to-use component for anyone looking to incorporate movement into their projects without complex setup or programming.
Interfacing Arduino with Servo Motors
In this section, we will interface an Arduino UNO with the SG90 micro servo motor to demonstrate how to control the motor’s movements with precision.
Hardware and Software Requirements
1. Hardware
Component Name | Quantity | Remarks |
Arduino UNO R3 | 1 | Revision R3 |
Micro Servo Motor | 1 | SG 90 |
Connection wires | 3 | For Motor and Arduino connections |
12V Supply Adapter | 1 | For providing power to Arduino |
2. Software
- Arduino IDE, version 2.3.3 or above installed on your PC
- Servo Library by Arduino.
Commonly Used Functions of Servo Library
Let’s now explore the functions offered by the Servo library in Arduino:
Servo Class
Create an instance of the Servo class to represent the servo motor.
Syntax:
Servo servo;
Parameter:
servo: An instance of the Servo class
attach()
This function attaches the servo motor to a designated pin on the Arduino board, creating a communication link between the Arduino and the servo motor.
Syntax:
servo.attach(pin)
servo.attach(pin, min, max)
Parameters:
servo: An instance of the Servo class.
pin: The pin connects to the control wire (orange) of the servo motor.
min (optional): The pulse width in microseconds for the minimum position (0 degrees) of the servo (default is 544).
max (optional): The pulse width in microseconds for the maximum position (180 degrees) of the servo (default is 2400).
write()
This function directs the servo motor to move to a specified angle. The angle parameter, measured in degrees, typically ranges from 0 to 180, representing the motor’s full range of motion. When you call the function with a specific angle, the servo motor adjusts its position to match that value.
Syntax:
servo.write(angle)
Parameters:
- servo: An instance of the Servo class.
- angle: The position value for the servo, ranging from 0 to 180 degrees.
writeMicroseconds()
This function offers a more accurate method for controlling the servo motor’s position. Instead of directly setting the angle, you define the target position by specifying the pulse width in microseconds. This approach enables more precise and fine-tuned control over the servo motor’s movements.
Syntax:
servo.writeMicroseconds(us)
Parameters:
- servo: A Servo object representing the servo motor.
- us: An integer specifying the pulse width in microseconds.
read()
This function retrieves the current angle of the servo motor and returns it in degrees. It is helpful when you need to determine the servo motor’s position for feedback or control purposes.
Syntax:
servo.read()
Parameters:
- servo: A Servo object representing the servo motor.
- Returns: The current angle of the servo, ranging from 0 to 180 degrees.
attached()
This function checks if the servo motor currently connects to a pin on the Arduino board. It returns true if the servo attaches and false if it does not.
Syntax:
servo.attached()
Parameters:
- servo: A Servo object representing the servo motor.
- Returns: true if the servo connects to a pin; false otherwise.
detach()
This function disconnects the servo motor from the pin it is currently attached to. Once you detach the servo motor, it will no longer respond to commands from the Arduino until you reattach it using the attach() function.
Syntax:
servo.detach()
Parameter:
servo: A Servo object representing the connected servo motor.
Interfacing Arduino with Servo Motor
In the wiring diagram, a micro-servo motor connects to the Arduino UNO. The servo’s power pin connects to the Arduino’s Vcc using a red wire. The ground pin connects to the Arduino’s Gnd pin with a black wire. The control pin connects to pin 9 of the Arduino through its PWM pin.

Example Code
In the following code, we will control the position of the servo arm from 0 to 180 degrees using the Arduino UNO. A new function will be written to manage the movement of the servo motor.
/*
Interfacing Micro Servo Motor with Arduino without using Library
by www.PiEmbSysTech.com
*/
#define PULSE_TIME 20000 // in microseconds
#define SERVO_PIN 9 // Pin for communicating with servo motor
int pos = 0; // variable to store the servo position
void servo_write(unsigned char angle);
void setup() {
pinMode(SERVO_PIN,OUTPUT); // set the mode of Servo pin as output
for(int i=0; i<30; i++){ // set angle to zero
servo_write(pos);
delay(30);
}
delay(1000); // wait for 1 sec
}
void loop() {
delay(1000); // delay of 1 second before start
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees in steps of 1 degree
servo_write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15 ms for the servo to reach the position
}
delay(1000); // delay of 1 second before changing direction of rotation
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
servo_write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15 ms for the servo to reach the position
}
}
void servo_write(unsigned char angle)
{
unsigned int highPulseTime;
unsigned int lowPulseTime;
// high pulse time should vary from 544 microseconds to 2000 microseconds to change angle from 0 to 180 degree
highPulseTime = map(angle,0,180,544,2400);
// set low pulse time by subtracting high pulse time from 20000 microseconds or 20 milliseconds as this is the time period of pulse for Servo Motor
lowPulseTime = PULSE_TIME - highPulseTime;
// Now create a 50 Hz pulse
digitalWrite(SERVO_PIN,HIGH);
delayMicroseconds(highPulseTime); // High time
digitalWrite(SERVO_PIN,LOW);
delayMicroseconds(lowPulseTime); // Low time
}
What is a Servo Tester?
A servo tester is a device used to test and calibrate servo motors. It typically consists of a small electronic unit with knobs or buttons, allowing users to manually adjust the servo motor’s position and test its responsiveness. Servo testers usually feature adjustable pulse width modulation (PWM) signals, which control the motor’s position, speed, and direction.
By connecting the servo motor to the tester, you can adjust the settings and observe the motor’s response. This enables you to verify that the servo motor is working properly and fine-tune its performance for specific applications.
Making a Servo Tester using Arduino UNO
In this section, we will build a Servo Tester using an Arduino to test a servo motor. We will use a 10 k-Ohm potentiometer as the knob to adjust the position or angle of the servo arm. The middle pin of the potentiometer (POT) connects to Analog input A0. As you rotate the knob, the servo moves accordingly, and the current angle displays on an LCD. If the angle of the servo arm changes in sync with the angle shown on the LCD, the servo motor functions properly; otherwise, it may be faulty.
Hardware and Software Requirements
Hardware
Component Name | Quantity | Remarks |
Arduino UNO R3 | 1 | Revision R3 |
Micro Servo Motor | 1 | SG 90 |
LCD | 1 | 16×2, to display Servo Arm Angle |
Resistance | 1 | 220 Ω for using with LCD |
POT | 2 | 10k Ω |
Breadboard | 1 | Full Size |
12V Supply Adapter | 1 | For providing power to Arduino |
Connection wires | 25 | For breadboard connections |
USB Cable Type A to B | 1 | for programming Arduino UNO |
Software
- Arduino IDE (Version 2.3.3 or later) installed on your PC.
- Servo Library by Arduino.
- LiquidCrystal Library by Arduino (Version 2.3.2) installed in the Arduino IDE.

In the wiring diagram of the Servo Tester, a 10K Ohm potentiometer is used to control the position of the servo motor’s arm via the Arduino UNO. The current angle of the arm, relative to its starting position, is displayed on the LCD.
The potentiometer’s knob, which adjusts the servo, is connected to Analog input A0, while the servo motor’s control pin is connected to pin 10 of the Arduino UNO. The remaining two pins of both the servo motor and the potentiometer are connected to the VCC and Ground pins of the Arduino.
The connections between the Arduino and the LCD are outlined in the following table:
LCD and Arduino Connections
Sl. No. | LCD Side Pin | Arduino Side Connection |
1. | VSS/GND (Pin 1) | Connected to Ground |
2. | VDD/VCC (pin 2) | Connected to 5V |
3. | VEE/Vo (Pin 3) | Connected to Variable pin of 10k POT to Control Contrast of LCD |
4. | RS (Pin 4) | pin 12 |
5. | R/W (pin 5) | Connected to Ground |
6. | E (Pin6) | pin 11 |
7. | D0 (pin 7) | Connected to Ground |
8. | D1 (pin 8) | Connected to Ground |
9. | D2 (pin 9) | Connected to Ground |
10. | D3 (pin 10) | Connected to Ground |
11. | D4 (pin 11) | pin 6 |
12. | D5 (pin 12) | pin 7 |
13. | D6 (pin 13) | pin 8 |
14. | D7 (pin 14) | pin 9 |
15. | LED(+)(Pin 15) | Connected to VCC via 220 Ohm resistor |
16. | LED(-)(Pin 16) | Connected to Ground |
Arduino Code
/*
Controlling the Angle of Micro Servo Motor Arm using Arduino and a Potentiometer.
by www.PiEmbSysTech.com.
In this code the Angle of the Servo Motor is controlled by the POT and it will be displayed on the LCD.
*/
#include <Servo.h>
#include <LiquidCrystal.h>
// We are using JHD 16x2 alphanumeric LCD using HD44780 controller for its controller
// initialize the library with the numbers of the interface pins
// Here We initialize LCD in 4-bit mode when R/W pin (Pin 5 of LCD) is permanently attached to GND
// LCD side / Arduino Side Pin
// (Pin 4 )RS = 12
// (Pin 6 )E = 11
// (Pin 7 )D0 (pulled to GND) = No connection with Arduino
// (Pin 8 )D1 (pulled to GND) = No connection with Arduino
// (Pin 9 )D2 (pulled to GND) = No connection with Arduino
// (Pin 10)D3 (pulled to GND) = No connection with Arduino
// (Pin 11)D4 = 6
// (Pin 12)D5 = 7
// (Pin 13)D6 = 8
// (Pin 14)D7 = 9
// Pin No 1 and 16 of LCD of should be connected to GND
// Pin No 2 and 15 of LCD of should be connected to VCC
// A 10k pot should be connected b/w VCC and GND and its variable o/p pin should be connected to VEE (pin 3 of LCD) to control contrast of LCD.
LiquidCrystal lcd(12, 11, 6,7,8,9);
#define SERVO_PIN 10
#define POT_PIN A0
Servo myservo; // create servo object to control a servo
int currentAngle; // variable to store the servo position
int potReading; // variable to save data from Analog input A0
void setup() {
myservo.attach(SERVO_PIN,544,2400); // attaches the servo on pin 9 to the servo object
lcd.begin(16, 2); // set up the LCD's number of columns and rows:
lcd.setCursor(0, 0); // set the cursor to (column = 0,row = 0)
lcd.print("Arm Angle :");
}
void loop() {
potReading = analogRead(POT_PIN); // Read the analog INput
currentAngle = map(potReading,0,1023,0,180);// Map the Analog Input data with the possible Servo Angle
myservo.write(currentAngle); // tell servo to go to position in variable 'currentAngle'
// Print data on LCD
lcd.setCursor(0, 1);
lcd.print(currentAngle);
lcd.print(" degrees ");
// Here provide 5ms delay before next reading
delay(5);
}
Discover more from PiEmbSysTech
Subscribe to get the latest posts sent to your email.