Servo shield

Hi!

Can I control 6 RC servos with servo shield simultaneously? Because I controlled 4 of them with arduino uno and they moved slower when I moved just one.

Thanks.

Which servo shield? The most common servo controller that I have seen is the pca9685 from Adafruit.
This driver does let you control many servos simultaneously.

But so does controlling them directly from the arduino.

A common mistake is also trying to power the servos from the arduino. This is bad on many levels.

Can you describe the problem you are having in greater detail? And include a wiring diagram?

Also what type of servo?

I too suspect the problem is how you are powering that many servos. A detailed connection diagram would help.

Steve

Yes, I will be using pca9685 from Adafruit. Power supply is not the problem in my case. I am just wondering if I can drive 6 servos (each different speed an direction) without any slower movement when I drive only 2 servos.

If they are connected and powered correctly and your code is properly written e.g. without loads of blocking code, then yes you certainly can.

But you can't really control the "speed" and "direction" of a servo. With RC servos what you get to control is the position.

Steve

But you can control how fast it goes to certain position with a for loop.

tlogar:
Power supply is not the problem in my case.

I remain unconvinced. I am not saying that you are wrong. Only that you have not convinced me that you are right about the power.

I controlled 4 of them with arduino uno and they moved slower when I moved just one.

This just screams power problems.

Anyway, the pca9685 is a great way to go. I have about 20 of them scattered around my workbench and various projects. If your time frame permits it, take a look at AliExpress. The are under $2 there with free shipping from China.

I have power supply that can provide 34A of current at 5V and each servo is powered from the supply. I think this shouldn't be a problem. But I heard from someone that the timers can be the problem for this issue.

Timers are generally only a problem if you are using multiple devices/libraries which all want the same timers. But I can't see anything like that in your code (hint, hint!).

Steve

tlogar:
I have power supply that can provide 34A of current at 5V and each servo is powered from the supply.

Can you show us how you have the servos wired up? Again, I am not just assuming that you are wrong. Just looking to confirm that you did not make the same mistake that SSSOOOOOOOOOO many new servo users have made.

But I heard from someone that the timers can be the problem for this issue.

Yes, that can be a problem. I have seen it cause twitching in servos or failure to work at all.
Can you post your sketch here? Remember to use code tags.

#include <Servo.h>
Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;

//Servo pozicija
int poz1 = 90;
int poz2 = 23;
int poz3 = 111;
int poz4 = 146;

//Joystick
int VRx1 = 0;
int VRy1 = 0;
int VRx2 = 0;
int VRy2 = 0;

//Potenciometer
int pot = 0;
int hitrost = 0;

//Tipka za vklop črpalke
const int tipka1 = 2;
int stanjeTipke1;
int zadnjeStanje = LOW;
long zadnjiDebounce = 0;
long zakasnitevDebounce = 50;
const int ledCrpalka = 7;
int branje;
int stanjeCrpalke;
const int rele = 12;

//Stikalo
const int stikalo = 4;
int stanjeStikala;

//Led diode
const int ledManual = 8;
const int ledAuto = 13;
const int ledOperacija = 3;

//Tipka START
const int tipkaStart = 11;
int stanjeStart;

void setup() {
  // put your setup code here, to run once:

  Serial.begin(9600); //

  //Priklop servo motorjev
  servo1.attach(5);
  servo2.attach(6);
  servo3.attach(9);
  servo4.attach(10);

  servo1.write(poz1);
  servo2.write(poz2);
  servo3.write(poz3);
  servo4.write(poz4);

  //Tipka za vklop črpalke in rele
  pinMode(tipka1, INPUT_PULLUP);
  pinMode(ledCrpalka, OUTPUT);
  pinMode(rele, OUTPUT);
  digitalWrite(rele, LOW);

  //Stikalo
  pinMode(stikalo, INPUT_PULLUP);

  //LED indikacija
  pinMode(ledManual, OUTPUT);
  pinMode(ledAuto, OUTPUT);
  pinMode(ledOperacija, OUTPUT);

  //Tipka START
  pinMode(tipkaStart, INPUT_PULLUP);

}

