Help! Arduino mega project not working

Hi all, hoping I could get some thoughts on why my project isn’t working. I’m using an arduino mega and have 14 servos attached. These include 1 x 12g micro servo Here, 7 x 9g micro servos Here, and 6 x 56g servos Here. I’ve made a simplified drawing of the circuit, the three way connectors mentioned are these ones Here, and that’s all the links I can put! By my reckoning 5v works as a good middle ground for all the servos and 20a should supply them all, particularly as the arduino mega has its own power supply, but the 24v power supply is only showing maximum 0.4A when everything is connected and powered on and the only servo that is doing anything is the 12g micro servo which is going crazy. My code is as follows :


#include <Servo.h>

Servo head;

Servo mouth;

Servo eyeleft;

Servo eyeright;

Servo bneckRL;

Servo uneckRL;

Servo bneckUD;

Servo uneckUD;

Servo tail;

Servo wings;

Servo LtoeMid;

Servo LtoeInd;

Servo RtoeMid;

Servo RtoeInd;

void setup() {

head.attach(9);

mouth.attach(10);

eyeleft.attach(8);

eyeright.attach(7);

bneckRL.attach(6);

uneckRL.attach(5);

bneckUD.attach(4);

uneckUD.attach(3);

tail.attach(2);

wings.attach(15);

LtoeMid.attach(11);

LtoeInd.attach(12);

RtoeMid.attach(13);

RtoeInd.attach(14);

head.write(45);

eyeleft.write(90);

eyeright.write(30);

mouth.write(40);

bneckRL.write(90);

uneckRL.write(90);

bneckUD.write(90);

uneckUD.write(90);

tail.write(90);

wings.write(90);

LtoeMid.write(110);

LtoeInd.write(180);

RtoeMid.write(180);

RtoeInd.write(140);

// put your setup code here, to run once:

}

