Arduino STEM DIY (Session 5): Control an SG90 Servo Motor with Arduino
Share
In the previous lessons, we learned how to read sensor data with Arduino and display measurements on an OLED screen. In this lesson, we will take the next step and control physical movement using an Arduino Uno R3 and an SG90 9g micro servo motor. The servo will move to programmed target positions and sweep back and forth.
The compact SG90 is a convenient way to learn the basics of servo control. By the end of this lesson, you will understand the three servo wires, the Arduino Servo library, and how to set a target position with write().
What You Will Learn
- Understand how a positional servo motor works.
- Identify the power, ground, and signal wires.
- Use the Arduino Servo library to control a servo position.
- Write a simple program that sweeps between two target angles.
- Learn basic servo power and mechanical installation precautions.
Suitable for: Arduino beginners, parents and children, students, and makers
Estimated time: 20–30 minutes, depending on experience and wiring speed
Difficulty: Beginner
Prerequisite: Basic familiarity with Arduino Uno R3 wiring and uploading a sketch is recommended.
1. Components Required
- Arduino Uno R3 development board — Runs the program and sends the control signal.
- SG90 9g micro servo motor (180° positional type) — Moves to the position specified by the program.
- USB cable — Connects the Arduino to a computer, uploads the sketch, and powers the board.
- Jumper wires / servo three-wire connector — Connects the servo to the Arduino.
Related products:
- Arduino Uno R3 Development Board
- SG90 9g Micro Servo Motor (180°)
- Browse Arduino and STEM DIY Accessories
Setup used in this lesson: One SG90 is tested without an attached mechanical load. The Arduino Uno R3 is powered over USB, and the servo's red power wire is connected to the Arduino 5V pin. This describes our light-load demonstration setup; it should not be treated as a universal power arrangement for every servo or loaded mechanism.
2. What Is a Servo Motor?
A servo motor is designed to move to a commanded position. A typical hobby servo contains a small DC motor, a gear train, and a position-feedback control circuit. Unlike a basic DC motor, which is commonly controlled for direction and speed, a positional servo receives a target-position command and attempts to move its output shaft to that position.
The SG90 used in this lesson is a common 9g micro positional servo, specified as a 180° model. Actual usable travel can vary between units and with mechanical installation. For this demonstration, the program uses a more conservative range of 20° to 160° rather than immediately commanding 0° or 180°.
Note: This is a positional SG90 servo, not a continuous-rotation 360° servo. A continuous-rotation model generally uses the control signal to set direction and speed, so its operation is different.
3. Understanding the Three Servo Wires
The SG90 typically has three wires for power, ground, and the control signal. Common wire colours are:
| Common Wire Colour | Function | Connection in This Lesson |
|---|---|---|
| Orange / Yellow | Signal: control input | Arduino D9 |
| Red | VCC: positive power | Arduino 5V |
| Brown | GND: ground | Arduino GND |
Wire colours can vary between manufacturers or batches. Before connecting the servo, check the product labels, connector pin order, or the wiring definition supplied with your unit rather than relying on colour alone.
4. Wiring to the Arduino Uno R3
The following connections match the actual setup shown in the photos accompanying this article:
| SG90 Wire | Arduino Uno R3 | Purpose |
|---|---|---|
| Yellow / orange signal wire | D9 | Servo control signal |
| Red power wire | 5V | Positive supply |
| Brown ground wire | GND | Power ground |

