小编
Published2025-10-15
Learn how to control two servo motors using Arduino with this easy-to-follow guide. This article will walk you through the basic principles of servo motors, the code needed to control them, and how to integrate two servo motors into your Arduino project.
Arduino, Servo Motor, Servo Motor Code, Arduino Project, Servo Motor Control, Two Servo Motors, Arduino Programming, Electronics, Robotics, DIY Projects
Understanding Servo Motors and Getting Started with Arduino
Servo motors are a common component in robotics, automation, and other electronics projects. Whether you're building a robot arm, a simple automation system, or creating a fun project to impress friends, understanding how to control servo motors with Arduino is a crucial skill. In this article, we will explore how to control two servo motors using Arduino code, starting with some essential background information.
A servo motor is a type of motor that allows for precise control of angular position. Unlike regular DC motors, which continuously rotate, servo motors can be positioned at specific angles. This makes them ideal for applications that require precise movement, such as steering systems, robotic arms, or camera gimbals.
Servos typically have three wires: power (VCC), ground (GND), and signal (PWM – Pulse Width Modulation). The signal wire is used to send PWM signals from a microcontroller (in this case, Arduino) to control the motor's position. By varying the width of the pulse, you can control how far the servo turns.
Before you can start writing your code, you need to set up your Arduino environment. Here's how:
Install Arduino IDE: If you don't have it yet, download the Arduino IDE from the official website and install it on your computer.
Connect the Arduino: Use a USB cable to connect your Arduino board (e.g., Uno, Mega, Nano) to your computer.
Select the Correct Board and Port: In the Arduino IDE, select the correct board type and COM port under the “Tools” menu.
Now that you have everything set up, it's time to connect the servo motors.
Wiring the Servo Motors to the Arduino
To control the servo motors, you need to wire them to the Arduino correctly:
Servo Motor 1: Connect the power wire (usually red) to the 5V pin on the Arduino, the ground wire (usually black or brown) to the GND pin, and the signal wire (usually yellow or white) to a PWM-enabled pin on the Arduino (e.g., pin 9).
Servo Motor 2: Repeat the process for the second servo, connecting it to another PWM-enabled pin (e.g., pin 10) on the Arduino.
Here’s a simple wiring diagram:
Servo 1: 5V (Red) → Arduino 5V, GND (Black) → Arduino GND, PWM (Yellow/White) → Pin 9
Servo 2: 5V (Red) → Arduino 5V, GND (Black) → Arduino GND, PWM (Yellow/White) → Pin 10
The Basics of Servo Motor Control
To control the servo motor’s position, you need to send a Pulse Width Modulation (PWM) signal. The width of the pulse determines the angle the servo will move to. A standard servo usually accepts a PWM signal in the range of 0 to 180 degrees, where 0 corresponds to one extreme position, and 180 corresponds to the other extreme.
Luckily, Arduino makes controlling servo motors incredibly easy with the Servo library. This library provides the functions needed to control the angle of the servo motors. You don’t have to worry about the intricate details of generating PWM signals yourself—Arduino handles all of that for you.
To use the Servo library, simply include it at the beginning of your code:
Once the library is included, you can create servo objects to control each motor. For example:
Servo servo1; // Servo motor 1
Servo servo2; // Servo motor 2
Now you’re ready to begin writing the code to control your servo motors. Let’s look at the basics of the code before diving into controlling two servo motors simultaneously.
Writing the Code to Control Two Servo Motors
Now that we understand how servos work and how to wire them up to the Arduino, it's time to write the code that will control two servo motors. Here’s a simple example to get started:
First, let's create a simple sketch where the two servos move to different positions. We'll set the servo motors on pins 9 and 10, and they will rotate to 0 and 90 degrees, respectively.
#include // Include the Servo library
// Attach the servo motors to the respective pins
servo1.attach(9); // Pin 9 for servo1
servo2.attach(10); // Pin 10 for servo2
// Move servo1 to 0 degrees
// Move servo2 to 90 degrees
delay(1000); // Wait for 1 second
// Move servo1 to 90 degrees
// Move servo2 to 0 degrees
delay(1000); // Wait for 1 second
In this code, the setup() function runs once when the Arduino starts, where we attach the servo motors to pins 9 and 10. The loop() function continuously runs the code inside it, making the servos rotate between two positions with a 1-second delay between each action.
Step 2: Controlling Two Servo Motors Simultaneously
If you want both servos to move simultaneously (at the same time), you can simply call the write() function for both servos in the same iteration of the loop.
Here’s a modified version of the code where both servos move to the same angle simultaneously:
#include // Include the Servo library
// Attach the servo motors to the respective pins
// Move both servos to 90 degrees at the same time
delay(1000); // Wait for 1 second
// Move both servos to 0 degrees at the same time
delay(1000); // Wait for 1 second
Step 3: Smooth Servo Movement
In real-world applications, you may not want the servo motors to move abruptly from one position to another. Instead, a smoother transition is often desired. To achieve this, we can use the writeMicroseconds() function or a technique called “sweeping” that gradually moves the servo from one position to another.
Let’s write code to smoothly transition the servo motors from 0 to 180 degrees:
// Sweep both servos from 0 to 180 degrees
for (int pos = 0; pos <= 180; pos++) {
delay(15); // Wait for the servo to reach the position
// Sweep both servos from 180 to 0 degrees
for (int pos = 180; pos >= 0; pos--) {
In this code, we use a for loop to gradually increase or decrease the servo position. The delay(15) ensures that the servos move smoothly, and the delay time can be adjusted to control the speed of the movement.
Controlling two servo motors with an Arduino is a fantastic starting point for any robotics or automation project. With just a few lines of code, you can create motion, interaction, and even complex behaviors in your projects. As you gain more experience, you can refine your code to incorporate more sophisticated movement patterns, feedback systems, or sensor integrations.
By understanding the fundamentals of servo motors and Arduino programming, you open up countless possibilities for DIY electronics projects, from basic automation to advanced robotics. The only limit is your creativity!
Feel free to experiment with different angles, speeds, and movements to enhance your project further. With the knowledge gained in this article, you’re now well-equipped to tackle more complex projects that involve servo motors and Arduino!
Leveraging innovations in modular drive technology, Kpower integrates high-performance motors, precision reducers, and multi-protocol control systems to provide efficient and customized smart drive system solutions.
Update:2025-10-15
Contact Kpower's product specialist to recommend suitable motor or gearbox for your product.