void loop() {
  // put your main code here, to run repeatedly:

  //Branje vrednosti stikala DELUJE!
  stanjeStikala = digitalRead(stikalo);

  //Branje in normiranje vrednosti potenciometra DELUJE!
  pot = analogRead(A4);
  hitrost = map(pot, 0, 1023, 100, 12);
  
  //Branje in normiranje joystickov
  VRx1 = analogRead(A1);
  VRx1 = map(VRx1, 0, 1023, 0, 10);
  VRy1 = analogRead(A0);
  VRy1 = map(VRy1, 0, 1023, 10, 0);
  VRx2 = analogRead(A2);
  VRx2 = map(VRx2, 0, 1023, 0, 10);
  VRy2 = analogRead(A3);
  VRy2 = map(VRy2, 0, 1023, 0, 10);

  //Branje tipke za vklop črpalke DELUJE!
  branje = digitalRead(tipka1);

  //Branje tipke START DELUJE!
  stanjeStart = digitalRead(tipkaStart);

  //MANUAL
  
  if (stanjeStikala == LOW) {

    //LED indikacija DELUJE!
      digitalWrite(ledManual, HIGH);
      digitalWrite(ledAuto, LOW);
      
    
    //Premikanje osi S DELUJE!
    if (VRx1 < 2 && poz1 > 0) {
      (poz1--);
      delay(hitrost);
    }
  
    if (VRx1 > 8 && poz1 <180) {
      (poz1++);
     delay(hitrost);
    }

    servo1.write(poz1);

    //Premikanje osi L DELUJE!
    if (VRy1 < 2 && poz2 > 0) {
      (poz2--);
      delay(hitrost);
   }

   if (VRy1 > 8 && poz2 <180) {
     (poz2++);
     delay(hitrost);
   }

   servo2.write(poz2);

   //Premikanje osi U DELUJE!
   if (VRx2 < 2 && poz3 > 0) {
     (poz3--);
     delay(hitrost);
   }

   if (VRx2 > 8 && poz3 < 180) {
     (poz3++);
      delay(hitrost);
    }

   servo3.write(poz3);  

   //Premikanje osi R DELUJE!
   if (VRy2 < 2 && poz4 > 0) {
     (poz4--);
     delay(hitrost);
    }

    if (VRy2 > 8 && poz4 < 180) {
     (poz4++);
      delay(hitrost);
   }

    servo4.write(poz4);
  
    //Vklop/izklop črpalke DELUJE!
    if (branje != zadnjeStanje) {
      zadnjiDebounce = millis();
    }

    if ((millis() - zadnjiDebounce) > zakasnitevDebounce) {
      if (branje != stanjeTipke1) {
       stanjeTipke1 = branje;
       if (stanjeTipke1 == LOW) {
          stanjeCrpalke = ! stanjeCrpalke;
       }
     }  
    }

   zadnjeStanje = branje;

      digitalWrite(ledCrpalka, stanjeCrpalke);
      digitalWrite(rele, !stanjeCrpalke);
  }

  //začetna pozicija

  if (stanjeStart == LOW) {

    
    for (poz1; poz1 < 90; poz1++) {
        delay(hitrost);
        servo1.write(poz1);
      }

    for (poz1; poz1 > 90; poz1--) {
        delay(hitrost);
        servo1.write(poz1);
      }

    for (poz4; poz4 < 146; poz4++) {
      delay(hitrost);
      servo4.write(poz4);
     }

     for (poz4; poz4 > 146; poz4--) {
      delay(hitrost);
      servo4.write(poz4);
     }

    for (poz3; poz3 < 111; poz3++) {
      delay(hitrost);
      servo3.write(poz3);
    }

    for (poz3; poz3 > 111; poz3--) {
      delay(hitrost);
      servo3.write(poz3);
    }

    for (poz2; poz2 < 23; poz2++) {
      delay(hitrost);
      servo2.write(poz2);
    }

    for (poz2; poz2 > 23; poz2--) {
      delay(hitrost);
      servo2.write(poz2);
    }

  }

  //AUTO 
  
  if (stanjeStikala == HIGH && stanjeCrpalke == LOW) {

    //LED indikacija DELUJE!
    digitalWrite(ledAuto, HIGH);
    digitalWrite(ledManual, LOW);

    //Program 

    if (stanjeStart == LOW) {

      digitalWrite(ledOperacija, HIGH);

      //začetna pozicija

      for (poz1; poz1 < 90; poz1++) {
        delay(hitrost);
        servo1.write(poz1);
      }

      for (poz1; poz1 > 90; poz1--) {
        delay(hitrost);
        servo1.write(poz1);
      }

     for (poz4; poz4 < 146; poz4++) {
      delay(hitrost);
      servo4.write(poz4);
     }

     for (poz4; poz4 > 146; poz4--) {
      delay(hitrost);
      servo4.write(poz4);
     }

    for (poz3; poz3 < 111; poz3++) {
      delay(hitrost);
      servo3.write(poz3);
    }

    for (poz3; poz3 > 111; poz3--) {
      delay(hitrost);
      servo3.write(poz3);
    }

    for (poz2; poz2 < 23; poz2++) {
      delay(hitrost);
      servo2.write(poz2);
    }

    for (poz2; poz2 > 23; poz2--) {
      delay(hitrost);
      servo2.write(poz2);
    }


    //operacija

    for (poz2; poz2 < 35; poz2++) {
      delay(hitrost);
      servo2.write(poz2);
    }
    
    for (poz1; poz1 > 60; poz1--) {
      delay(hitrost);
      servo1.write(poz1);
    }

    for (poz2; poz2 > 20; poz2--) {
      delay(hitrost);
      servo2.write(poz2);
    }

    delay(500);
    digitalWrite(rele, LOW);
    digitalWrite(ledCrpalka, HIGH);
    delay(1000);

    for (poz2; poz2 < 35; poz2++) {
      delay(hitrost);
      servo2.write(poz2);
    }

    for (poz1; poz1 < 170; poz1++) {
      delay(hitrost);
      servo1.write(poz1);
    }

    for (poz2; poz2 < 80; poz2++) {
      delay(hitrost);
      servo2.write(poz2);
    }

    for (poz3; poz3 > 75; poz3--) {
      delay(hitrost);
      servo3.write(poz3);
    }

    for (poz4; poz4 > 70; poz4--) {
      delay(hitrost);
      servo4.write(poz4);
     }

     for (poz1; poz1 > 15; poz1--) {
      delay(hitrost);
      servo1.write(poz1);
    }

    for (poz4; poz4 < 146; poz4++) {
      delay(hitrost);
      servo4.write(poz4);
     }

     for (poz3; poz3 < 111; poz3++) {
      delay(hitrost);
      servo3.write(poz3);
    }

    for (poz1; poz1 < 120; poz1++) {
      delay(hitrost);
      servo1.write(poz1);
    }

    for (poz2; poz2 > 20; poz2--) {
      delay(hitrost);
      servo2.write(poz2);
    }

    delay(1000);
    digitalWrite(rele, HIGH);
    digitalWrite(ledCrpalka, LOW);
    delay(500); 

    for (poz2; poz2 <35; poz2++) {
      delay(hitrost);
      servo2.write(poz2);
    }

    for (poz1; poz1 > 90; poz1--) {
      delay(hitrost);
      servo1.write(poz1);
    }

    for(poz2; poz2 > 23; poz2--) {
      delay(hitrost);
      servo2.write(poz2);
    }

   digitalWrite(ledOperacija, LOW); 
     
  }
 }
}

Robotska roka_schem.pdf (1.21 MB)

Awesome. Servos powered from the external supply rather than from the arduino. Great!

You should consider loading up the knob tutorial.
Modifiy it to write to all of your servos.
The servos should all move to match the input of the potentiometer.

Actually, even if you DO get the servo driver, that is a good idea. Modify the knob tutorial to work with the driver examples.

You have lots of delays in loop(). The more servos you run the more delays are active and so the slower the loop runs.

Steve

slipstick:
You have lots of delays in loop(). The more servos you run the more delays are active and so the slower the loop runs.

Steve

So if I do it with millis this will do the trick? Because last year when I wrote the code I didn't know about delay with millis so I didn't do it with millis.

vinceherman:
Awesome. Servos powered from the external supply rather than from the arduino. Great!

You should consider loading up the knob tutorial.
Modifiy it to write to all of your servos.
The servos should all move to match the input of the potentiometer.

Actually, even if you DO get the servo driver, that is a good idea. Modify the knob tutorial to work with the driver examples.

I don't use potenciometers but two joysticks, because it's easier to control. Here you can check video of this project. https://www.youtube.com/watch?v=ZnPdHYBl5Z0