How to use array of servo motor
How to use array of servo motor with Arduino? How to use multiple servo motors with Arduino?
Answer
Array of servo motor code
#include <Servo.h>
#define SERVO_NUM 5
Servo servoArray[SERVO_NUM]; // create aaray of servo object
void setup() {
servoArray[0].attach(2); // attaches the servo on pin 2
servoArray[1].attach(3); // attaches the servo on pin 3
servoArray[2].attach(4); // attaches the servo on pin 4
servoArray[3].attach(5); // attaches the servo on pin 5
servoArray[4].attach(6); // attaches the servo on pin 6
// rotate all servo motors to 90°
for (int i = 0; i < SERVO_NUM; i++) {
servoArray[i].write(90);
}
}
void loop() {
}
※ NOTE THAT:
Please do not provide power to multiple servo motors via Arduino 5V pin. Use external power source instead. See below wiring diagram for multiple servo motors
![Arduino Servo Motor Extra Power](https://arduinogetstarted.com/images/tutorial/arduino-servo-motor-extra-power-wiring-diagram.jpg)
This image is created using Fritzing. Click to enlarge image
Buy Arduino
1 × Arduino UNO Buy on Amazon | |
1 × USB 2.0 cable type A/B Buy on Amazon | |
1 × Jumper Wires Buy on Amazon |
Please note: These are Amazon affiliate links. If you buy the components through these links, We will get a commission at no extra cost to you. We appreciate it.