Servo twitch on startup

Thanks for the suggestions. I had to put this away for a while, but I'm back for the weekend.

The problem is in the setup, not in the loop. The twitch is at the myservo.attatch() and myservo.write() commands. As AWOL suggested, I tried swapping those commands and also adding a delay(1000) in between the commands. None of these ideas worked. What's shown below is the last code I tried.

#include <Servo.h> 
Servo myservo; 

int servo = 9;     // pin output to control the servo
int start = 50;    // start position of servo
int finish = 130;  // finish position of servo

void setup() {  
  myservo.write(start);   // move servo to start position
  myservo.attach(servo);  // attaches the servo to pin 9
  *** non-servo related code removed ***
}

void loop() { 
  *** all this code works fine ***
}

oric_dan - (a) I've tried 3 servos, and they all do this. I'm buying cheap servos, so maybe that's the problem. (b) In my system, the servo will always end in the start position for the next operation. I don't have to worry about this part of the initial servo jump.

EDIT - corrected comments in code to pin 9

Simple servo test code. I noticed in your code that a comment said pin 8 was being used, but the code used pin 9. Also, if the 5V BEC can be set to 6v, the servo will perform better. Does the BEC function the same as an UBEC that is used to power servos (In usually see the BEC associated with brushless motors)?

// zoomkat 10-22-11 serial servo test
// type servo position 0 to 180 in serial monitor
// or for writeMicroseconds, use a value like 1500
// for IDE 0022 and later
// Powering a servo from the arduino usually *DOES NOT WORK*.

String readString;
#include <Servo.h> 
Servo myservo;  // create servo object to control a servo 

void setup() {
  Serial.begin(9600);
  myservo.writeMicroseconds(1500); //set initial servo position if desired
  myservo.attach(7);  //the pin for the servo control 
  Serial.println("servo-test-22-dual-input"); // so I can keep track of what is loaded
}

void loop() {
  while (Serial.available()) {
    char c = Serial.read();  //gets one byte from serial buffer
    readString += c; //makes the string readString
    delay(2);  //slow looping to allow buffer to fill with next character
  }

  if (readString.length() >0) {
    Serial.println(readString);  //so you can see the captured string 
    int n = readString.toInt();  //convert readString into a number

    // auto select appropriate value, copied from someone elses code.
    if(n >= 500)
    {
      Serial.print("writing Microseconds: ");
      Serial.println(n);
      myservo.writeMicroseconds(n);
    }
    else
    {   
      Serial.print("writing Angle: ");
      Serial.println(n);
      myservo.write(n);
    }

    readString=""; //empty for next input
  } 
}

AWOL:
Have you tried to write the servo position before the attach?

My (probably wrong) assumption was that you could only do a write to an attached servo.... What's the thinking behind this please?

What's the thinking behind this please?

A write sets the timing for the pulse, but if there's no pin attached to the Servo object, it effectively changes the default position for the Servo.
Say, for example, your application always parks the servo at zero degrees.
At startup, the default behaviour of the Servo object would be to move the servo to 90°, and you would do an immediate write to set it to zero. It may well twitch.
Do the write before the attach, no twitch.
Or, you could just put the horns on with a ninety degree displacement.

Ah ok, thanks.

That makes sense, especially in the light of this part....

the default behaviour of the Servo object would be to move the servo to 90°

... which I didn't know in the first place.

zoomkat - Yes, its acutally a UBEC. It's an old habit from the r/c car days to call them BEC's. I switched the UBEC output from 5V to 6V and I still get the twitch.

AWOL - I changed my starting position from 50deg to 90deg. I still get the twitch.

Let me give everyone more detail. When the myservo.attach(servo) line is executed, the servo rotates 30deg clockwise. It doesn't matter if I write a myservo.write(start) before this line or not. The attach line always causes the 30deg cw rotation. I've tried manually moving the servo horn to some random position before energizing the system. Even then, the servo rotates 30deg cw before going to its start position. I'm stumped.

