
How to Control a Servo Motor (SG90) with Arduino
A servo motor is one of the most useful components for beginners learning Arduino, robotics, and electronics. Unlike a regular DC motor that continuously rotates, a standard servo motor can move its shaft to a specific angle, making it ideal for projects that require controlled movement.
The SG90 micro servo motor is especially popular because it is small, lightweight, affordable, and easy to use with an Arduino UNO.
You can use an SG90 servo to build a robot arm, automatic dustbin, obstacle-avoiding robot, smart door lock prototype, or a rotating sensor mount.
In this beginner-friendly guide, you will learn how an SG90 servo works, how to connect it to Arduino, how to write the code, and how to control its movement from 0° to 180°.
1. What Is an SG90 Servo Motor?
The SG90 is a small 9g hobby servo motor commonly used in educational electronics, DIY robotics, and Arduino projects.
A standard positional SG90 typically rotates through an approximate 0° to 180° range. The exact usable movement can vary between manufacturers and individual servos.
Unlike a standard DC motor, a servo contains:
-
A small DC motor
-
A gear mechanism
-
A position-sensing potentiometer
-
An internal control circuit
These parts allow the servo to move its output shaft to a commanded position and hold that position against a suitable load.
Common applications include:
-
Robot arms and grippers
-
Automatic dustbins
-
Obstacle-avoiding robots
-
Pan-and-tilt camera mounts
-
Model aircraft control surfaces
-
Automatic door and gate prototypes
2. How Does a Servo Motor Work?
An Arduino controls a standard hobby servo by sending repeated electrical pulses through its signal wire.
The pulse duration tells the servo which position to move towards. The internal control circuit compares the requested position with the actual shaft position and drives the motor accordingly.
For a typical positional servo:
| Approximate Position | Typical Control Pulse |
|---|---|
| 0° | 1,000 microseconds |
| 90° | 1,500 microseconds |
| 180° | 2,000 microseconds |
These are illustrative values, not guaranteed SG90 calibration limits. The exact pulse range varies by model.
The Arduino Servo library handles the signal timing, so beginners do not need to generate the pulses manually.
3. Components Required
| Component | Quantity | Purpose |
|---|---|---|
| Arduino UNO | 1 | Main controller |
| SG90 Servo Motor | 1 | Controlled movement |
| Jumper Wires | 3 | Electrical connections |
| USB Cable | 1 | Uploading Arduino code |
| Regulated 5V Supply | 1 | Servo power when required |
For a simple unloaded demonstration, you can begin with one SG90 servo. For more reliable operation, use a suitable regulated external 5V supply rather than relying on the Arduino board‘s 5V output to handle servo current peaks.
4. Understanding the SG90 Servo Motor Wires
The SG90 servo motor usually has three wires:
| Wire Colour | Connection | Function |
|---|---|---|
| Brown or Black | GND | Ground |
| Red | 5V | Power supply |
| Orange or Yellow | Digital Pin 9 | Control signal |
Wire colours can vary between manufacturers, so check the markings and documentation of your particular servo before connecting it.
5. SG90 Servo Motor Wiring with
Connect the SG90 servo to Arduino UNO using the following pinout:
| SG90 Servo Wire | Arduino UNO |
|---|---|
| Brown / Black (GND) | GND |
| Red (VCC) | Regulated 5V supply |
| Orange / Yellow (Signal) | D9 |
Power supply recommendation:
-
Use a regulated 5V supply suitable for the servo’s current requirements.
-
A separate 5V supply rated around 1A is a practical starting point for one SG90.
-
Connect the external supply’s GND to Arduino GND.
-
Connect the servo’s red wire to the external regulated 5V positive output.
A single lightly loaded SG90 may work from the Arduino 5V rail in a simple test, but the servo can draw current spikes when starting, moving, or encountering resistance. An external supply helps prevent voltage drops and unexpected Arduino resets. Never connect an unregulated battery voltage directly to the servo’s 5V wire.
6. Arduino Code to Control SG90 from 0° to 180°
Now that the wiring is complete, upload the following Arduino code.
This program moves the servo gradually from 0° to 180° and then returns it to 0°.
Complete Arduino Code:
#include <Servo.h>
Servo myServo;
void setup() {
myServo.attach(9); // Servo signal connected to D9
}
void loop() {
// Move from 0 to 180 degrees
for (int angle = 0; angle <= 180; angle++) {
myServo.write(angle);
delay(15);
}
delay(500);
// Move back from 180 to 0 degrees
for (int angle = 180; angle >= 0; angle--) {
myServo.write(angle);
delay(15);
}
delay(500);
}
How the code works:
-
#include <Servo.h>includes the Arduino Servo library. -
Servo myServo;creates a servo control object. -
myServo.attach(9);assigns the servo signal to digital pin 9. -
myServo.write(angle);commands the servo to move to a specified angle. -
delay(15);gives the servo time to move between positions. -
The two
forloops move the servo forward and backward.
The Servo library is included with the standard Arduino IDE installation, so you normally do not need to install an additional library. You can also find the built-in example under File → Examples → Servo → Sweep.
Arduino Documentation+1
7. How to Move the Servo to a Specific Angle
You do not always need to sweep the servo between 0° and 180°. You can command it to move directly to a particular position.
For example:
#include <Servo.h>
Servo myServo;
void setup() {
myServo.attach(9);
myServo.write(0); // Move to 0 degrees
delay(1000);
myServo.write(90); // Move to 90 degrees
delay(1000);
myServo.write(180); // Move to 180 degrees
}
void loop() {
// Servo stays at the last commanded position
}
| Command | Servo Position |
|---|---|
myServo.write(0); |
0° |
myServo.write(45); |
45° |
myServo.write(90); |
90° |
myServo.write(135); |
135° |
myServo.write(180); |
180° |
If your servo makes a buzzing noise or pushes against its mechanical limit, reduce the commanded range. Do not force the horn beyond its safe travel.
8. Control SG90 Servo Using a Potentiometer
Once you understand basic servo movement, you can build a more interactive project using a potentiometer.
A potentiometer allows you to control the servo angle by turning a knob.
Additional Component:
– 10kΩ potentiometer – 1 piece
Wiring:
| Component | Arduino UNO |
|---|---|
| Servo Signal | D9 |
| Servo Red Wire | Regulated 5V |
| Servo GND | Common GND |
| Potentiometer Outer Pin 1 | 5V |
| Potentiometer Outer Pin 2 | GND |
| Potentiometer Middle Pin | A0 |
Arduino Code:
#include <Servo.h>
Servo myServo;
int potPin = A0;
int potValue = 0;
int angle = 0;
void setup() {
myServo.attach(9);
}
void loop() {
potValue = analogRead(potPin);
angle = map(potValue, 0, 1023, 0, 180);
myServo.write(angle);
delay(15);
}
How it works:
-
Turning the potentiometer changes its analog voltage.
-
Arduino reads the voltage using
analogRead(A0). -
The
map()function converts the reading into an angle between 0° and 180°. -
The servo moves to the requested position.
This is a great beginner project for understanding analog inputs and servo control.
9. Practical Projects Using an SG90 Servo Motor
After learning how to control an SG90 servo, you can use it in many DIY electronics and robotics projects.
| Project | How the Servo Is Used |
|---|---|
| Automatic Dustbin | Opens and closes the lid |
| Ultrasonic Radar | Rotates the ultrasonic sensor |
| Robot Arm | Moves a lightweight arm or gripper |
| Smart Door Lock | Operates a small lock prototype |
| Obstacle-Avoiding Car | Rotates the sensor to scan directions |
| Camera Mount | Changes camera viewing angle |
The SG90 is suitable for lightweight mechanisms. For heavier loads or applications requiring greater torque, choose a servo designed for that job.
10. Common SG90 Servo Motor Problems and Solutions
| Problem | Possible Cause | Solution |
|---|---|---|
| Servo does not move | Incorrect wiring | Check signal, power and ground |
| Servo vibrates or jitters | Unstable power supply | Use a suitable regulated 5V supply |
| Arduino keeps restarting | Servo current spikes | Use a separate servo supply and common ground |
| Servo moves in the wrong direction | Incorrect angle mapping or mounting | Adjust code or horn position |
| Servo makes buzzing noise | Mechanical obstruction or endpoint pressure | Remove obstruction and reduce angle range |
| Servo moves only slightly | Incorrect signal or unsuitable pulse range | Check wiring and servo compatibility |
| Servo becomes hot | Excessive load or stalled movement | Disconnect power and inspect the mechanism |
11. Important Safety Tips
-
Check the SG90’s operating voltage before connecting its power wire.
-
Do not connect the servo directly to a 9V battery.
-
Never connect the servo’s red wire to Arduino’s 9V VIN pin.
-
Use a regulated 5V supply with sufficient current capacity.
-
Connect the external supply ground to Arduino GND.
-
Do not force the servo horn by hand while powered.
-
Avoid keeping the servo stalled against an object.
-
Check wiring polarity before switching on the circuit.
-
Keep fingers and wires away from moving gears and mechanisms.
12. Where to Buy SG90 Servo Motors and Arduino Components in Pakistan?
If you are building Arduino, robotics, or automation projects in Pakistan, having the right electronic components makes your work easier.
At Electroway.org, students, hobbyists, engineers, and electronics enthusiasts can explore components for:
-
Arduino boards and microcontrollers
-
Servo motors and DC motors
-
Sensors and electronic modules
-
Robotics and automation projects
-
IoT and smart solutions
-
Electronic tools and accessories
Explore Arduino boards and accessories and other electronics components at Electroway.org.
You can also use the Electroway app as part of your electronics shopping experience. Check product specifications, operating voltage, and compatibility before purchasing.
13. Frequently Asked Questions (FAQs)
Q1. What is an SG90 servo motor?
The SG90 is a lightweight 9g hobby servo motor commonly used in Arduino, robotics, and DIY electronics projects. It provides controlled angular movement.
Q2. Can I connect an SG90 servo directly to Arduino UNO?
Yes, you can connect its signal wire to an Arduino digital pin. However, for reliable operation, use a suitable regulated 5V supply for servo power, with a common ground.
Q3. Which Arduino pin is used for SG90?
Digital pin D9 is commonly used for the servo signal. The Arduino Servo library can also control a servo from other supported digital pins.
Q4. What is the operating voltage of an SG90 servo?
Many SG90 versions operate around 4.8V–6V. Always verify the specifications of your particular servo.
Q5. How many degrees can an SG90 servo rotate?
A standard positional SG90 is commonly advertised for approximately 180° of movement, although the actual usable range varies by model.
Q6. Which library is required to control an SG90 servo?
The Arduino Servo.h library is commonly used. It is included with the standard Arduino IDE installation.
Q7. Can I control multiple SG90 servo motors with Arduino?
Yes. You can control multiple servos using separate signal pins, subject to the board’s library and hardware limitations. Use an appropriately rated external power supply for the servos.
Q8. Can an SG90 servo rotate continuously?
A standard SG90 is a positional servo, not a continuous-rotation motor. Modified continuous-rotation versions may operate differently and usually control speed and direction rather than a specific angle.
