Servos twitching before moving

Hello, I am using Arduino Nano Every to control 6 servos. I receive data from ESP32 connected to the nano every via SoftwareSerial and this command tells the nano every which servo and how much to rotate.
My issue: When I send the command several (or all) servos twitch (move few degrees then back to original position) for a brief moment and after that the chosen servo rotates.
I tried debbugging all the code but all seems fine. My suspicions are mixing PWM and non-PWM pins but I am really not sure as SoftwareSerial and Servo.h are not dependant on PWM. You can see the chosen pins in my code bellow.

#include <SoftwareSerial.h>
#include <Servo.h>

#define rxPin 3
#define txPin 2
#define index_finger_pin 4
#define middle_finger_pin 5
#define ring_finger_pin 6
#define pinky_finger_pin 7
#define thumb_finger_bend_pin 8
#define thumb_finger_rotate_pin 9

int index_finger_initial_position = 90;
int middle_finger_initial_position = 90;
int ring_finger_initial_position = 90;
int pinky_finger_initial_position = 90;
int thumb_finger_bend_initial_position = 180;
int thumb_finger_rotate_initial_position = 180;

Servo index_finger;
Servo middle_finger;
Servo ring_finger;
Servo pinky_finger;
Servo thumb_finger_bend;
Servo thumb_finger_rotate;
SoftwareSerial mySerial = SoftwareSerial(rxPin, txPin);

String getValue(String data, char separator, int index) {
  int found = 0;
  int strIndex[] = {0, -1};
  int maxIndex = data.length()-1;
  for(int i=0; i<=maxIndex && found<=index; i++){
    if(data.charAt(i)==separator || i==maxIndex){
        found++;
        strIndex[0] = strIndex[1]+1;
        strIndex[1] = (i == maxIndex) ? i+1 : i;
    }
  }
  return found>index ? data.substring(strIndex[0], strIndex[1]) : "";
}

void setup() {
  pinMode(rxPin, INPUT);
  pinMode(txPin, OUTPUT);
  
  Serial.begin(9600);
  mySerial.begin(9600);
  
  index_finger.write(index_finger_initial_position);
  index_finger.attach(index_finger_pin);

  middle_finger.write(middle_finger_initial_position);
  middle_finger.attach(middle_finger_pin);

  ring_finger.write(ring_finger_initial_position);
  ring_finger.attach(ring_finger_pin);

  pinky_finger.write(pinky_finger_initial_position);
  pinky_finger.attach(pinky_finger_pin);

  thumb_finger_bend.write(thumb_finger_bend_initial_position);
  thumb_finger_bend.attach(thumb_finger_bend_pin);

  thumb_finger_rotate.write(thumb_finger_rotate_initial_position);
  thumb_finger_rotate.attach(thumb_finger_rotate_pin);
  Serial.println("STARTED");
}

void loop() {
  if (mySerial.available() > 0) {
    String incomingString = mySerial.readString();
    Serial.println(incomingString);

    char buf[incomingString.length()];
    incomingString.toCharArray(buf, sizeof(buf));
    char *p = buf;
    char *str;
    while ((str = strtok_r(p, ";", &p)) != NULL) { // delimiter is the semicolon
      String row = String(str);
      String servo_name = getValue(row,'=',0);
      String rotation_value_pre = getValue(row,'=',1);
      int rotation_value = rotation_value_pre.toInt();
      Serial.println(servo_name);
      if(servo_name == "index_finger") {              //index_finger
        index_finger.write(rotation_value);
      } else if(servo_name == "middle_finger") {      //middle_finger
        Serial.print("Rotating middle finger to: ");
        Serial.println(rotation_value);
        middle_finger.write(rotation_value);
      } else if(servo_name == "ring_finger") {        //ring_finger
        ring_finger.write(rotation_value);
      } else if(servo_name == "pinky_finger") {       //pinky_finger
        pinky_finger.write(rotation_value);
      } else if(servo_name == "thumb_finger_bend") {   //thumb_finger_bend
        thumb_finger_bend.write(rotation_value);
      } else if(servo_name == "thumb_finger_rotate") { //thumb_finger_rotate
        thumb_finger_rotate.write(rotation_value);
      }
    }
  }
}

An example of the issue would be: I send a line via SoftwareSerial pinky_finger=0; (pinky_finger being at 90 currently) and before the servo goes to 0 few other servos twitch.
Any help would be welcomes as I already tried and tested many situations. I haven't tested using different pins yet still I would like to learn more why would this happen or any debigging approaches.

How are the servos powered?

There seems to be more than 1 hardware serial port on the Every. Google "nano every hardware serial". See pages like this.

