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
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 |
Disclosure: Some links in this section are Amazon affiliate links. If you make a purchase through these links, we may earn a commission at no extra cost to you.
Additionally, some links direct to products from our own brand, DIYables.
Additionally, some links direct to products from our own brand, DIYables.