Make servo's turn in sync

Hello everyone,
my name is Senna and i got a project where 2 servo motors need to turn on the exact same speed.
The servos are continuous. The problem is that 1 turns faster than the other one.
They don't have a potentiometer.

#include <Servo.h>

Servo servo_L;
Servo servo_R;

int buttonState1 = 0;
int buttonState2 = 0;
int pos= 1500;

int var1=0;
int var2=0;


void setup(){
  
  Serial.begin(115200);
//  servo_L.attach(5);  //Left servo Pin3
//  servo_R.attach(6);  //Right servo Pin3
  pinMode(3, INPUT);  //Button 1
  pinMode(4, INPUT);  //Button 2


}





void loop(){
  
 buttonState1 = digitalRead(3);
  buttonState2 = digitalRead(4);
  
  if (buttonState1 == 1) {
    
    servo_L.attach(5);  //Left servo Pin3
   servo_R.attach(6);  //Right servo Pin3
                   
        servo_L.writeMicroseconds(2000);                                 // tell servo's to go to position in variable 'pos'
        servo_R.writeMicroseconds(2000);
        Serial.println("Button 1, turning");
      delay(25); 
     }
  
else if (buttonState1== 0){
  servo_L.detach();
  servo_R.detach();
//  servo_L.writeMicroseconds(1500);                                 // tell servo's to go to position in variable 'pos'
//  servo_R.writeMicroseconds(1500);
}



if (buttonState2 == 1) {
  servo_L.attach(5);  //Left servo Pin3
  servo_R.attach(6);  //Right servo Pin3
                   
        servo_L.writeMicroseconds(600);                                 // tell servo's to go to position in variable 'pos'
        servo_R.writeMicroseconds(600);
        Serial.println("Button 2, turning in steps");
      delay(2000); 
     }
  
if (buttonState2== 0){
  servo_L.detach();
  servo_R.detach();
//  servo_L.writeMicroseconds(1500);                                 // tell servo's to go to position in variable 'pos'
//  servo_R.writeMicroseconds(1500);
}
}

This is my code. I tried fine-tuning via software but couldn't get them to turn the exact same speed.
Maybe someone can tell me what the problem is, and how to fix it.
Can't wait to read the reply's.
Kind regards,
Senna Nieuwland

Have you tried using shaft encoders on each device to measure their speed, and feed that back to the ex-servos?

The problem is caused by the fact that the servos are not matched in their performance and nor should you expect them to be if they are simple hobby servos with no position feedback

Even if you manage to fine tune them using writeMicroseconds() at one speed they will probably not stay synchronised for long at that speed, will not be synchronised at other speeds and the speed will change when the load on the servos changes

Your only hope of synchronising them is to add encoders to both of them so that you can measure their rotational speed and apply corrections to them make it match

Perhaps you didn't try the right thing. Were you not able to slow down the faster motor by using a number closer to 1500?

Aren't you missing an 'else'? If you push both buttons the servos will be jerking between full forward and full reverse. That can be good for them! An with only one button pressed the servos will go 25 milliseconds forward or 2000 millisecond backward before being detached.

Try this:

void loop()
{
  if (digitalRead(3) == HIGH)
  {
    servo_L.attach(5);  //Left servo Pin3
    servo_R.attach(6);  //Right servo Pin3

    servo_L.writeMicroseconds(2000);
    servo_R.writeMicroseconds(2000);
    Serial.println("Button 1, turning");
    delay(25);
  }
  else if (digitalRead(4 == HIGH)
  {
    servo_L.attach(5);  //Left servo Pin3
    servo_R.attach(6);  //Right servo Pin3

    servo_L.writeMicroseconds(600);
    servo_R.writeMicroseconds(600);
    Serial.println("Button 2, turning in steps");
    delay(25);
  }
  else
  {
    servo_L.detach();
    servo_R.detach();
    //  servo_L.writeMicroseconds(1500);
    //  servo_R.writeMicroseconds(1500);
  }
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.