Delayed movement of multiple Micro Servo SG90 wired to common

Hi All,

I hope I can get some help from the experienced or anyone who has encountered this issue, I am new to robotics and i have been trying to google for a solution on this to no avail.

I am trying to control 4 SG90 micro servo with on a ESP 32 Dev kit powered by 4 AA batteries; no other hardware or electronics were used in this setup other than a joystick wired to one of the pin and connected to a separate power source of 4 AA batteries.

I have managed to make make the control work with the servo with Y and X axis of the joystick movement affecting the 4 servos (Y axis controls 2 servos to go between 0 or 180 degrees, X axis controls the other 2 servos to go between 0 or 180 degrees, button down to move all servos between 0 to 180)

But in this setup i noticed there’s a slight delay one i am trying to execute any of the movement; moving 2 servos always ended up having one moving slightly slower than the other, like a 0.5 seconds or less delay. I wired all of them to a single power source and even tried joining the wires to allow 2 servos to be connected to 1 pin instead of 4 servos to 4 pins, it did not resolve the issue.

Can someone advise whether is this normal for micro servos such as mg90s or sg90 or it could be a programming methodology issue?

The below is my code, a and b are servo names i gave to accommodate 2 x 2 set of servos, skpin and fmpinm are the only 2 pins i used in this latest code to accommodate the 4 servos:

//create conditions for joystick positions, corresponding to movements
  if(xvalue < 300){
    b.write(420);
     
  }
  if(xvalue > 3300){
    b.write(10);
     
  }
   if(yvalue < 300){
    a.write(420);
      
  }
  if(yvalue > 3300){
    b.write(10);
   
  }

  
  if(swvalue <1){
    if(swState == false){
    swState= true;
    a.write(180);
    
    delay(500);
    b.write(180);
    
  }
  else{
    swState=false;
    a.write(0);
    delay(500);
    b.write(0);
    
  }
  Serial.println("swState");
  Serial.print(swState);
  
  Serial.println();
  }
}
 
void setup() {
  setCpuFrequencyMhz(80);
  //Initialize Serial Monitor
  Serial.begin(115200);
  
  //Set device as a Wi-Fi Station
  WiFi.mode(WIFI_STA);

  //Init ESP-NOW
  if (esp_now_init() != ESP_OK) {
    Serial.println("Error initializing ESP-NOW");
    return;
  }
  
  // Once ESPNow is successfully Init, we will register for recv CB to
  // get recv packer info
  esp_now_register_recv_cb(OnDataRecv);

  //code for servos
  ESP32PWM::allocateTimer(0);
  ESP32PWM::allocateTimer(1);
  ESP32PWM::allocateTimer(2);
  ESP32PWM::allocateTimer(3);
  a.setPeriodHertz(50);
  b.setPeriodHertz(50);
  a.attach(skPin,500,2400);
  b.attach(fmPinm,500,2400);

 
  esp_deep_sleep_start();
}

Any help and advice would be greatly appreciated, thank you!

Please post the full sketch

Hi UKHeliBob,

Below is the full sketch for my ESP32:

#include <esp_now.h>
#include <WiFi.h>
#include <ESP32Servo.h>


//Setup Servo objects
Servo a;
Servo b;


//pin setup for skullcap and facemask servo signals

int skPin = 27;
int fmPinm = 26;


//variables to store the default state of the joystick
int xvalue = 0;
int yvalue = 0;
int swvalue = 0;
bool swState = false;
//

//Structure example to receive data
//Must match the sender structure
typedef struct struct_message {
  int x;
  int y;
  int z;
} struct_message;

//Create a struct_message called myData
struct_message IncData;

//callback function that will be executed when data is received
void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
  memcpy(&IncData, incomingData, sizeof(IncData));
  Serial.print("Bytes received: ");
  Serial.println(len);
  Serial.print("x: ");
  Serial.println(IncData.x);
  Serial.print("y: ");
  Serial.println(IncData.y);
  Serial.println("z: ");
  Serial.println(IncData.z);
  Serial.println();
  

  // read the joystick position and store it to x, y, z
  xvalue =IncData.x;
  yvalue =IncData.y;
  swvalue =IncData.z;

