小编
Published2025-10-15
Introduction to Servo Motors and Arduino Control
In the world of robotics, automation, and DIY electronics, servo motors are incredibly popular for precise control over angular movements. When paired with an Arduino, a microcontroller board known for its simplicity and versatility, you can easily make your projects come to life with precise, controlled motion. This guide will walk you through how to control a servo motor to rotate a full 180 degrees using Arduino, covering everything from the basic setup to the code you need to make it happen.

A servo motor is a type of motor that rotates a specific angle with high precision. Unlike regular motors that simply spin continuously, servo motors are designed to move to a particular position and stay there. They are commonly used in applications requiring controlled movements, such as in robotics, camera pans, and steering systems. The key feature of servo motors is that they can rotate a shaft anywhere from 0 to 180 degrees (or more, depending on the type of servo).
In most cases, servo motors operate using a Pulse Width Modulation (PWM) signal, which determines the position of the motor’s shaft. By varying the duration of the pulse sent to the motor, you can control the angle of rotation.
Why Use Arduino to Control a Servo?
Arduino is a user-friendly microcontroller that makes it incredibly easy to integrate sensors, motors, and other components into a single project. By using Arduino, you can send signals to a servo motor that control its position, making it an essential tool for hobbyists and engineers alike. The beauty of combining Arduino with a servo motor is its simplicity and flexibility, allowing anyone—from beginners to experienced makers—to experiment and bring their ideas to life.
For this project, we will focus on controlling a servo motor to move precisely through a 180-degree range. This is a common task in many robotics and automation applications, and once you understand the basics, you can use this knowledge to control servos in more complex projects.
Materials Needed for the Project
To get started with controlling a servo motor using Arduino, you'll need the following components:
Arduino Board (Uno, Nano, etc.): The brain of your project, the Arduino controls the servo by sending PWM signals.
Servo Motor: A standard hobby servo motor is perfect for this task. Popular models like the SG90 or MG90S work well for beginners.
Jumper Wires: These wires will be used to connect the servo motor to the Arduino board.
Breadboard (optional): While not strictly necessary, a breadboard helps organize the connections and makes prototyping easier.
External Power Supply (optional): If your servo motor requires more current than the Arduino can provide, an external power supply will be needed.
Once you have all the components ready, it’s time to move on to the setup and programming.
Wiring the Servo Motor to Arduino
The wiring setup for controlling a servo motor with Arduino is relatively straightforward. Here's how you should connect the components:
Power (VCC): Connect the 5V pin of the Arduino to the power pin (usually red) on the servo motor. This supplies the servo with power.
Ground (GND): Connect the GND pin of the Arduino to the ground pin (usually black or brown) of the servo motor.
Control (Signal): Connect the control (PWM) pin of the servo motor to a PWM-enabled pin on the Arduino. On most Arduino boards, pin 9 is a good choice.
Once everything is connected, the circuit is ready to be controlled by your Arduino. Now, let’s dive into the code that will bring your servo motor to life.
Writing the Arduino Code to Control 180-Degree Rotation
Now that you've connected the servo motor to your Arduino, it's time to write the code that will allow you to control the servo's movement. Fortunately, Arduino’s built-in Servo library makes it incredibly easy to work with servo motors.
Installing the Servo Library
Before we can begin writing the code, we need to ensure that the Servo library is installed in the Arduino IDE. If you are using the official Arduino IDE, the Servo library is included by default. If, for any reason, it's not available, you can easily install it through the Library Manager:
Open the Arduino IDE and go to Sketch > Include Library > Manage Libraries.
In the search box, type "Servo" and hit Enter.
Click the "Install" button next to the Servo library in the list.
Here is the basic Arduino code that will control the servo motor to rotate 180 degrees:
#include // Include the Servo library
Servo myServo; // Create a Servo object
myServo.attach(9); // Attach the servo to pin 9 on the Arduino
// Rotate the servo to 0 degrees
delay(1000); // Wait for 1 second
// Rotate the servo to 180 degrees
delay(1000); // Wait for 1 second
#include : This line includes the Servo library, which contains all the functions needed to control a servo motor.
Servo myServo;: This creates a Servo object named myServo. This object will control the servo motor.
myServo.attach(9);: The attach() function tells Arduino which pin the servo is connected to (in this case, pin 9).
myServo.write(0);: The write() function sends a PWM signal to the servo, telling it to rotate to the specified angle (in this case, 0 degrees).
delay(1000);: The delay() function pauses the program for 1 second before executing the next command, allowing the servo to hold its position.
The setup() function runs once when the Arduino is powered up. In this function, the servo motor is attached to pin 9.
The loop() function runs repeatedly. In this case, it rotates the servo to 0 degrees, waits for one second, then rotates it to 180 degrees and waits for another second. This cycle repeats indefinitely.
With this simple code, your servo motor will move back and forth between 0 and 180 degrees, making it a great starting point for learning how to control servo motors with Arduino.
While this code moves the servo motor between 0 and 180 degrees, you can easily customize the movement. For example, you can:
Control the speed: The speed of the servo’s movement can be adjusted by changing the delay between angle commands or using more sophisticated techniques like gradually changing the angle in small steps.
Control the angle dynamically: Instead of hardcoding the 0 and 180-degree values, you could use input from sensors (like a potentiometer or ultrasonic sensor) to adjust the servo’s position based on real-time data.
Make more complex movements: By combining multiple servo motors and adding logic, you can create more intricate movements for robotic arms, camera pans, or other projects.
In the next part, we’ll explore some advanced techniques for controlling multiple servos, fine-tuning your servo motor’s performance, and troubleshooting common issues. Stay tuned!
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.