小编
Published2025-10-15
Introduction to Arduino and Servo Motors
Arduino is one of the most accessible platforms for electronics enthusiasts and engineers alike. With its open-source nature, extensive community support, and ease of use, it's an ideal choice for those looking to dive into the world of electronics, robotics, or automation. One of the most exciting applications of Arduino is controlling servo motors, which are widely used in various applications, from robotics to home automation.

In this article, we’ll explore how to control servo motors using Arduino, and provide you with the knowledge and tools to master servo control for your projects. Whether you’re building a robot arm, a remote-controlled car, or even a simple automated curtain, servos are the perfect solution for precise control of movement. This guide will take you through the basics of servo motors, how to wire them to your Arduino, and how to write the code needed to make them move.
A servo motor is a type of electric motor that is designed to precisely control angular position, velocity, and acceleration. Unlike regular motors that rotate continuously, servo motors are typically used for applications that require precise, incremental movement. They consist of a small DC motor, a potentiometer (for feedback), and a control circuit.
Servos are controlled by Pulse Width Modulation (PWM), a method that sends pulses of electricity to the motor. The length of these pulses determines the position of the motor's shaft. This means that you can easily control the angle of a servo motor by adjusting the pulse width sent from the Arduino.
Understanding PWM (Pulse Width Modulation)
PWM is a technique used to create a signal that turns a motor on and off rapidly, adjusting the duration of the "on" time (also known as the pulse width). For example, a short pulse might move the motor to one position, while a longer pulse could move it to another. In the case of servo motors, these pulses are typically sent at a frequency of 50 Hz, which corresponds to a pulse every 20 milliseconds.
The duration of the pulse determines how far the servo rotates. A typical servo motor operates with a pulse width between 1 millisecond (which moves the servo to one extreme) and 2 milliseconds (which moves it to the other extreme). The servo motor’s rotation angle is proportional to the pulse width, with 1 millisecond corresponding to 0 degrees and 2 milliseconds corresponding to 180 degrees.
The Arduino Servo Library
To simplify the process of controlling a servo with Arduino, the platform provides a library called "Servo." The Servo library allows you to easily control the position of a servo motor by just writing a few lines of code. It abstracts away the need to manually create PWM signals, making it incredibly simple to use.
To start using the Servo library, you need to include it in your Arduino sketch. The library provides a set of functions that allow you to control one or more servos, adjusting their angle and position.
Here’s a simple example of how to use the Servo library:
Servo myServo; // Create a Servo object
myServo.attach(9); // Attach the servo to pin 9
myServo.write(90); // Move the servo to the 90-degree position
delay(1000); // Wait for 1 second
This simple code will move a servo attached to pin 9 to the 90-degree position. The attach() function tells the Arduino which pin the servo is connected to, while the write() function sets the servo’s position.
Advanced Servo Control Techniques
Now that we have covered the basics of servo control, let’s dive into some more advanced techniques that can help you take full advantage of servo motors in your Arduino projects. We’ll explore multiple servo control, smooth movements, and the integration of sensors to make your project more interactive.
Controlling Multiple Servos
One of the most powerful features of Arduino is the ability to control multiple devices simultaneously. The Servo library supports controlling multiple servos at once by creating multiple Servo objects. Each object can be attached to a different pin, and you can control each servo independently by calling the write() function for each one.
Here’s an example of how to control two servos at once:
servo1.attach(9); // Attach the first servo to pin 9
servo2.attach(10); // Attach the second servo to pin 10
servo1.write(0); // Move servo1 to 0 degrees
servo2.write(180); // Move servo2 to 180 degrees
delay(1000); // Wait for 1 second
servo1.write(90); // Move servo1 to 90 degrees
servo2.write(90); // Move servo2 to 90 degrees
delay(1000); // Wait for 1 second
This code moves two servos attached to pins 9 and 10. By calling write() for each servo individually, you can make them move in a coordinated manner. You can expand this to control as many servos as you need, as long as you have enough pins available on your Arduino board.
One of the challenges with controlling servos is achieving smooth, gradual movements. By default, servos tend to "snap" to a new position when commanded, but with a little extra code, we can smooth out these movements. This is particularly useful in projects like robotic arms, where smooth and controlled movements are essential.
To create smooth transitions, you can use a loop to incrementally adjust the servo’s position over time. Here’s an example of how to move a servo smoothly from 0 to 180 degrees:
myServo.attach(9); // Attach the servo to pin 9
for (int pos = 0; pos <= 180; pos++) {
myServo.write(pos); // Move the servo to the current position
delay(15); // Wait for 15 milliseconds to allow the servo to move
for (int pos = 180; pos >= 0; pos--) {
myServo.write(pos); // Move the servo back to 0 degrees
delay(15); // Wait for 15 milliseconds
In this example, the servo moves from 0 to 180 degrees and back. The loop gradually increments the position, and the delay(15) ensures that the servo has enough time to move smoothly between each step. You can adjust the delay to change the speed of the movement.
Integrating Sensors with Servo Motors
One of the most exciting ways to use servos is by integrating them with sensors, allowing you to create interactive projects that respond to external stimuli. For instance, you can use a potentiometer to control the position of a servo in real-time, or use a motion sensor to trigger a servo to perform a task.
Here's an example of using a potentiometer to control the position of a servo:
int potPin = A0; // Analog pin where the potentiometer is connected
int potValue = 0; // Variable to store potentiometer value
myServo.attach(9); // Attach the servo to pin 9
potValue = analogRead(potPin); // Read the potentiometer value
int angle = map(potValue, 0, 1023, 0, 180); // Map the potentiometer value to an angle
myServo.write(angle); // Move the servo to the mapped angle
delay(15); // Wait for the servo to reach the position
In this code, the potentiometer connected to analog pin A0 controls the position of the servo. The analogRead() function reads the potentiometer’s position, and the map() function maps the potentiometer’s range (0-1023) to the servo’s range (0-180 degrees). The servo then moves accordingly.
With Arduino, controlling servo motors is a straightforward and powerful way to add precision movement to your projects. Whether you're a beginner or an advanced maker, understanding the basics of PWM, the Servo library, and how to integrate sensors opens up endless possibilities for creating interactive, responsive devices. From robotic arms to simple automation systems, servos provide the motion, and Arduino gives you the control.
By experimenting with multiple servos, smooth movement techniques, and sensor integration, you can take your Arduino projects to the next level, turning your ideas into reality with ease and precision.
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.