//create conditions for joystick positions, corresponding to movements
  if(xvalue < 300){
    b.write(420);
     
  }
  if(xvalue > 3300){
    b.write(10);
     
  }
   if(yvalue < 300){
    a.write(420);
      
  }
  if(yvalue > 3300){
    b.write(10);
   
  }

  
  if(swvalue <1){
    if(swState == false){
    swState= true;
    a.write(180);
    
    delay(500);
    b.write(180);
    
  }
  else{
    swState=false;
    a.write(0);
    delay(500);
    b.write(0);
    
  }
  Serial.println("swState");
  Serial.print(swState);
  
  Serial.println();
  }
}
 
void setup() {
  setCpuFrequencyMhz(80);
  //Initialize Serial Monitor
  Serial.begin(115200);
  
  //Set device as a Wi-Fi Station
  WiFi.mode(WIFI_STA);

  //Init ESP-NOW
  if (esp_now_init() != ESP_OK) {
    Serial.println("Error initializing ESP-NOW");
    return;
  }
  
  // Once ESPNow is successfully Init, we will register for recv CB to
  // get recv packer info
  esp_now_register_recv_cb(OnDataRecv);

  //code for servos
  ESP32PWM::allocateTimer(0);
  ESP32PWM::allocateTimer(1);
  ESP32PWM::allocateTimer(2);
  ESP32PWM::allocateTimer(3);
  a.setPeriodHertz(50);
  b.setPeriodHertz(50);
  a.attach(skPin,500,2400);
  b.attach(fmPinm,500,2400);

 
  esp_deep_sleep_start();
}

void loop() {

}

I am not clear how the servos are wired to the power supply. Does the power to the servos come from the ESP32 or directly from the power supply ? The latter is to be preferred

When you say that there is a delay between the servos moving is it that one starts before the other or that they start and end their movement at the same time but that one moves slower than the other ?

If the latter, what happens if you exchange the servos ? Is it the same physical servo that goes slower or is the slow servo attached to a particular pin ?

I used multiple jumper wires to make a 1-to-4 female+1 male cable to connect the 4 female pins to the servos power and ground from the power supply, the 1 male pin was left unused as it was initially intended to use for supplying the power to the esp32 ( but apparently it can’t do that somehow) .

The servos when triggered to move, they all seems to move together at the same time, but if eg. pin D27 (split signal to 2 servos, lets call them servo A n B) and D26 (split signal to 2 servos, lets call them servo C n D), apparently the servos on D27 will move first with A moving first and 0.3 to 0.5 sec later, B will move, follow by D26’s servos C and D in sequence; somehow they are moving together in but with a very slight delay in between each servos, but comparing A and D, the difference of delay seems more noticeable.

In my code, it was suppose to make sure all of them moved at the same time if swvalue<1, a.write and b.write will happen at the same time. I also tried in my latest code (the current example) to put a delay of (500) in between those 2 sets of servos to see if the delay still occurs, but it seems like Servo A and B still have a slight delay between both of them with A moving first before B moves. same for b.write that attends to servo C and D.

So, in this section of code

  if (swvalue < 1)
  {
    if (swState == false)
    {
      swState = true;
      a.write(180);
      delay(500);
      b.write(180);
    }

why is there a delay() between a.write() and b.write() ?

Too confusing for me. I can only see 2 servos a and b. Are you saying you have two servos connected to a and another 2 to b? Maybe time for a schematic.

And why do you write(420) to them? What do you expect that to do? It’s normally an invalid value.

Steve

It was to test if running them all at the same time and running 2 sets of servos with a delay in between each pin has any effect on the movement delay, apparently it seems like it makes no difference having the delay(500); all the servos are still moving with slight delay no matter the assignment of servos to the pins.

Yes you are correct, 2 servos are connected to A and 2 to B. for the 420 part apparently it works, it was suppose to move to the maximum angle possible, i know i could have put 180 and it wouldnt make any difference since it will stop at the maximum point as well.

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