Have you double checked your servo wiring? Make sure you have a good ground connection between the servo power ground and the arduino ground.

Good idea. I just checked all the wiring and the grounds look good. When I was going over my breadboard, I realized that I was assuming the two grounds on the UBEC were grounded to each other. I just verified this with the multimeter, but I added a ground circuit across the UBEC grounds just to be safe. Still twitching...

I've done some tinkering with servos controlled by the arduino and never had this issue.

zoomkat:
I've done some tinkering with servos controlled by the arduino and never had this issue.

As noted, some servos, such as GWS, twitch much more than others on power application.
Other than finding servos that don't twitch, it may work to use a Mosfet to apply power to the
servo [p-chan Mosfet on high-side power], and start the incoming pulses before power is applied.
Maybe.

oric_dan:
As noted, some servos, such as GWS, twitch much more than others on power application.
Other than finding servos that don't twitch, it may work to use a Mosfet to apply power to the
servo [p-chan Mosfet on high-side power], and start the incoming pulses before power is applied.
Maybe.

This works with the servos I have. After I power on the Arduino, I manually connect the servo to power and there's no twitch. So I know the P-chan Mosfet circuit you describe will work.

I swapped my Uno boards and the twitch is still present. When swapping servos on the new Uno, one thing that was interesting is that different brand servos twitch in different directions (ccw vs cw). I'm ordering some other servos today. If that doesn't do it, I'll do the Mosfet design.

Maybe zoomkat will tell you which servos he has that don't twitch on power application.

oric_dan:
Maybe zoomkat will tell you which servos he has that don't twitch on power application.

I just use cheap analog servos powered by a 7805 chip with diode to output 5.7v. If the servos in this discussion are digital servos, then that might be where the issue lies. Some of the discription is strange in that no matter where the servo is physically positioned, it always moves 30 deg. This is strange in that the servo should move to some default control signal position when attached.

Some of the discription is strange in that no matter where the servo is physically positioned, it always moves 30 deg. This is strange in that the servo should move to some default control signal position when attached.

These are 2 different issues, as I noted in post #2. Re the 2nd half, whenever you start sending pulses to a
servo it will normally jump at maximum speed, because it's unlikely to be at the commanded target position
[except as OP noted, for his particular situation].

Re the 1st half, I have some GWS servos that jump literally 30-deg when power is applied, and NO pulses are
being sent. If I re-apply power several times, I can get the servo to jump a full 180 deg around the clock,
until it gets over to its mechanical stops.

Re the 1st half, I have some GWS servos that jump literally 30-deg when power is applied, and NO pulses are
being sent. If I re-apply power several times, I can get the servo to jump a full 180 deg around the clock,
until it gets over to its mechanical stops.

Are the gws servos analog or digital? The "and NO pulses are being sent." sounds like a possible floating control pin situation. Corrupt or inappropriate control signals can cause analog servos to erratically turn until they hit the hard stop.

edit: When you code "detach" your servo, does it behave strangely? If you physically remove the control wire, does the servo behave strangely?

Both of the servos I have (2 diff brands) do something similar. They (or at least one) actually give 2 little twitches before a bigger twitch, then go to either 900 or wherever I have them positioned in setup, but sometimes it will be just three little twitches before default. The only thing that seems to make a difference (maybe) is how much torque is on them at power on. They also twitch every time I turn on the serial monitor.

GWSes are analog. But good thinking. I just tried 2 GWS servos, and it doesn't matter if
the signal pin is grounded or not. One of the GWSes simply twitches noticeably on power
application, and then comes back to the same position. The other one turns 20-30 degrees
or so, even with a moderate torque load on it.

I got a new servo yesterday, and it does the same thing. It does twitch in the opposite direction though.

I'm adding a relay to delay the power to the servo.

When you start up an Arduino, the servo will automatically jump the servo a bit and move. One way to prevent this could be to always have a voltage applied, but maybe like a "sleep" mode where no power is being drawn other than the LED on the main board. Does that help at all?