小编
Published2025-09-16
Introduction to Dual Servo Control with Arduino
Servo motors are the unsung heroes of robotics and automation. Their precision, compact design, and ability to hold positions under load make them indispensable for projects ranging from robotic arms to camera gimbals. But what happens when you need two servo motors working in harmony? This guide will walk you through everything you need to know about controlling dual servo motors with Arduino, complete with code examples, wiring diagrams, and pro tips.
Why Use Two Servo Motors?
Using two servos unlocks endless possibilities:
Robotic arms with multiple joints Pan-tilt camera systems for surveillance or photography Interactive art installations with moving parts Automated pet feeders with dual-axis control
The key to success lies in synchronizing their movements while avoiding power issues or signal conflicts.
Arduino Uno or Nano 2x SG90 or MG90S servo motors Breadboard and jumper wires 5V power supply (optional for high-torque applications) USB cable for Arduino
Power Management: Servos can draw significant current. For small servos, the Arduino’s 5V pin may suffice, but for larger loads, use an external 5V supply connected to the breadboard’s power rails. Signal Wires: Connect each servo’s signal (yellow/orange) wire to separate PWM-enabled Arduino pins (e.g., 9 and 10). Ground Connection: Link all grounds (Arduino, servos, and external power) together.
Basic Dual Servo Sweep Code
Let’s start with a simple program that moves both servos back and forth simultaneously.
Servo servo1; Servo servo2; int pos = 0;
void setup() { servo1.attach(9); servo2.attach(10); }
void loop() { for (pos = 0; pos <= 180; pos += 1) { servo1.write(pos); servo2.write(pos); delay(15); } for (pos = 180; pos >= 0; pos -= 1) { servo1.write(pos); servo2.write(pos); delay(15); } }
##### Code Breakdown: - `#include `: Imports the Servo library. - `Servo servo1, servo2`: Creates servo objects. - `attach(pin)`: Assigns PWM pins to servos. - `write(pos)`: Sets servo angle (0–180 degrees). #### Common Pitfalls & Fixes 1. Jittery Movement: Add a capacitor (100µF) across the servo power lines. 2. Incorrect Initial Position: Use `servo.write(90)` in `setup()` to start at midpoint. 3. Power Brownouts: If servos reset unexpectedly, use a dedicated 5V supply. --- ### Advanced Projects and Synchronized Control Now that you’ve mastered basic control, let’s dive into advanced applications where the servos work independently or in coordinated patterns. #### Project: Dual-Axis Pan-Tilt Mechanism Transform your servos into a professional-grade camera or sensor mount! ##### Additional Components: - Pan-tilt bracket kit - Small camera or ultrasonic sensor - M3 screws and nuts ##### Circuit Upgrade: - Mount servos to the bracket (pan servo horizontal, tilt servo vertical). - Keep signal wires connected to pins 9 and 10. #### Independent Servo Control Code Use potentiometers or a joystick to control each servo separately:
Servo panServo; Servo tiltServo; int panPin = A0; int tiltPin = A1;
void setup() { panServo.attach(9); tiltServo.attach(10); }
void loop() { int panAngle = map(analogRead(panPin), 0, 1023, 0, 180); int tiltAngle = map(analogRead(tiltPin), 0, 1023, 0, 180); panServo.write(panAngle); tiltServo.write(tiltAngle); delay(20); }
##### How It Works: - `map()` function: Converts analog input (0–1023) to servo angles (0–180). - Real-time control allows precise positioning. #### Wireless Control via Bluetooth Upgrade your project with an HC-05 Bluetooth module: 1. Connect HC-05 TX/RX to Arduino RX/TX. 2. Use a smartphone app (e.g., "Bluetooth Controller") to send angle values.
SoftwareSerial bluetooth(2, 3); // RX, TX Servo panServo, tiltServo;
void setup() { bluetooth.begin(9600); panServo.attach(9); tiltServo.attach(10); }
void loop() { if (bluetooth.available() >= 4) { char header = bluetooth.read(); if (header == 'P') { int angle = bluetooth.parseInt(); panServo.write(angle); } else if (header == 'T') { int angle = bluetooth.parseInt(); tiltServo.write(angle); } } } ```
Security Camera: Automate patrol patterns using pre-programmed servo sequences. Robotic Bartender: Use two servos to pour liquids accurately. Interactive Sculptures: Create kinetic art that responds to sensors.
Pro Tips for Optimization
Smooth Motion: Use servo.writeMicroseconds() for finer control. Power Saving: Detach servos with servo.detach() when idle. Synchronization: For complex movements, use timestamps instead of delay().
Controlling two servo motors with Arduino opens doors to limitless creativity. Whether you’re building a robot, an automated system, or an art project, the synergy between code and hardware will bring your ideas to life. Experiment with different control methods, integrate sensors, and share your creations with the world!
This guide equips you with the knowledge to tackle dual servo projects confidently. Ready for more? Explore our tutorials on integrating stepper motors or machine learning with Arduino!
Update:2025-09-16
Contact Kpower's product specialist to recommend suitable motor or gearbox for your product.