Servos are powered separately via power supply 5V 20A, its not powering issue for sure. I am not sure I understand your point about the serial. I use SoftwareSerial and I am happy with it, sent and received data is just as expected to be. Are you saying that switching to hardware serial will fix my servo twitching problem?

I ask about power because it is a common problem that servos are under powered.

No, I was just pointing out that there is another hardware serial port. It is usually preferred to use hardware serial if available. If what you have is working the way that you want, fine. That said, it might be worth a try to see if it makes a difference.

well to be honest, what I want to happen - happens, the servos rotate and they move my shafts and gears to desired location BUT those twitches are causing problems because the other servos are holding load, and load being shaked while adjusting more servos is a mechanical problem in the end goal of the project. I am starting to think about servo driver... PCA9685 was recommended to me.

By the way there are 4 of those arduino nano every, not just one, all of them controlled by a single ESP32, so thats the reason I use SoftwareSerial, I can't have 4 hardware serials on the ESP32. And there are more slaves to come so I need the SoftwareSerial one way or another.

Unless.. I can send data via SoftwareSerial but receive it in the hardware serial... (not sure if that can happen)

Why do you think it might not work? Serial is serial; software serial has limitations but it will work as long as you stay within the limitations.

1 Like

worth a try, I'll report back tomorrow on that test.

It's almost always power. Insufficient current or trying to push that power through inadequate connections - breadboard is a particularly egregious offender there.

What are these servos? What voltage/current do they need? Also, remember that nameplate power may be "optimistic".

behaviour is observed on 2 slaves (arduino nano every). One slave (the one from the code example above) runs 6 tiny TowerPro SG90 servos. The other slave runs a little bit stronger servos 4 DS3225 and 1 MG996R, the behaviour there is observed only on one of the DS3225. I should admit that wiring is not super great and all wires are bundled together even on the plug ends.

They all go connected to 3 different breadboards - 1 for the powering (and the ESP32) and then for every slave (arduino nano every) separate breadboard only the signal wire.

What I believe (I might be wrong tho) is that I am getting some sort of artefact signal somewhere along the line because when I power up my project all the servos stay fixed which means they are powered. If as you suggest powering is the problem then my logic leads me to believe that for example when I send signal to servo X then servo Y loses power and servo Y because of the load goes to initial position, but then power is back on and servo Y is powered again so it goes to previous position (thus twitch).

I will try with another power source but I can not solder the powering wires yet, I am not that advanced in the project yet I need to build more before allowing myslef to solder any endpoints.

Case closed I suspect. Those servos pull ~650mA each at stall and you have six. Breadboards are for logic level stuff, not powering servos.

waaaaaaaaait wait wait... I am using a power source of 5V 20A but you say that breadboards can not operate on high current consumption in this case 4A (or at least they misbehave). Is that whats happening... I never would have suspected that...

I am gonna test this right now!

I rewired the servos power feed outside the breadboard but unfortunatelly twitching still happens, now another thing that comes to my mind in this situation would be the endpoint wires of my power source. In the end of my power source cable I soldered breadboard jumper cables (so I can plug them into the breadboard) and may be just may be those wires can't carry the current load. I'll check on that and report back.

I changed the mentioned wires too but there was no effect at all on the behaviour, its absolutely the same as it was when when powered by the breadboard. Not even tiny improvement...

I am getting nowhere debugging the power at this point, I changed the power supply with professional one I changed wires I even left only 2 servos just to observe if I control one servo does the other twitch - yes it does. Another thing I did was to leave again 2 servos but unplug the signal cable for one (to see if there is noise in the power) and that test was successfull, when I send signal to connected servo, the one without signal connection (yet powered) is not twitching.

At this point all I can think of is to try using servo driver PCA9685 and see if I get better results.

So far my conclusion is that my problem is definatelly not from my power source. It is still possible to be power connection problems related to share the same power as the nano but my suspicions are still around the signal cables and noises.

I forgot to mention but it might be important, my servos cables are extended with around 1m additional wires from their original servo cable lengths (I need them all to gather in non moving location).

Maybe you're getting some signal wire crosstalk.

do you have any suggestions on how to insulate them against crosstalk. I extended them with LAN wires (extracted single wires from the LAN cable and used those)?

Keep them physically apart. Grounded shielding perhaps too. I'm no expert though.

I will try using servo driver board first and if that fails I'll go for the wires. What you suggest to keep them physically apart is not affordable in this project, they should be bundled into wrapper which is calculated not to be affected by movements from the servos.

well after all, I ended up using Adafruit PCA9685 Servo Driver and all problems are now gone. I can't identify what was causing the problem with twitching in the first place but at least thats what solved it. If anyone in the future stumbles to the same problem - I recommend to use servo driver if you end up using multiple servos on single arduino board (my case is with arduino nano every).

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