Learn how to control a servo motor using Arduino in this beginner-friendly project. Includes circuit diagram, code, components list, and project expansion tips. Perfect for automation, robotics, and Arduino starters.
Introduction
🔧 What is a Servo Motor?
A servo motor is a rotary actuator that allows for precise control of angular position. It typically consists of a DC motor, a gear system, and a feedback mechanism (usually a potentiometer) for position sensing. Servo motors are widely used in robotics, automation, RC vehicles, and embedded systems for tasks requiring movement within a specific range (usually 0° to 180°).
🔍 What This Project Does
In this project, we will control the position of a servo motor using Arduino. We’ll send angle commands via code to rotate the motor shaft to a desired angle.
🌍 Real-World Use Case
- Robotic arms and joints
- Camera gimbals
- Automated curtain systems
- Sensor orientation systems (e.g., solar trackers)
📘 Skill Level
Beginner – No prior experience required.
🎯 Expected Outcome
By the end of this tutorial, you’ll be able to:
- Understand servo motor basics
- Control a servo motor’s angle using Arduino code
- Experiment with angle-based movement
Components Required
Quantity | Parts | Description | Buy Link |
---|---|---|---|
1 | Arduino UNO | Microcontroller board | Buy on Elecsynergy |
1 | SG90 Servo Motor | Standard 180° mini servo motor | Buy on Elecsynergy |
1 | Breadboard | For wiring connections | Buy on Elecsynergy |
3 | Jumper Wires (Male to Male) | To connect servo motor | Buy on Elecsynergy |
1 | USB Cable | For Arduino power & code upload | Buy on Elecsynergy |
Circuit Diagram + Explanation
Servo Motor Wiring:
- Red Wire (VCC) → 5V on Arduino
- Brown/Black Wire (GND) → GND on Arduino
- Orange/Yellow Wire (Signal) → Digital Pin 9 on Arduino
Circuit Explanation:
- The servo receives power from the Arduino (5V, GND)
- The signal pin receives PWM signals from Pin 9 to determine the angle
- Arduino sends signals using the Servo library’s
write()
function
Arduino Code – Commented Line-by-Line
#include <Servo.h> // Include Servo library
Servo myServo; // Create Servo object
int angle = 0; // Variable to hold angle value
void setup() {
myServo.attach(9); // Connect servo to digital pin 9
Serial.begin(9600); // Start Serial Monitor
}
void loop() {
for (angle = 0; angle <= 180; angle += 1) {
myServo.write(angle); // Move servo to current angle
Serial.print("Angle: ");
Serial.println(angle);
delay(15); // Wait for servo to reach position
}
for (angle = 180; angle >= 0; angle -= 1) {
myServo.write(angle); // Move servo back to 0
Serial.print("Angle: ");
Serial.println(angle);
delay(15);
}
}
Working Explanation
- When powered on, the Arduino begins sweeping the servo from 0° to 180° and back.
- The
Servo.h
library simplifies sending PWM signals to the servo. - Internally, the servo motor compares the received signal (pulse width) with its current position using its potentiometer and rotates accordingly.
- The delay ensures smooth and visible movement.
Demo / Output Preview
The servo slowly moves from 0° to 180° and back in a continuous loop.
Troubleshooting & Tips
- Servo not moving?
- Double-check wiring to 5V, GND, and signal pin
- Ensure proper pin is specified in
myServo.attach()
- Jittery movement?
- Power servo via external 5V supply if drawing too much current
- Servo not sweeping fully?
- Try using smaller delays or test individual angles like 90°, 45°, etc.
- Arduino freezing?
- Avoid overloading Arduino’s 5V pin with large servos
Project Expansion Ideas
- Use Potentiometer to Control Servo Angle
- Add IR or Bluetooth Remote Control
- Control Multiple Servos for Robotic Arm
- Automated Servo Control Based on Sensor Input (e.g., LDR or Ultrasonic)
Encourage the user to experiment by modifying angle limits, adding buttons, or controlling via sensors.
Conclusion
This project introduced you to controlling a servo motor using Arduino. You learned how to wire a servo, write code for angle-based movement, and expand your project further.
🧠 What You Learned:
- Basics of servo motors
- Using Servo.h library
- Writing PWM-based angle control logic
- Expanding projects using sensors or user input