小编
Published2025-10-15
Introduction to Arduino and Servo Motors: A Perfect Pair for Robotics Projects
In the exciting world of electronics and robotics, few components offer as much versatility as the servo motor. Known for their precision in movement, servo motors are widely used in various applications, from controlling robotic arms to steering mechanisms in remote-controlled vehicles. If you're just starting with Arduino, learning how to connect and control a servo motor is an excellent first step toward bringing your robotic ideas to life.

The Arduino Uno is one of the most popular microcontrollers used by hobbyists and engineers alike. With its open-source hardware and vast support community, it provides an ideal platform for beginners and experts. When paired with a servo motor, it can create dynamic and responsive systems that can mimic movement, adjust angles, or even automate mechanical actions.
In this guide, we’ll walk you through the steps of connecting a servo motor to an Arduino Uno, from wiring and programming to testing. By the end, you’ll have a working setup that can control the rotation of a servo with ease.
Before diving into the technical details, let’s understand what a servo motor is. Unlike regular DC motors, which simply spin in a continuous loop, servo motors can rotate to specific angles. They use a feedback loop to control the position of their output shaft, which is what makes them perfect for applications requiring precise motion, such as controlling the position of robotic arms or camera gimbals.
Servo motors typically have three wires:
Power (Red): Supplies the motor with electrical energy.
Ground (Black or Brown): Completes the electrical circuit.
Signal (Yellow or White): Sends the control signal from the microcontroller (Arduino) to determine the motor’s position.
Most common hobby servo motors have a rotation range of 0° to 180°, allowing them to move to precise positions within this range.
The Components You’ll Need
To connect a servo motor to an Arduino Uno, you’ll need the following components:
Arduino Uno board: The brain of the operation.
Servo motor (e.g., SG90 or MG90S): A small, inexpensive motor that works well for most projects.
Jumper wires: To make the necessary electrical connections.
Breadboard (optional): To organize your wiring (though not required in this simple setup).
External power source (optional): If your servo motor requires more power than the Arduino can provide.
Wiring the Servo Motor to the Arduino Uno
Now that you have all the components ready, let’s go over the wiring process. This is a simple setup, so the connections are straightforward:
Servo Motor Power (Red Wire): Connect this to the 5V pin on the Arduino Uno. The Arduino will supply power to the servo motor from its 5V output pin.
Servo Motor Ground (Black or Brown Wire): Connect this to one of the Arduino’s GND (ground) pins. This creates a common ground for the motor and the Arduino.
Servo Motor Signal (Yellow or White Wire): Connect this to one of the Arduino’s digital pins. For this example, we will use pin 9. This is the pin that will send control signals to the servo motor.
Once the wiring is complete, your setup should look something like this:
Servo Red Wire → Arduino 5V Pin
Servo Black Wire → Arduino GND Pin
Servo Yellow Wire → Arduino Pin 9
With the connections made, it’s time to move on to the software side.
Arduino IDE Setup and Writing the Code
To control the servo motor, we will use the Servo library, which is already included in the Arduino IDE by default. This library makes it incredibly easy to control the angle of the servo motor by sending PWM (Pulse Width Modulation) signals through the Arduino’s digital pins.
Here’s a simple code to control the servo motor. This code will make the servo move back and forth between 0° and 180°:
#include // Include the Servo library
Servo myServo; // Create a servo object
myServo.attach(9); // Attach the servo to pin 9
myServo.write(0); // Move the servo to 0 degrees
delay(1000); // Wait for 1 second
myServo.write(180); // Move the servo to 180 degrees
delay(1000); // Wait for 1 second
#include : This line tells the Arduino to use the Servo library, which provides functions to control the servo motor.
Servo myServo;: This creates a servo object named myServo, which we will use to control the motor.
myServo.attach(9);: This attaches the servo object to pin 9, where we’ve connected the servo’s signal wire.
myServo.write(0);: This command tells the servo to move to 0 degrees (the starting position).
delay(1000);: This pauses the program for 1 second (1000 milliseconds) before moving the servo again.
myServo.write(180);: This moves the servo to 180 degrees (the maximum position).
delay(1000);: Another 1-second delay.
Once you’ve uploaded the code to the Arduino Uno, you should see the servo motor move back and forth between 0° and 180°. If the motor is moving smoothly, congratulations! You’ve successfully connected and controlled your first servo motor with an Arduino Uno.
Troubleshooting and Advanced Techniques
While the basic setup described in the first part should work well for most beginner projects, there are a few things you might run into when working with servo motors. In this section, we’ll go over some common issues and troubleshooting tips, as well as advanced techniques you can use to enhance your projects.
Common Issues and Troubleshooting
1. Servo Not Moving or Moving Erratically
Cause: The servo might not be receiving enough power from the Arduino.
Solution: If your servo is moving erratically or not moving at all, ensure it’s receiving adequate power. Sometimes, the Arduino’s 5V pin may not provide enough current for the servo, especially if the motor is under load. In such cases, consider powering the servo separately using an external power supply (usually 4.8V to 6V) and only connect the ground (GND) of the power supply to the Arduino.
Cause: Drawing too much current from the Arduino’s 5V pin.
Solution: Use an external power supply for your servo. If you continue to draw too much current from the Arduino, it may overheat and become damaged.
3. Servo Not Reaching Desired Position
Cause: The servo may not be calibrated correctly or is faulty.
Solution: Make sure the servo is a standard hobby servo that has a 0° to 180° range. If the servo doesn’t reach the expected positions, try adjusting the servo angles in the code (e.g., use myServo.write(90); for the center position).
Advanced Techniques: Controlling Multiple Servo Motors
While controlling one servo motor is exciting, many projects require controlling multiple servos. The good news is that the Servo library supports multiple servos, allowing you to control up to 12 servos using the Arduino Uno’s digital pins. Here's an example of how you can control two servos:
servo1.attach(9); // Attach the first servo to pin 9
servo2.attach(10); // Attach the second servo to pin 10
servo1.write(0); // Move the first servo to 0 degrees
servo2.write(180); // Move the second servo to 180 degrees
servo1.write(180); // Move the first servo to 180 degrees
servo2.write(0); // Move the second servo to 0 degrees
This code moves two servos at the same time, with one moving from 0° to 180° and the other moving in the opposite direction.
Conclusion: Unlocking the Potential of Servo Motors with Arduino
Connecting a servo motor to an Arduino Uno is an exciting introduction to the world of robotics, automation, and electronics. Whether you’re building a robotic arm, a simple rotating platform, or something more complex, understanding how to control servos gives you the flexibility to create interactive and dynamic systems.
By following the steps outlined in this guide, you should now have the knowledge to not only connect a servo motor to your Arduino Uno but also troubleshoot common issues and explore advanced
Kpower has delivered professional drive system solutions to over 500 enterprise clients globally with products covering various fields such as Smart Home Systems, Automatic Electronics, Robotics, Precision Agriculture, Drones, and Industrial Automation.
Update:2025-10-15
Contact Kpower's product specialist to recommend suitable motor or gearbox for your product.