小编
Published2025-10-15
This article walks you through the essentials of controlling a servo motor using an Arduino. From understanding the components and setup to coding and troubleshooting, we cover all the important steps for you to get started on your Arduino projects.

Arduino, servo motor, control servo, Arduino tutorial, servo motor control, electronics, Arduino programming, DIY projects, robotics, beginner's guide.
Introduction to Servo Motors and Arduino Basics
A servo motor is a small yet powerful device that allows for precise control of angular position. Unlike standard motors, which continuously rotate, servos can be controlled to rotate to a specific angle within a range (usually 0° to 180°). These motors are commonly used in robotics, remote-control devices, and even hobbyist projects like controlling a robotic arm or steering a car.
Servo motors are typically powered by a 5V supply, and they contain a built-in feedback system that allows for position accuracy. Inside each servo, there’s a small motor, a gearbox, and a potentiometer, which constantly measures and adjusts the position of the motor’s shaft.
Arduino is an open-source electronics platform that makes it incredibly easy for hobbyists, engineers, and designers to create interactive projects. With Arduino, you can program hardware to react to various inputs and control devices like motors, lights, and sensors. It's an excellent choice for controlling a servo motor because it can be easily programmed, is cost-effective, and provides the flexibility needed for complex tasks.
In the context of controlling a servo motor, Arduino’s ease of use and ability to work with PWM (Pulse Width Modulation) signals make it the perfect platform for beginners. PWM is a signal method that Arduino uses to control the precise position of the servo motor.
Before diving into the technical steps, let’s first list the materials and components you’ll need to control your servo with Arduino:
Arduino Board (e.g., Arduino Uno) – This will serve as the controller for your servo motor.
Servo Motor – Make sure to choose one that is compatible with your project.
Jumper Wires – For making connections between your Arduino and the servo motor.
Breadboard (optional) – For organizing your components and avoiding unnecessary wire mess.
External Power Supply (optional) – Some servos require more power than the Arduino can provide. If this is the case, an external power supply may be necessary.
Computer with Arduino IDE – You’ll need the software to write and upload your code to the Arduino board.
Basic Wiring for Connecting a Servo Motor to Arduino
To get started, you need to connect your servo motor to the Arduino. Here’s a simple guide for making the connections:
Connect the Ground (GND) pin of the servo motor to the Ground (GND) pin on the Arduino.
Connect the Power (VCC) pin of the servo motor to the 5V pin on the Arduino. Some servos may require an external power supply if they draw more current than the Arduino can handle.
Connect the Signal (S) pin of the servo to a PWM-enabled pin on the Arduino. The most common pin for PWM signals is Pin 9, but other pins (3, 5, 6, 10, or 11) can also work.
This completes the basic wiring setup to allow Arduino to control the servo motor.
Once you’ve made the necessary physical connections, the next step is to write a basic program (or sketch, as it’s called in Arduino language) to control the servo motor. Here’s a simple code example to get you started:
#include // Include the Servo library
Servo myServo; // Create a Servo object
myServo.attach(9); // Attach the servo to pin 9
myServo.write(0); // Move servo to 0 degrees
delay(1000); // Wait for 1 second
myServo.write(90); // Move servo to 90 degrees
delay(1000); // Wait for 1 second
myServo.write(180); // Move servo to 180 degrees
delay(1000); // Wait for 1 second
Let’s break down the code:
Servo Library: The Servo.h library is essential for controlling servo motors in Arduino. It makes things easy by offering functions like attach() and write().
Servo Object: We create a Servo object called myServo to represent the servo motor.
Setup Function: In the setup(), we tell Arduino which pin the servo is connected to (in this case, pin 9). The attach() function links the servo motor to the pin.
Loop Function: This part is where the servo motor is controlled. The write() function is used to send an angle value to the servo. The value should be between 0 (fully counterclockwise) and 180 (fully clockwise). We also use the delay() function to pause the program for 1 second between movements.
Troubleshooting Basic Servo Control
If your servo isn’t moving or behaving erratically, there are a few common issues to check:
Power Supply: Make sure the servo is getting enough power. If you're using an external power supply, double-check the voltage (typically 5V for most servos).
Wiring: Ensure that the connections between the Arduino and the servo are secure. Sometimes, loose or incorrectly connected wires can cause issues.
Servo Library: Make sure the Servo.h library is included correctly in your code.
Code Issues: Check your code for any syntax errors or incorrect pin assignments.
Advanced Servo Control Techniques and Applications
Using Multiple Servos with Arduino
While the previous example demonstrated how to control a single servo, you can control multiple servos using Arduino as well. This is especially useful in robotics or mechanical systems where multiple axes of movement are required.
To control multiple servos, you simply need to create multiple Servo objects, each attached to a different pin. For example:
servo1.write(0); // Move servo 1 to 0 degrees
servo2.write(180); // Move servo 2 to 180 degrees
servo1.write(90); // Move servo 1 to 90 degrees
servo2.write(90); // Move servo 2 to 90 degrees
In this code, we control two servos connected to pins 9 and 10. The servo motors can be manipulated independently by writing different angles to each one.
Using Potentiometers for Real-Time Control
Another common way to control a servo is by using a potentiometer, which acts as a variable resistor. You can use a potentiometer to adjust the angle of the servo in real-time. The following code demonstrates this:
int potValue = 0; // Variable to store potentiometer value
int angle = 0; // Variable to store angle value
potValue = analogRead(A0); // Read the potentiometer value
angle = map(potValue, 0, 1023, 0, 180); // Map the value to an angle between 0-180
myServo.write(angle); // Move the servo to the mapped angle
delay(15); // Wait for the servo to reach the position
We read the value from the potentiometer using analogRead().
The map() function converts the potentiometer’s range (0 to 1023) to the servo’s angle range (0 to 180).
The servo motor’s position is then updated according to the potentiometer’s position.
Controlling Servo with PWM
If you're dealing with more advanced projects, you might need finer control over the servo motor. Using Pulse Width Modulation (PWM), you can fine-tune the behavior of the servo motor by adjusting the width of the pulse sent to it.
Arduino’s analogWrite() function generates a PWM signal that can control various devices, including servos. While this is a more advanced topic, understanding PWM in the context of controlling servos opens the door to precise motor control in complex applications.
Controlling a servo motor with an Arduino is one of the most rewarding and accessible projects for beginners in electronics. By following the basic steps outlined in this guide, you can easily set up your servo, write the necessary code, and begin experimenting with more advanced techniques like controlling multiple servos or using sensors. The possibilities are endless, from building robotic arms to creating automated systems, all with the power of Arduino!
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.