Let's Make Robots!

Control 12 Servos using Arduino Mega

Hi!

I'm a newbie and am currently doing my first ever Hexapod ROBOT with 18servo motors controlled using Arduino Mega ADK. I already control 6 servos using 4 pins from arduino (pins 8,9,10,11). However, when i tried controlling the other 6 servos with 4 other pins, something went wrong. it doesnt word at all. It's seems it's my codes fault. Here's my code:

 

 

 

#include <Servo.h>

 

Servo servoa;             // Define a servo

Servo servob;       

Servo servoc;

Servo servod;

Servo servoe;

Servo servof;

Servo servog;

Servo servoh;

int i,j,k;

int a=90;

int b=90;

int c=90;

int d=90;

int e=90;

int f=90;

int g=90;

int h=90;

 

void setup() { 

  servoa.attach(10);      // RightA(4,6)

  servob.attach(9);       // LeftA(2)

  servoc.attach(8);       // RightB(4,6)

  servod.attach(7);       // LeftB(2)

  servoe.attach(6);       // LeftA(1,3)

  servof.attach(5);       // RightA(5)

  servog.attach(4);       // LeftB(1,3)

  servoh.attach(3);       // RightB(5)

  servoa.write(a);

  servob.write(b);

  servoc.write(c);

  servod.write(d);

  servoe.write(e);

  servof.write(f);

  servog.write(g);

  servoh.write(h);

 

  } 

 

void loop()  {           // Loop through motion tests

delay(5000);

for(i=1;i<=30;i++){

a++;

b--;

c++;

d--;

servoa.write(a); 

servob.write(b); 

servoc.write(c); 

servod.write(d);

delay(50);

}

for(i=1;i<=30;i++){

c--;

d++;

servoc.write(c); 

servod.write(d); 

delay(25);

}

for(i=1;i<=30;i++){

a--;

b++;

e++;

f--;

g++;

h--;

servoa.write(a); 

servob.write(b); 

servoe.write(e); 

servof.write(f);

servog.write(g); 

servoh.write(h);

delay(75);

}

for(i=1;i<=30;i++){

g--;

h++;

servog.write(g); 

servoh.write(h);

delay(25);

}

for(i=1;i<=30;i++){

e--;

f++;

servoe.write(e); 

servof.write(f);

delay(25);

}

}

 

 

Can somebody please tell what's wrong with this? Any help would be much appreciated. Thank you!

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Hi Sir Oddbot,

Your work really help a lot with the problems other than this. After checking Arduino 0022, it made me realize that Arduino 1.0.1 is way updated than 0022 and it's not its fault for not recognizing the program properly. I tried loading the program direcly to the Arduino board but it doesnt work. Then I tried again in Arduino Simulator using other PC and failed. Load it directly to board and presto it works just fine. 

Now, I'm working on using 12 servos controlled by 8 pins. I'm using a powersupply with 5V (45A max). Setup works fine when 10servos are connected but more than that, the robot malfunctions or the desired position is not reached instead it goes directly to its minimum or maximum position. Also, when I connect the one pin to a servo, the setup's fine only until 7 pair, with 8 pins to 8 servo, it malfunctions. Btw, I am using SG5010 servo motor.

It makes me feel like an old man :(

If your robot is not working with more than 10 servos then you probably have a power problem. Even if your power supply can deliver 45A your wiring and switch might be under rated.

I could not find reliable specifications for that servo. In some cases they rated it for 12Kg/cm desite the fact it had plastic gears and output shaft. I would guess the stall current to be about 1.5A - 2A.

You need wires and a switch rated for 20A - 25A otherwise you will have all sorts of problems. You may also need some noise suppression capacitors.

 

I removed the aligator clip and connect the supply using much thicker wire and now it works all right. Thank!

Oops! My bad. It's just an old habit. Wont mention again. :D

I'm using an aligator clip from the supply to the robot, I'll now search for replacement. 

Wow 1.5-2A would really be a pain. I thought the specification 450mA is rated for it's stall. 

Ok! I'll try #8 or #9 wire. If the robot requires that amount of current, my battery requirement would be tooo humongous. 

Hi Weisheil, I managed to get 15 servos working on my Arduino Mega ADK and thought I'd share a snippet of the code with you here to see the approach I took.

#include <Servo.h>

#define number_of_servos 15
int servoPins[] = {23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 48};
Servo servos[number_of_servos];
int servoPosMax[] = {120, 120, 120, 120, 120, 120, 120, 120, 120, 120, 120, 120, 120, 120, 120};
int servoPosMin[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int servoPosSafeStart[] = {10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10};

void initServos() {
  int i;
  for (i=0; i<number_of_servos; i++) 
  {
    servos[i].attach(servoPins[i]); // servo setup
    servos[i].write(servoPosSafeStart[i]);
    Serial.print(i, DEC);
    Serial.print(", ");
  }
  Serial.println(" ");
}

void setServo(int num, int deg) {
  deg = constrain(deg, servoPosMin[num], servoPosMax[num]);
  servos[num].write(deg);
}


void setup() {
  initServos();
}
void loop() {
  // your code here, sample should move all through 180deg and back
  int j;
  int i;
  int pos;
  for (j=0; j<360; j += 20) {
    if (j <= 180)
      pos = j;
    else
      pos = 360 - j;
    
    for (i=0; i<number_of_servos; i++) {
      setServo(i,pos);
      delay(50);
    }
    
  }
}

This compiles in Arduino v1.0.1 but was not tested on an Arduino (the functions work fine but I haven't tested the sample code in the loop).

Hope this is of some use to you.

Hi markcra,

I'll try it. I believe this involves PC to board interface?

"Serial.print(i, DEC);

    Serial.print(", ");
  }
  Serial.println(" ");"

But, I still dont know how to control my robot directly from PC. Do I have to use another program for this?

Thanks!

The Serial calls were throw-backs to some debugging I was doing. Without setting the baudrate for the Serial communications those calls won't do anything.

My code is supposed to be stand-alone and just moves all the servos, control would be implemented in the loop() function by checking some inputs or communications.

I've not tried computer control but may take a look at it for one of my projects. I'll post back here when I get a chance.

I somehow get the gist of it. I removed "serial" related codes and it verifies all right. I'll gonna work on my programming later and will try to implement it in an array. Right now, I'm having trouble with the supply. Thanks for the info! Hope to get some more later :D

Have a look at my tip / walkthrough on driving up to 48 servos here: http://letsmakerobots.com/node/25923

This was using a Spider controller but it is basically an Arduino Mega with a better power supply and pin connectors for servos. I use writeMicroseconds() for better servo control but that is up to you.

I tried loading the program to Arduino Simulator excluding all related to IR from #include <Servo.h> to setup () but still the same error occurs when I load my program. It says: 

Unknown command: attach(29,800,2200)

sv[0].attach(29,800,2200);

 

I tried using only 4 pins for servo control it loads properly but with more than 4 it shows. What will i do to execute this properly? Thank you!