小编
Published2025-10-15
When it comes to the world of robotics and automation, one of the key components you'll often encounter is the servo motor. These motors provide precise control over the position of a mechanical arm or other mechanical systems. If you’re looking to create a project that involves moving parts, a servo motor is an excellent choice due to its reliability, ease of control, and compact size. In this guide, we’ll focus on how you can use Arduino code to move a servo motor, providing a hands-on approach to mastering this powerful tool.

A servo motor is a small, high-precision motor that typically includes a feedback system to control its rotation accurately. Unlike regular DC motors, which rotate continuously, servo motors are designed to rotate to specific positions within a range, typically 0° to 180°. This makes them ideal for applications like steering, robotic arms, and even camera pans.
Before we dive into the code, let’s make sure you have the right components:
Arduino Board (e.g., Arduino Uno)
Breadboard (optional, but helpful for connecting the components)
The servo motor will have three wires:
Ground (usually black or brown)
Signal (usually yellow or white)
The power wire goes to the 5V pin on your Arduino, the ground wire goes to the GND pin, and the signal wire connects to a digital I/O pin (let’s use pin 9 for simplicity).
Basic Code to Control the Servo
Let’s start by writing a basic Arduino code to move the servo motor to a specified angle. The Servo library simplifies the coding process by providing functions that easily allow you to control the position of the servo motor.
First, we need to include the Servo library and create a Servo object to control the motor:
#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(90); // Move the servo to 90 degrees
delay(1000); // Wait for 1 second
myservo.write(180); // Move the servo to 180 degrees
delay(1000); // Wait for 1 second
This code does the following:
Includes the Servo library for controlling the servo motor.
Creates a Servo object named myservo.
Attaches the servo to digital pin 9.
In the loop() function, it moves the servo to positions 0°, 90°, and 180°, pausing for 1 second between each position.
myservo.attach(9);: This attaches the servo motor to pin 9. The attach() function essentially tells Arduino to send PWM (pulse-width modulation) signals to the servo motor, controlling its movement.
myservo.write(angle);: This command moves the servo to the specified angle. It accepts values between 0 and 180, where 0 is the minimum position and 180 is the maximum.
delay(1000);: This pauses the program for 1000 milliseconds (1 second). This gives the servo time to reach the new position before the next command is sent.
By running this code, your servo motor will smoothly transition between 0°, 90°, and 180°, creating a simple but effective demonstration of how Arduino can control a servo motor.
Now that we’ve covered the basics of controlling a servo motor with Arduino, let’s dive deeper into more advanced control techniques. In this part, we’ll explore how to use variable inputs, create smoother movements, and introduce feedback mechanisms for more sophisticated control.
Using Analog Inputs for Variable Control
One of the more powerful features of Arduino is the ability to interact with various sensors or inputs to dynamically adjust motor positions. For example, let’s use a potentiometer, which is an analog device that allows you to vary its resistance (and therefore its voltage output) to adjust the servo’s position.
Here’s how to integrate a potentiometer into our project:
Connect one side of the potentiometer to 5V.
Connect the other side to GND.
Connect the middle pin of the potentiometer to an analog input pin (e.g., A0).
#include // Include the Servo library
Servo myservo; // Create a Servo object
int potValue = 0; // Variable to store potentiometer value
int angle = 0; // Variable to store servo angle
myservo.attach(9); // Attach the servo to pin 9
potValue = analogRead(A0); // Read the potentiometer value (0 to 1023)
angle = map(potValue, 0, 1023, 0, 180); // Map the potentiometer value to an angle (0 to 180)
myservo.write(angle); // Set the servo position
delay(15); // Wait for the servo to reach the position
analogRead(A0);: This reads the analog value from the potentiometer (values range from 0 to 1023).
map(potValue, 0, 1023, 0, 180);: The map() function converts the potentiometer value into an angle between 0 and 180°. This allows you to control the servo’s position smoothly by turning the potentiometer.
myservo.write(angle);: The servo moves to the mapped angle.
With this setup, turning the potentiometer will directly adjust the position of the servo motor. This method is great for creating interactive projects, such as a joystick-controlled robot arm or camera gimbal.
One common challenge when working with servo motors is achieving smooth movements. Servo motors can sometimes move too abruptly, making the action seem unnatural. To create smoother, more controlled movements, you can use gradual increments in the servo position instead of jumping straight to a new angle.
Here’s an example where we gradually move the servo from 0° to 180°:
#include // Include the Servo library
Servo myservo; // Create a Servo object
myservo.attach(9); // Attach the servo to pin 9
for (int pos = 0; pos <= 180; pos++) { // Gradually move from 0° to 180°
myservo.write(pos); // Set the servo position
delay(15); // Wait for the servo to reach the position
for (int pos = 180; pos >= 0; pos--) { // Gradually move from 180° back to 0°
myservo.write(pos); // Set the servo position
delay(15); // Wait for the servo to reach the position
The for loop incrementally moves the servo motor from 0° to 180° and back.
Each step is delayed by 15 milliseconds, allowing the servo motor to reach each position smoothly.
This technique creates fluid, gradual movements instead of abrupt jumps. It can be useful for applications where smooth motion is critical, such as in animatronics or camera tracking.
By now, you should have a solid understanding of how to control a servo motor with Arduino, from basic movement to more advanced techniques like variable input control and smooth movement programming. Arduino provides an accessible and versatile platform for controlling hardware like servo motors, allowing you to create everything from basic projects to complex systems.
Whether you’re building a simple robotic arm or a more interactive system with user input, understanding how to control servos with Arduino will open the door to a wide range of creative and practical projects. So, get your hands on the components, try out these techniques, and start creating your own motorized creations!
Established in 2005, Kpower has been dedicated to a professional compact motion unit manufacturer, headquartered in Dongguan, Guangdong Province, China.
Update:2025-10-15
Contact Kpower's product specialist to recommend suitable motor or gearbox for your product.