How to control 6 servo motors with an Arduino Mega board? I am trying to make autonomous hexapod with an Arduino Mega board. I have followed tripod mechanism of hexapod movement. First I could control simultaneously legs (0, 2, 4) and (1, 3, 5) separately but whenever I tried to combine both i.e control 6 legs at a time I could not see any movement in the hexapod. Even in the IDE I am not getting any error.

I have connected the legs (0 to 5) to signal pins (2 to 7) respectively. I have connected a common +5v voltage and common ground voltage to individual legs using a breadboard. Can anyone give me a proper solution to overcome this issue and further would like to know how to control multiple servos and source code for it?

Here is my Arduino source code for controlling 6 servo motors.

#include &lt;Servo.h&gt;
Servo myservo[6]; 
int f=0;// create servo object to control a servo
//[0] -tibia , [1] -femur , [2] -coxa motors
void setup() {
  myservo[0].attach(2);
  myservo[1].attach(3);
  myservo[2].attach(4);
  myservo[3].attach(5);
  myservo[4].attach(6);
  myservo[5].attach(7);
}

void loop() {
  //alternatively making hexapod stand at its place
  //f is just flag variable to make it stop
  if(f==0) {
    myservo[0].write(130);
    myservo[2].write(130);
    myservo[4].write(90);
    myservo[1].write(130);
    myservo[3].write(90);
    myservo[5].write(0);
    f=1;
  }
}


Note the values that are added above are calculated separately for individual legs to mount legs on ground.
The Arduino Mega can handle at most 40mA on the I/O pins, and 200mA on the 5V/GND pins. Six servos will draw far more current than the max of 200mA. You could try hooking up the servo power to a separate power source and the data pins to the Arduino. Hope this helps!