void loop() {

// put your main code here, to run repeatedly:

for (int pos = 90; pos >= 60; pos -= 1) {

eyeleft.write(pos);

delay(500);

}

for (int pos = 30; pos >=0; pos -= 1) {

eyeright.write(pos);

delay (500);

}

for (int pos = 60; pos <=90; pos += 1) {

eyeleft.write(pos);

delay(500);

}

for (int pos = 0; pos <=30; pos += 1){

eyeright.write(pos);

delay(500);

}

for (int pos = 45; pos <= 60; pos += 1) {

head.write(pos);

delay(1000);

}

for (int pos = 60; pos >= 45; pos -= 1) {

head.write(pos);

delay(1000);

}

for (int pos = 90; pos <= 150; pos += 1) {

bneckUD.write(pos);

delay(1000);

}

for (int pos = 90; pos >= 50; pos -= 1) {

uneckUD.write(pos);

delay(1000);

}

for (int pos = 90; pos <= 150; pos += 1) {

tail.write(pos);

delay(500);

}

for (int pos = 180; pos >= 150; pos -= 1) {

RtoeMid.write(pos);

delay(1000);

}

for (int pos = 150; pos >= 90; pos -= 1){

tail.write (pos);

delay(1000);

}

for (int pos = 150; pos <= 180; pos += 1){

RtoeMid.write (pos);

delay (1000);

}

for (int pos = 90; pos <= 150; pos += 1) {

wings.write(pos);

delay (1000);

}

for (int pos = 150; pos >=90; pos -= 1){

wings.write(pos);

delay(1000);

}

for (int pos = 90; pos <= 150; pos += 1){

wings.write(pos);

delay (1000);

}

for (int pos = 150; pos >=90; pos -= 1){

wings.write(pos);

delay(1000);

}

for (int pos = 40; pos >= 20; pos -= 1) {

mouth.write(pos);

delay(1000);

}

for (int pos = 20; pos <= 40; pos += 1) {

mouth.write(pos);

delay(1000);

}

for (int pos = 40; pos >= 10; pos -= 1) {

mouth.write(pos);

delay(1000);

}

for (int pos = 10; pos <= 40; pos += 1) {

mouth.write(pos);

delay(1000);

}

for (int pos = 50; pos <= 90; pos += 1) {

uneckUD.write(pos);

delay(1000);

}

for (int pos= 90; pos >= 60; pos -= 1) {

eyeleft.write(pos);

delay(100);

}

for (int pos = 30; pos >=0; pos -= 1) {

eyeright.write(pos);

delay (100);

}

for (int pos = 40; pos >= 5; pos -= 1) {

mouth.write(pos);

delay(500);

}

for (int pos = 90; pos <= 150; pos += 1) {

tail.write(pos);

delay(500);

}

for (int pos = 180; pos >= 150; pos -= 1) {

RtoeMid.write(pos);

delay(1000);

}

for (int pos = 90; pos <= 150; pos += 1){

wings.write(pos);

delay (1000);

}

for (int pos = 150; pos >=90; pos -= 1){

wings.write(pos);

delay(1000);

}

for (int pos = 5; pos <= 40; pos += 1) {

mouth.write(pos);

delay(1000);

}

for (int pos = 150; pos <= 180; pos += 1){

RtoeMid.write (pos);

delay (500);

}

for (int pos = 60; pos <=90; pos += 1) {

eyeleft.write(pos);

delay(500);

}

for (int pos = 0; pos <=30; pos += 1){

eyeright.write(pos);

delay(500);

}

for (int pos = 150; pos >= 90; pos -= 1){

tail.write (pos);

delay(1000);

}

for (int pos = 90; pos <= 150; pos += 1){

wings.write(pos);

delay (1000);

}

for (int pos = 150; pos >=90; pos -= 1){

wings.write(pos);

delay(1000);

}

for (int pos = 90; pos <= 150; pos += 1){

wings.write(pos);

delay (1000);

}

for (int pos = 150; pos >=90; pos -= 1){

wings.write(pos);

delay(1000);

}

for (int pos = 45; pos >= 30; pos -= 1) {

head.write(pos);

delay(1000);

}

for (int pos = 30; pos >=0; pos -= 1) {

eyeright.write(pos);

delay (500);

}

for (int pos = 90; pos <= 150; pos += 1){

wings.write(pos);

delay (1000);

}

for (int pos = 150; pos >=90; pos -= 1){

wings.write(pos);

delay(1000);

}

for (int pos = 40; pos >= 10; pos -= 1) {

mouth.write(pos);

delay(1000);

}

for (int pos = 10; pos <= 40; pos += 1) {

mouth.write(pos);

delay(1000);

}

for (int pos = 0; pos <=30; pos += 1){

eyeright.write(pos);

delay(500);

}

for (int pos = 90; pos <= 140; pos += 1) {

bneckRL.write(pos);

delay(1000);

}

for (int pos = 140; pos >= 90; pos -= 1) {

bneckRL.write(pos);

delay(1000);

}

for (int pos = 30; pos <= 45; pos += 1) {

head.write(pos);

delay(1000);

}

for (int pos = 150; pos >= 90; pos -= 1){

tail.write (pos);

delay(1000);

}

for (int pos = 180; pos >= 150; pos -= 1) {

RtoeMid.write(pos);

delay(1000);

}

for (int pos = 150; pos >= 90; pos -= 1){

tail.write (pos);

delay(1000);

}

for (int pos = 150; pos <= 180; pos += 1){

RtoeMid.write (pos);

delay (1000);

}

for (int pos = 150; pos >= 90; pos -= 1) {

bneckUD.write(pos);

delay(1000);

}

}

I’ve tried removing a few of the smaller servos and that doesn’t seem to improve anything. Any help would be massively appreciated thanks in advance!

it is how your code written - only one servo acting in a time and do it very-very slowly.

If my calculation was correct, a full cycle through all the servos will take more than 30 minutes.

There seems to be no gnd connection between 5V and 9V supply...

1 Like

That makes sense, I had it much shorter originally but then had servos moving at incredibly fast speeds and thought I’d gotten the time period meanings wrong (with 1 then being 1 thousandth of a second) so accounted for that with this version

There isn’t! I just had the control wires going to the arduino (please ignore that in the diagram it shows as the middle wire, it is the control wires going to the arduino!) and figured I wouldn’t need a ground wire between them as it was signal being sent and both were connected at mains power. Is this incorrect? If I was to put a ground wire between them how would I do this?

I’m not sure why the one servo would then be going wild though, surely it would also just not be moving?

Make this connection (blue wire)

Connect 5V negative to Mega GND

1 Like

Thank you! Would it just be the one wire over or one wire from the circuit (just before returning to the transformer) going to one ground port of the arduino and then a second wire going from the second ground port back into the circuit, closing the loop?

