2

Recently, I have been working on a project involving 8 Tower Pro sg90 servos and an Arduino Uno. The issue I am running into is that the servos will randomly move when connected, without even receiving a signal from the Arduino.

Specifically, sometimes the servos will work perfectly and the input move the servos fine, but then all of the sudden the servos will start twitching and moving erratically back and forth. Then I disconnect power and plug it back in and I will sometimes work again or sometimes not.

I don't think power is the problem because I am powering the servos externally with a 5.5v power supply which can provide 2.5 amps. I also made sure to have a common ground between the Arduino and the power supply. Additionally I don't think this is an issue of a broken servo because I have this problem with all eight. In my code I have input pins to control the servos which are just buttons. These buttons also have pull down resistors, so I don't think the input is the problem either. I am using the Arduino servo library.

The only other thing I can think of is external frequency interference, because some of the circuitry and wiring is close together, but none of the wires are very long and I even tried putting a ferrite bead on the servo cables but to no avail.

If anyone has any ideas about a solution to my problem which I have not tried yet that would be amazing!
Thanks - Xander

#include <Servo.h>

Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;
Servo servo5;
Servo servo6;
Servo servo7;
Servo servo8;

void setup(){
  pinMode(5, INPUT);
  pinMode(4, INPUT);
  pinMode(3, INPUT);
  pinMode(2, INPUT);
  pinMode(1, INPUT);
  pinMode(A0, INPUT);
  pinMode(A1, INPUT);
  pinMode(A2, INPUT);
  servo1.attach(13);
  servo2.attach(12);
  servo3.attach(11);
  servo4.attach(10);
  servo5.attach(9);
  servo6.attach(8);
  servo7.attach(7);
  servo8.attach(6);
}

void loop(){
  if(digitalRead(5)==LOW){
    servo1.write(120);
    delay(20);
  }
  else{
    servo1.write(50);
    delay(20);
  }
  if(digitalRead(4)==LOW){
    servo2.write(110);
    delay(20);
  }
  else{
    servo2.write(200);
    delay(20);
  }
  if(digitalRead(3)==LOW){
    servo3.write(120);
    delay(20);
  }
  else{
    servo3.write(50);
    delay(20);
  }
  if(digitalRead(2)==LOW){
    servo4.write(110);
    delay(20);
  }
  else{
    servo4.write(200);
    delay(20);
  }
  if(digitalRead(1)==LOW){
    servo5.write(120);
    delay(20);
  }
  else{
    servo5.write(50);
    delay(20);
  }
  if(digitalRead(A3)==LOW){
    servo6.write(110);
    delay(20);
  }
  else{
    servo6.write(200);
    delay(20);
  }
  if(digitalRead(A2)==LOW){
    servo7.write(120);
    delay(20);
  }
  else{
    servo7.write(50);
    delay(20);
  }
  if(digitalRead(A1)==LOW){
    servo8.write(110);
    delay(20);
  }
  else{
    servo8.write(200);
    delay(20);
  }
}
4
  • Are the servos getting heated up? Apr 20 at 3:41
  • No not at all. They aren't under really any load either.
    – Xander
    Apr 20 at 3:58
  • Please show your code. You can edit the question, include it there and format it correctly.
    – chrisl
    Apr 20 at 10:53
  • As JRobert says you'll want to (accurately) debounce and maintain current state. I would also ensure that the delay is long enough for the servo to reach its end position. I would also drop the number of servos in half and see if the problem persists, but in theory eight servos should be fine on the Uno. You could also try a non-blocking delay, but I don't think that should matter. Cross-talk isn't hugely likely. I think wrapping up each button/servo combo would be a good way to get started. May 23 at 21:19
0

You call servoX.write() repeatedly, even when no button has changed. Each call turns off interrupts briefly while the servo value is being updated. This happens, maybe 40 times/sec and may be messing up the servo timing in some non-constant way. Try only calling the servoX.write() functions once, only when its button-state has settled into a new state.

Update:

could you provide an example please

  bool prevButton5 = LOW;            // one of these for each button
    :                                // outside of the loop() func.
    :

  if(digitalRead(5) != prevButton5){  // button changed?
    prevButton5 = !prevButton5;       // save new button state
    if(preButton5 == LOW){            // (your same code from here down)
      servo1.write(120);
      delay(20);
    }
    else{
      servo1.write(50);
      delay(20);
    }
  }
  • Make a boolean for each button.
  • Wrap each of your button segments as I did for button5. Notice that your code is now executed only if the associated button has changed, so you will have many fewer servoX.write() calls.

That isn't the totally best way to do it. I made it as straightforward as I could, even though the additional code gets replicated for every button. I'd probably write a single function that manages one button and one motor, and call that function for each button / motor pair.

I also ignored the concept of switch-bounce to keep the number of changes low, but you should look it up and consider it. Switch-bounce is apparent bouncing of the switch contacts for a few milli-seconds during the change from open to closed or vice-versa, which leads to several make-break-make.....-make cycles (or the reverse) when you push or release the button. I'd probably make (or search for) a button debounce function and call it for the particular button I'm testing if I see that button has changed.

First try the "rude and crude" fix I suggested and get that working. Then if you're interested, try making one or more of the refinements.

Update:

the servos still jitter and twitch esspecailly when
I move the whole arduino and wires around.

That suggests either or both of

  • cross-talk;
  • the plug wires' connections being less than solid, especially when the wires move.

Try:

  • laying out the servo cables to keep them away from each other as much as possible;
  • wiring only a few of the servos, and in different combinations;
  • also try commenting out the code - or not - for those servos you disconnected in the previous step.

If it turns out to be the plug-in wires and if this project is intended to actually be used for something, I'd suggest using a screw-terminal shield on your Uno to attach the servo wires more securely.

4
  • 1
    This sounds promising but Im a little confused could you provide an example please. Thank you
    – Xander
    Apr 20 at 19:06
  • Well I tried the code and it works but after a certian amount of time the servo starts jittering again. I would say that the servo jitters by less magnitude but it still jitteres and twitches. I also noticed that if I touch the 5v pin of the Arduino that the "TX" light on the Arduino turns on and the servo moves a large distance clockwise. I think the code helped but it didnt fix it yet. Any other thoughts, Thank you
    – Xander
    Apr 21 at 3:53
  • I just disconnect the input pins from the buttons and the servos still jitter and twitch esspecailly when I move the whole arduino and wires around. This tells me that the problem is not in my circuitry but probably something to do with the arduino itself. Do you think it is broken or something else?
    – Xander
    Apr 21 at 4:18
  • @Xander If it jitters and twitches when you "move the arduino and wires around" it sounds like an issue with the wiring. Jun 22 at 21:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.