小编
Published2025-10-15
This article explains how to program and control a servo motor using an Arduino board. It walks you through the essential components, wiring, and the coding process, making it easy for beginners to learn how to control servos for various applications like robotics, automation, and more.
.webp)
Arduino, Servo motor, Programming, Robotics, Control, Tutorial, Electronics, Beginner’s guide, Servo motor control
Introduction to Arduino and Servo Motors
When it comes to controlling precise mechanical movements, few devices are as widely used as the servo motor. These motors are widely employed in robotics, automation systems, remote-controlled vehicles, and various DIY electronics projects. Combining the power of Arduino, an easy-to-use microcontroller, with a servo motor can open up a world of possibilities. Whether you’re looking to build a robotic arm or automate a system, programming a servo motor with Arduino is an essential skill.
Before diving into the programming, let’s quickly explore what a servo motor is and why it's ideal for precision control. A servo motor is a type of motor that allows you to control its rotational position. Unlike regular DC motors, which simply rotate continuously, servo motors rotate to a specific angle and then stop. This makes them ideal for applications requiring precise movement, such as steering mechanisms, robotic joints, and camera gimbals.
Servo motors come with built-in controllers, and you can command them to turn to specific angles. These motors typically consist of a small DC motor, a gear train, a position sensor (usually a potentiometer), and an electronic controller.
Why Use Arduino to Control a Servo Motor?
Arduino is an open-source microcontroller platform that is widely favored for its simplicity and ease of use. With just a few lines of code, you can program an Arduino to control different devices like LEDs, motors, sensors, and even servos. When paired with a servo motor, Arduino allows you to easily create a variety of projects that require movement or rotation.
One of the key advantages of using Arduino is that it can communicate with the servo motor through simple pulse-width modulation (PWM). PWM is a technique used to vary the amount of time the motor spends on and off within a cycle, ultimately controlling the motor’s position. By adjusting the duration of the PWM signal, you can easily command the servo motor to rotate to a specific angle.
Components Needed to Get Started
To get started with programming a servo motor using Arduino, you will need the following components:
Arduino Board (e.g., Arduino Uno): This is the brain of your project that will control the servo.
Servo Motor: Any standard servo motor that operates on a 5V supply (like the SG90 or MG996R).
Jumper Wires: To connect the servo to the Arduino.
Breadboard (optional): For organizing the connections if needed.
Power Source: Arduino can be powered via USB or an external power supply, and the servo might require a separate power source depending on its current requirements.
Connecting the Servo to Arduino
The physical connections between the servo motor and the Arduino are relatively simple:
Connect the red wire of the servo to the 5V pin on the Arduino.
Connect the brown or black wire (ground) of the servo to the GND pin on the Arduino.
Connect the yellow or white wire (signal) of the servo to a PWM-capable pin on the Arduino, typically pin 9.
Now that we have a basic understanding of what a servo motor is, why we use Arduino to control it, and how to wire up the components, let’s dive into programming.
Writing the Code for the Servo Motor
Arduino uses a simplified programming language based on C/C++. To make the servo motor move to a specific position, we will use the Servo library, which makes controlling a servo motor extremely easy. Below is a simple sketch to get started.
Basic Arduino Code to Control a Servo
#include // Include the Servo library
Servo myServo; // Create a Servo object to control the servo motor
myServo.attach(9); // Attach the servo to pin 9 on the Arduino
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 is the simplest code you can write to move the servo motor back and forth between 0, 90, and 180 degrees. Here's a breakdown of the key parts of the code:
#include : This line includes the Servo library, which contains all the functions you need to control the servo.
Servo myServo;: This line creates a Servo object named myServo that will represent your servo motor in the code.
myServo.attach(9);: This attaches the servo to pin 9 on the Arduino board. You can change the number to another pin if you're using a different one.
myServo.write(angle);: This function moves the servo to a specified angle. The argument passed to write() is the angle (in degrees) to which the servo should rotate. The servo can rotate from 0 to 180 degrees.
delay(1000);: This pauses the program for 1 second before executing the next command.
When you upload this code to your Arduino, the servo will move between 0, 90, and 180 degrees, creating a back-and-forth motion. You can modify the angles and the delay times to suit your project’s needs.
Advanced Servo Motor Control and Practical Applications
Now that you’ve seen the basics of controlling a servo motor, let’s explore some more advanced applications and techniques to control servos with Arduino. This part will cover using multiple servos, controlling the speed of servo movements, and applying servo motors to real-world projects.
Controlling Multiple Servo Motors
In many projects, you may need to control multiple servos at once. Luckily, Arduino makes it easy to control several servos simultaneously with just a few modifications to the code. You can create additional Servo objects and attach them to different pins. Here’s how to control two servos:
Code for Controlling Two Servos:
Servo servo1; // Create Servo object for the first servo
Servo servo2; // Create Servo object for the second servo
servo1.attach(9); // Attach the first servo to pin 9
servo2.attach(10); // Attach the second servo to pin 10
servo1.write(0); // Move the first servo to 0 degrees
servo2.write(180); // Move the second servo to 180 degrees
delay(1000); // Wait for 1 second
servo1.write(90); // Move the first servo to 90 degrees
servo2.write(90); // Move the second servo to 90 degrees
delay(1000); // Wait for 1 second
In this example, we have two servos, and we control each one independently. You can extend this code to control as many servos as your Arduino can handle, but be aware that each servo requires its own pin. If you run out of pins on your Arduino, you might need to use a multiplexer or a servo controller.
While the basic write() function moves the servo to a given angle instantly, you may want to control the speed at which the servo moves. The Servo library doesn’t have a built-in method for this, but you can achieve a smooth movement by incrementing the angle gradually.
Here’s an example where the servo smoothly moves from 0 to 180 degrees:
Code for Smooth Servo Movement:
for (int angle = 0; angle <= 180; angle++) {
myServo.write(angle); // Incrementally move the servo to the current angle
delay(15); // Wait to create a smooth movement
for (int angle = 180; angle >= 0; angle--) {
myServo.write(angle); // Decrementally move the servo back to 0 degrees
delay(15); // Wait to create a smooth movement
In this code, the servo gradually moves from 0 to 180 degrees and then back again. The delay of 15 milliseconds between each angle adjustment helps to smooth out the movement.
Applications of Servo Motors in Real-World Projects
Servo motors have a wide variety of applications. Some of the most popular ones include:
Robotics: Servo motors are often used in robotic arms and legs to control precise movement. For example, in a humanoid robot, servos can control the movement of joints.
Automation: In automation, servos are used to control actuators and mechanisms that require precise motion. For example, servo motors can be used in automated doors, camera sliders, and more.
RC Vehicles: Servo motors are commonly found
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.