I want to declare 11 servo motors in my code. Is there a better way to do that than just declare everyone under each other. The reason is that i have a code that will set the rotation for one at a time. The best would be a list so i could iterate over which server i want to move.

Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;
Servo servo5;
Servo servo6;
void moveServo(int n,int deg){
  servo[n].write(deg); //something like this

  • Generally, to control a servo motor you need to develop a fairly well timed pulse train. Usually you use the PWM hardware inside the processor. The consequence of using software? The pulses may have a bit of jitter and this translates into the servos jittering. If you are willing to use the correct hardware (processor) there are ways around this. Do you wish for such an answer?
    – st2000
    Nov 6 '19 at 19:12
  • Its for a robot arm so i want to send message from a raspberry pi, that i want to move servo[1] 20 degrees. so i can move a joint at a time.
    – vegiv
    Nov 6 '19 at 19:37
  • Depending on the specific processor in the Arduino you are using, there may not be enough PWM hardware generators to provide the pulse train to control 11 servos. You can resort to using software to create the pulse trains. But this may end up causing some jitter in the servo motors. Likely more so during serial communications with the Raspberry Pi.
    – st2000
    Nov 6 '19 at 19:41
  • Servo.h library can control 12 servos in a '328P, and 48 in a 2560.
    – CrossRoads
    Nov 7 '19 at 13:00

Consider using a specific processor / Arduino-board. In this thread there are references to using the Arduino 1280 board with the Arduino Servo Library to control up to 48 servos using a combination of hardware and software.

In C++ when you "instantiate" a class you are making good use of memory space as you do not really create duplicate code. All you create are copies of the variables necessary to support each instantiation. There should only be one copy of the code in memory. To iterate over each instantiation consider using a switch/case statement.


First: Your code would not work, since servo is not defined and thus not an array to iterate over. You would need to first define the array and then fill it with Servo elements.

Servo servo[11];

void setup(){
    for(int i=0;i<11;i++){
        servo[i] = new Servo();

The different Servo objects are created dynamically. In most cases it is bad to use dynamic allocation on a microcontroller with litte RAM, but it is ok, when you only use it once at the start.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.