小编
Published2025-10-15
This guide explores the fundamentals of controlling servo motors using an Arduino. It covers both basic concepts and practical code examples to help enthusiasts and beginners master servo motor control for robotics, home automation, and DIY projects.

Introduction to Servo Motors and Arduino
In the world of robotics, automation, and DIY electronics, controlling motors is a fundamental skill. One of the most popular and versatile types of motors is the servo motor. Servo motors are widely used because they offer precise control over the rotation angle, making them ideal for applications like robotic arms, wheels, cameras, and even as simple as adjusting the position of a door.
This article will walk you through the basics of controlling a servo motor using an Arduino. Whether you’re building a robot, creating a moving display, or experimenting with DIY projects, learning how to control a servo motor is a vital skill.
A servo motor is a type of motor that can rotate to a specific angle within a defined range, usually between 0° and 180°. Unlike regular DC motors, which spin continuously, servos are equipped with a feedback mechanism that helps them maintain and adjust their position with high precision. This is made possible by the use of a potentiometer or a similar sensor within the servo unit.
The most common servo motor used in DIY projects is the SG90 (a small, low-cost servo), but there are also larger, more powerful servos designed for heavy-duty tasks. Regardless of size, all servos operate in a similar way: they take a signal and adjust the shaft position accordingly.
Components Needed for the Project
Before diving into the code, let’s look at the essential components you’ll need for your servo motor project:
Arduino Board (e.g., Arduino Uno or Nano) – This will serve as the controller for the servo motor.
Servo Motor – Any standard hobby servo motor, like the SG90.
Jumper Wires – These will connect the servo motor to the Arduino.
Breadboard (optional) – You can use this for cleaner connections.
Power Supply – While the Arduino can supply power to small servos, larger servos may need an external power source.
Understanding PWM (Pulse Width Modulation)
To control the position of a servo, you need to send it a PWM signal (Pulse Width Modulation). The idea is that the servo motor reads this signal and adjusts its position based on the width of the pulse.
1ms pulse – Moves the servo to 0°.
1.5ms pulse – Moves the servo to 90° (center).
2ms pulse – Moves the servo to 180°.
Each pulse lasts about 20 milliseconds, and the position is determined by the duration of the pulse within that window. The Arduino board generates this PWM signal, which the servo motor interprets to adjust its position.
Wiring the Servo to Arduino
Connecting your servo motor to the Arduino is straightforward:
Red wire (Power) – Connect it to the 5V pin on the Arduino.
Brown/Black wire (Ground) – Connect it to the GND pin on the Arduino.
Orange/Yellow wire (Control) – Connect this to a PWM-enabled pin on the Arduino, such as Pin 9.
Once the connections are made, you are ready to move on to the coding part.
Writing the Basic Servo Motor Code for Arduino
Now that you have an understanding of what a servo motor is and how it works, it’s time to dive into the actual code to control the servo using your Arduino.
Installing the Servo Library
Before you begin writing your code, you’ll need to include the Servo library. The good news is that the Arduino IDE already comes with a built-in Servo library, so there’s no need to install anything extra.
To include the library, add this line at the top of your code:
The Servo library provides simple functions that allow you to control a servo motor with ease.
Here’s a basic Arduino sketch to get you started with controlling a servo 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 (center position)
delay(1000); // Wait for 1 second
myServo.write(180); // Move the servo to 180 degrees
delay(1000); // Wait for 1 second
Include the Servo library: The first line #include includes the Servo library, which gives you access to various servo control functions.
Create a Servo object: Servo myServo; creates an object named myServo that will represent the servo motor in the code.
Attach the servo to a pin: The myServo.attach(9); function tells the Arduino to use pin 9 for controlling the servo. You can change this pin number based on where your servo is connected on the Arduino board.
Control the servo: In the loop() function, we use myServo.write() to move the servo to specific angles:
myServo.write(0); moves the servo to 0°.
myServo.write(90); moves it to the center (90°).
myServo.write(180); moves the servo to its maximum (180°).
Delay: delay(1000); makes the program pause for 1 second after each movement, allowing you to visually observe the servo’s motion.
Experimenting with the Code
You can experiment with different angles and delays to achieve more sophisticated movements. For example, you can use myServo.write(45); to move the servo to a 45° angle or create more complex patterns by changing the delay times.
Servo not moving: Make sure the power and ground wires are correctly connected. Also, check the PWM pin for any wiring issues.
Servo jittering: If your servo is jittering or not responding smoothly, it could be due to insufficient power. In such cases, consider using an external power source for the servo motor.
Servo not centered: If your servo doesn’t seem to center at 90°, you may need to adjust the code slightly. Some servos require calibration to ensure that the 90° position is truly in the middle.
Once you're comfortable with the basics, you can expand the project further. For example, you could:
Control the servo with a potentiometer: Use an analog sensor to control the servo’s position based on user input.
Use multiple servos: Attach multiple servos to different pins and control them simultaneously.
Integrate with sensors: Use ultrasonic sensors, infrared sensors, or other types of input to automatically adjust the servo’s position in response to the environment.
By understanding the basic principles of controlling a servo motor with Arduino, you unlock a world of possibilities for your projects. Whether you're building simple mechanical systems or more complex robots, servo motors will play a crucial role in enabling precise, repeatable movement.
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.