Power Supply Notes
In this test, one SG90 is operated without an additional mechanical load. The Arduino Uno R3 is powered via USB, and the servo receives power from the Arduino 5V pin. This can be used for a short, light-load beginner demonstration, but a servo may draw higher current momentarily when starting, changing direction, or being blocked.
If the Arduino resets, the servo jitters, USB power becomes unstable, or you later add a mechanical load or multiple servos, use a regulated external power supply that meets the servo's voltage and current requirements. The SG90 product specification lists an operating range of 3.5V–6V DC. With an external 5V supply, connect the servo red wire to the external positive output, the brown wire to external GND, and keep the yellow/orange signal wire connected to Arduino D9. The external supply GND must also be connected to Arduino GND to establish a common ground.
Do not connect the servo power wire to the Arduino 3.3V pin or to an unverified higher-voltage source. Disconnect USB and external power before changing the wiring or supply arrangement.
5. The Arduino Servo Library
The Arduino Servo library simplifies servo control. You create a Servo object, attach it to a signal pin, and use write() to set a target angle. The library generates the control pulses required by the servo.
In the Arduino IDE, open File → Examples → Servo → Sweep to view the official sweep example. The Servo library is normally included with a standard Arduino IDE installation. If the compiler cannot find Servo.h, search for and install the Arduino Servo Library through Library Manager.
6. Complete Arduino Code
The following sketch first moves the SG90 to approximately the middle position at 90°. It then moves from 20° to 160°, pauses briefly, returns to 20°, and repeats.
#include <Servo.h>
Servo myServo;
const int servoPin = 9;
void setup() {
myServo.attach(servoPin);
// Move to the approximate centre position
myServo.write(90);
delay(500);
}
void loop() {
// Sweep from 20 to 160 degrees
for (int angle = 20; angle <= 160; angle++) {
myServo.write(angle);
delay(15);
}
delay(500);
// Return from 160 to 20 degrees
for (int angle = 160; angle >= 20; angle--) {
myServo.write(angle);
delay(15);
}
delay(500);
}
Code Explanation
1. Include the Servo library
#include <Servo.h>
This makes the Arduino Servo library functions available to the sketch.
2. Create a servo object and select the pin
Servo myServo;
const int servoPin = 9;
myServo is the object used to control the servo, and servoPin sets the signal connection to Arduino D9.
3. Attach and initialise the servo
myServo.attach(servoPin);
myServo.write(90);
attach() connects the Servo object to D9. write(90) requests an approximate centre position. This is a commanded angle; the actual mechanical position can vary between servo units and installations.
4. Increase the target angle step by step
for (int angle = 20; angle <= 160; angle++) {
myServo.write(angle);
delay(15);
}
The loop starts at 20 and increases the value by one until it reaches 160. After each angle command, the program waits 15 milliseconds to give the servo time to follow the movement. Actual movement speed depends on the servo and its power supply.
5. Move back in the opposite direction
for (int angle = 160; angle >= 20; angle--) {
myServo.write(angle);
delay(15);
}
This loop decreases the target angle by one at a time, moving the servo from 160° back to 20°.
Technical note: The Servo library generates the control pulses, so you do not need to use analogWrite() to control the SG90. On Arduino Uno boards, attaching a servo also affects the normal analogWrite() PWM functionality on pins 9 and 10 because of timer use. If your project needs other PWM outputs at the same time, plan the pins and control method accordingly.
7. How to Test the Project
- Check that the yellow/orange signal wire is connected to D9, the red wire to 5V, and the brown wire to GND.
- Make sure the servo shaft is not blocked. Do not attach a mechanism that adds a load during the first test.
- Connect the Arduino Uno R3 to your computer using USB.
- Open the Arduino IDE and select the correct board and port.
- Upload the sketch above to the Arduino.
- After uploading, observe whether the servo first moves to the centre position and then sweeps back and forth.
Real Test Video
The video below shows the actual SG90 servo movement controlled by the Arduino Uno R3 during our test.
The sketch commands a target range of 20° to 160°. The video demonstrates the sweeping movement, but it should not be treated as a calibrated angle measurement unless the shaft position has been measured with an angle tool.
8. Troubleshooting and FAQs
1. The servo does not move at all. What should I check?
Check that the red wire is connected to a suitable power source, the brown wire is connected to GND, and the yellow/orange signal wire is connected to D9 as specified in the sketch. Also confirm that the program uploaded successfully and includes #include <Servo.h>.
2. The servo only vibrates or makes a noise but does not move properly. Why?
Possible causes include insufficient power, loose wiring, excessive mechanical load, or a blocked mechanism. Remove any external load and check the connections first. If the problem continues, use a stable external supply that meets the servo's voltage and current requirements, and connect its ground to Arduino GND.
3. Why is the actual movement not exactly 20° to 160°?
write(20) and write(160) are target-angle commands. They do not guarantee that every SG90 will measure exactly those mechanical angles with a protractor. Servo tolerances, gears, installation position, and pulse calibration can all cause differences.
4. Can the SG90 rotate continuously through 360°?
The SG90 used in this lesson is a 180° positional servo, not a continuous-rotation servo. It moves to a target position rather than continuously rotating in one direction.
5. Can I power the SG90 directly from the Arduino 5V pin?
Our demonstration uses one SG90 without a load for a short test, powered from the Arduino 5V pin. When the servo is under load, starts moving, or when multiple servos are used, the Arduino 5V supply may not be sufficient. If the board resets or the servo jitters, use a suitable regulated external supply and connect its ground to Arduino GND.
6. Can I turn the servo horn by hand?
Do not force the servo horn while the servo is powered, and do not let the motor continuously push against a mechanical stop or obstruction. To adjust the horn position, disconnect power first, then reinstall and secure it.
9. STEM DIY Extension Challenges
Challenge A: Change the target angles
Change 20 and 160 in the sketch to 45 and 135. Observe how the sweep range changes. Avoid commanding the servo into a mechanical stop.
Challenge B: Control the angle with a potentiometer
Add a potentiometer, read its analogue value, and use map() to convert the reading into a target servo angle.
Challenge C: Add push-button control
Use a push button to switch between preset positions such as 30°, 90°, and 150°.
Challenge D: Combine the servo with an ultrasonic sensor
Combine the HC-SR04 with the SG90 so the servo can move the sensor from side to side, preparing for a simple scanning project.
10. Conclusion
In this lesson, we used an Arduino Uno R3 and an SG90 9g micro servo to demonstrate back-and-forth position control between target angles of 20° and 160°. By connecting the three servo wires and using the Servo library, we learned how to set a target angle and repeat a sweeping movement.
Servo motors are widely used in robotics, models, and automation projects. Once you understand the basic control method, you can add sensors, buttons, and displays to build more interactive Arduino projects.
Browse Sun Cheong Computer's Arduino range for development boards, sensors, and STEM DIY accessories:
Browse Arduino and STEM DIY Accessories
View the SG90 9g Micro Servo Motor
Sun Cheong Computer Company Limited
G/F, 246 Apliu Street, Sham Shui Po, Kowloon, Hong Kong