0
0
AutocadHow-ToBeginner · 3 min read

How to Control Multiple Servos with Arduino Easily

To control multiple servos with Arduino, use the Servo library to create separate servo objects for each motor. Attach each servo to a different PWM pin and control them independently by writing angles with servo.write().
📐

Syntax

Use the Servo library to control servos. Create a Servo object for each motor, attach it to a pin, and set its angle.

  • Servo servoName; - declares a servo object.
  • servoName.attach(pin); - connects the servo to a PWM pin.
  • servoName.write(angle); - moves the servo to the specified angle (0-180 degrees).
arduino
#include <Servo.h>

Servo servo1; // create servo object
Servo servo2;

void setup() {
  servo1.attach(9);  // attach servo1 to pin 9
  servo2.attach(10); // attach servo2 to pin 10
}

void loop() {
  servo1.write(90);  // set servo1 to 90 degrees
  servo2.write(45);  // set servo2 to 45 degrees
  delay(1000);
}
💻

Example

This example shows how to control two servos independently. Each servo moves to different angles with a delay, demonstrating multiple servo control.

arduino
#include <Servo.h>

Servo servo1;
Servo servo2;

void setup() {
  servo1.attach(9);  
  servo2.attach(10); 
}

void loop() {
  servo1.write(0);    // servo1 to 0 degrees
  servo2.write(180);  // servo2 to 180 degrees
  delay(2000);

  servo1.write(180);  // servo1 to 180 degrees
  servo2.write(0);    // servo2 to 0 degrees
  delay(2000);
}
Output
Servos move to 0 and 180 degrees alternately every 2 seconds.
⚠️

Common Pitfalls

Common mistakes when controlling multiple servos include:

  • Not creating separate Servo objects for each servo.
  • Attaching multiple servos to the same pin.
  • Using pins that do not support PWM signals.
  • Powering too many servos directly from the Arduino 5V pin, causing voltage drops.

Always use separate pins and provide external power if controlling many servos.

arduino
// Wrong way: Using one Servo object for multiple servos
#include <Servo.h>

Servo servo;

void setup() {
  servo.attach(9);  // Only one servo attached
}

void loop() {
  servo.write(90);  // Only controls one servo
  delay(1000);
}

// Right way: Separate Servo objects
#include <Servo.h>

Servo servo1;
Servo servo2;

void setup() {
  servo1.attach(9);
  servo2.attach(10);
}

void loop() {
  servo1.write(90);
  servo2.write(45);
  delay(1000);
}
📊

Quick Reference

Tips for controlling multiple servos:

  • Use the Servo library and create one object per servo.
  • Attach each servo to a unique PWM pin.
  • Use servo.write(angle) to set position.
  • Provide external power for many servos to avoid Arduino resets.
  • Use delay() to give servos time to move.

Key Takeaways

Create a separate Servo object for each servo motor.
Attach each servo to a unique PWM pin on the Arduino.
Use servo.write(angle) to control servo position from 0 to 180 degrees.
Avoid powering many servos directly from Arduino; use external power.
Delays help servos reach their target positions smoothly.