小编
Published2025-10-15
Certainly! Below is the first part of the article, which explains how to control two servos with an Arduino. The second part will follow in the next response.
.webp)
When you first step into the world of Arduino, one of the most exciting things you'll get to do is control servos. Servos are small motors that are used to create movement in various types of projects, from robotics to automated cameras. And while controlling a single servo is fairly straightforward, controlling two servos opens up a new world of possibilities for your creations.
In this guide, we’ll walk you through the process of controlling two servos using Arduino, so you can create more complex movements in your projects. By the end of this tutorial, you’ll have a solid understanding of how to make your servos move in a controlled way, making your robots and other projects more dynamic.
Before diving into the practical steps, it’s important to understand what a servo is. A servo motor is a small, self-contained motor that includes a gear system and a feedback mechanism. This allows the motor to be positioned precisely to a certain angle, making it ideal for use in applications where accuracy is critical, such as robotics and automation.
A typical servo can rotate between 0 and 180 degrees. The beauty of servos lies in their ability to move to specific angles, allowing for controlled, repeatable movements.
Understanding Arduino and Servo Libraries
To get started with controlling servos on your Arduino, you’ll first need to understand the basic functions available in the Arduino ecosystem. Arduino makes it very easy to control servos using a built-in library, aptly called Servo. This library allows you to send control signals to the servo motor to position it at any angle between 0 and 180 degrees.
You don’t need any special hardware other than an Arduino board and a few servos to get started. The power of Arduino lies in its simplicity and accessibility, making it the go-to choice for hobbyists and engineers alike.
Basic Components Required
To begin controlling two servos, you'll need a few basic components:
Arduino Board: Any Arduino model (Uno, Nano, etc.) will work fine for this project.
Two Servo Motors: Standard 9g servos (like the SG90) are commonly used in DIY projects.
Jumper Wires: To connect everything.
External Power Supply (optional but recommended for larger servos): Servos can draw a lot of current, and an external power supply may be needed to prevent overloading the Arduino board.
Breadboard (optional): For easier wiring, though not strictly necessary.
Wiring the Servos to Arduino
Before we start writing the code, let’s first wire up our servos to the Arduino. The typical wiring for a servo motor involves three pins:
Signal Pin (PWM Pin): This sends the control signal to the servo.
VCC (Power Pin): This supplies power to the servo motor, usually 5V.
GND (Ground Pin): This completes the circuit.
For two servos, the setup is essentially the same as for one, but you'll need to connect each servo to a different PWM pin on the Arduino.
Connect the signal pin of the first servo to pin 9 on the Arduino.
Connect the signal pin of the second servo to pin 10 on the Arduino.
Connect both VCC pins (power) to the 5V pin on the Arduino.
Connect both GND pins (ground) to one of the GND pins on the Arduino.
Installing the Servo Library
Now that the hardware is in place, we’ll need to install the Arduino Servo library. Fortunately, this library comes pre-installed with the Arduino IDE, so you don’t need to worry about downloading anything extra.
To check if the library is available:
Go to Sketch > Include Library.
In the list of libraries, check for Servo.
If it’s not already installed, you can search for it in the Library Manager and install it. This library gives you access to easy-to-use functions that control your servos.
Now that we’ve covered the basics of the components and wiring, let’s dive into the code and see how to control two servos with Arduino.
Writing the Code to Control Two Servos
To control the servos, we’ll be using the Servo library, which provides functions like attach(), write(), and writeMicroseconds(). These allow us to tell the servo to move to a specific angle (between 0 and 180 degrees).
Here’s a simple example code to control two servos on an Arduino:
#include // Include the Servo library
// Create Servo objects for two servos
// Attach the servo objects to their respective pins
servo1.attach(9); // Servo 1 connected to pin 9
servo2.attach(10); // Servo 2 connected to pin 10
// Move servo 1 to 90 degrees
servo1.write(90); // Servo 1 moves to 90 degrees
delay(1000); // Wait for 1 second
// Move servo 2 to 45 degrees
servo2.write(45); // Servo 2 moves to 45 degrees
delay(1000); // Wait for 1 second
// Move servo 1 back to 0 degrees
servo1.write(0); // Servo 1 moves to 0 degrees
delay(1000); // Wait for 1 second
// Move servo 2 to 135 degrees
servo2.write(135); // Servo 2 moves to 135 degrees
delay(1000); // Wait for 1 second
#include : This line includes the Servo library, which gives you access to all the necessary functions to control the servo motors.
Servo servo1; and Servo servo2;: Here, we define two servo objects. These objects will be used to control the actual servos.
servo1.attach(9); and servo2.attach(10);: These lines attach the servo objects to specific pins on the Arduino (pins 9 and 10).
servo1.write(90);: This tells servo 1 to move to a 90-degree position. The value can range from 0 (full left) to 180 (full right).
delay(1000);: After each movement, the delay function pauses the program for a set number of milliseconds (1000 ms = 1 second).
Testing and Troubleshooting
Once the wiring is complete and the code is uploaded to your Arduino, you should see the servos start moving according to the code. If things don’t work as expected, here are a few troubleshooting tips:
Double-check all your connections.
Ensure the servos are getting enough power. If the Arduino’s 5V pin isn’t providing sufficient current, you may need to use an external power supply.
Check that you’ve attached the servos to the correct PWM pins (pins 9 and 10 in this case).
Going Beyond: Advanced Control
Now that you’ve got the basics down, you can start exploring more advanced applications:
Servo Speed Control: You can make your servos move more slowly by adding some delay between small angle changes.
Multiple Movements: You can use more than two servos in your projects and control them independently by attaching them to different pins.
PWM Control: For more precise control, you can adjust the servo movement speed using PWM (Pulse Width Modulation).
Part 2 will continue shortly with more tips and advanced techniques on controlling two servos with Arduino.
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.