Can't exactly follow what you are saying but just connect one wire from the 5V negative to a Mega GND pin

To be clear, you must establish a connection between the negative leg of the servo supply, and the negative leg of the Arduino supply. @jim-p has shown you the easiest way to do that.
The reason is, the servo control signals require a return path to establish a circuit.

See post #10

Ah that makes sense, thank you!

Ah brilliant, thank you!

Thank you everyone who has commented! Have now connected up the arduino ground to the 5v circuit, just before everything returns to the negative terminal. I’ve also adjusted the code as suggested, but there’s still only movement from the head and it seems to be going much faster and without breaks than the code would ask for?

Any ideas? Code below

#include <Servo.h>

Servo head;
Servo mouth;
Servo eyeleft;
Servo eyeright;
Servo bneckRL;
Servo uneckRL;
Servo bneckUD;
Servo uneckUD;
Servo tail;
Servo wings;
Servo RtoeMid;


void setup() {
 head.attach(9);
  mouth.attach(10);
  eyeleft.attach(8);
  eyeright.attach(7);
   bneckRL.attach(6);
 uneckRL.attach(5);
 bneckUD.attach(4);
 uneckUD.attach(3);
 tail.attach(2);
 wings.attach(15);

 RtoeMid.attach(13);

 head.write(45);
 eyeleft.write(90);
 eyeright.write(30);
 mouth.write(40);
  bneckRL.write(90);
 uneckRL.write(90);
 bneckUD.write(90);
 uneckUD.write(90);
 tail.write(90);
 wings.write(90);
 
 RtoeMid.write(180);
 
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:
  for (int pos = 90; pos >= 60; pos -= 1) {
  eyeleft.write(pos);
  delay(15);
}
  for (int pos = 30; pos >=0; pos -= 1) {
  eyeright.write(pos);
      delay (15);
}
  for (int pos = 60; pos <=90; pos += 1) {
  eyeleft.write(pos);
  delay(15);
}
  for (int pos = 0; pos <=30; pos += 1){
  eyeright.write(pos);
  delay(15);
}
  for (int pos = 45; pos <= 60; pos += 1) {
   head.write(pos);
    delay(50);
  }
  for (int pos = 60; pos >= 45; pos -= 1) {
     head.write(pos);
     delay(50);
   }
  for (int pos = 90; pos <= 150; pos += 1) {
      bneckUD.write(pos);
      delay(100);
    }
  for (int pos = 90; pos >= 50; pos -= 1) {
    uneckUD.write(pos);
    delay(50);
  }
  for (int pos = 180; pos <= 120; pos += 1) {
  tail.write(pos);
    delay(50);
  }
  for (int pos = 180; pos >= 150; pos -= 1) {
    RtoeMid.write(pos);
    delay(15);
  }
  for (int pos = 120; pos >= 90; pos -= 1){
      tail.write (pos);
      delay(50);
    }
   
  for (int pos = 150; pos <= 180; pos += 1){
      
      RtoeMid.write (pos);
      delay (15);
      }
  for (int pos = 90; pos <= 150; pos += 1) {
    wings.write(pos);
    delay (50);
    } 
  for (int pos = 150; pos >=90; pos -= 1){
      wings.write(pos);
      delay(50);
    }

  for (int pos = 90; pos <= 150; pos += 1){
            wings.write(pos);
            delay (50);
    }
  for (int pos = 150; pos >=90; pos -= 1){
      wings.write(pos);
      delay(50);
  }
  for (int pos = 40; pos >= 20; pos -= 1) {
    mouth.write(pos);
    delay(50);
  }
  for (int pos = 20; pos <= 40; pos += 1) {
    mouth.write(pos);
    delay(50);
  }
  for (int pos = 40; pos >= 10; pos -= 1) {
    mouth.write(pos);
    delay(50);
  }
  for (int pos = 10; pos <= 40; pos += 1) {
    mouth.write(pos);
    delay(100);
  }
  for (int pos = 50; pos <= 90; pos += 1) {
    uneckUD.write(pos);
    delay(50);
  }
  for (int pos= 90; pos >= 60; pos -= 1) {
  eyeleft.write(pos);
  delay(15);
}
  for (int pos = 30; pos >=0; pos -= 1) {
  eyeright.write(pos);
      delay (15);
}
  for (int pos = 40; pos >= 5; pos -= 1) {
    mouth.write(pos);
    delay(50);
  }
  for (int pos = 150; pos <= 180; pos += 1) {
  tail.write(pos);
    delay(50);
  }
  for (int pos = 180; pos >= 150; pos -= 1) {
    RtoeMid.write(pos);
    delay(15);
  }
  for (int pos = 90; pos <= 150; pos += 1){
            wings.write(pos);
            delay (50);
    }
  for (int pos = 150; pos >=90; pos -= 1){
      wings.write(pos);
      delay(50);
  }
  for (int pos = 5; pos <= 40; pos += 1) {
    mouth.write(pos);
    delay(50);
  }
  for (int pos = 150; pos <= 180; pos += 1){
      
      RtoeMid.write (pos);
      delay (25);
      }
  for (int pos = 60; pos <=90; pos += 1) {
  eyeleft.write(pos);
  delay(15);
  }
  for (int pos = 0; pos <=30; pos += 1){
    eyeright.write(pos);
   delay(15);
  }
  for (int pos = 180; pos >= 90; pos -= 1){
      tail.write (pos);
      delay(50);
    }
  for (int pos = 90; pos <= 150; pos += 1){
            wings.write(pos);
            delay (50);
    }
  for (int pos = 150; pos >=90; pos -= 1){
      wings.write(pos);
      delay(50);
  }
  for (int pos = 90; pos <= 150; pos += 1){
            wings.write(pos);
            delay (50);
    }
  for (int pos = 150; pos >=90; pos -= 1){
      wings.write(pos);
      delay(50);
  }
  for (int pos = 45; pos >= 30; pos -= 1) {
   head.write(pos);
    delay(50);
  }
  for (int pos = 30; pos >=0; pos -= 1) {
  eyeright.write(pos);
      delay (15);
  }
  for (int pos = 90; pos <= 150; pos += 1){
            wings.write(pos);
            delay (50);
    }
  for (int pos = 150; pos >=90; pos -= 1){
      wings.write(pos);
      delay(50);
  }
  for (int pos = 40; pos >= 10; pos -= 1) {
    mouth.write(pos);
    delay(50);
  }
  for (int pos = 10; pos <= 40; pos += 1) {
    mouth.write(pos);
    delay(50);
  }
  for (int pos = 0; pos <=30; pos += 1){
    eyeright.write(pos);
   delay(15);
  }
  for (int pos = 90; pos <= 140; pos += 1) {
      bneckRL.write(pos);
      delay(50);
    }
  for (int pos = 140; pos >= 90; pos -= 1) {
      bneckRL.write(pos);
      delay(50);
    }

   for (int pos = 30; pos <= 45; pos += 1) {
   head.write(pos);
    delay(50);
  }
  for (int pos = 150; pos >= 90; pos -= 1){
      tail.write (pos);
      delay(50);
    }
   for (int pos = 180; pos >= 150; pos -= 1) {
    RtoeMid.write(pos);
    delay(15);
  }
  for (int pos = 150; pos >= 90; pos -= 1){
      tail.write (pos);
      delay(50);
    }
  for (int pos = 150; pos <= 180; pos += 1){
      
      RtoeMid.write (pos);
      delay (25);
      }
  for (int pos = 150; pos >= 90; pos -= 1) {
      bneckUD.write(pos);
      delay(50);
    } 

Is it not what you expected? Why?

Ah sorry should have continued with the terms I used before - there’s only movement from the 12g micro servo and it’s moving in ways I wouldn’t expect considering the codes instructions. Also, I am now not getting any movement from any of the servos, but all the wires to the servos are showing that they are receiving 5v so they are receiving power, just not going?

I think now the time to debug the project PROPERLY

Remove all servos but one, and do the same for the code. Show the clear connection diagram for the "single servo circuit". Publish the code.

Exactly what is that 5V, 20A thing you show in the diagram?

Your servo connection sketch suspiciously shows three connections for each servo - left to +5, right to GND, and middle to the arduino. I hope that's not for real, because the usual servo connector has +5 in the middle, signal on one end, GND on the other. I realize it was probably just convenient to draw it that way, but it is extremely misleading, and leads to my wondering what else you may have not shown quite "as it is".