Servo twitch on startup

I'm making a remote start for my generator and I have a servo to operate the choke. When I power my system, both the Arduino and the servo get power at the same time. My servo gives a quick little twitch and rotates about 30 degrees. This isn't desirable. Everything runs off the 12V battery and the servo gets its power from a 5V BEC. The servo is too big to run off the Arduino 5V supply.

I played around a little and I can get rid of the twitch by delaying the power to the servo. My current solution is to manually connect the BEC after the Arduino is alive and sending a control signal to the servo. This works, but obviously isn't a good long term solution.

Anyone else experienced this? Any good solutions?

My current thought is to use a MOSFET to control the BEC. I would just turn on the BEC around 200 milliseconds after the Arduino control signal is sent. Is there some product out there that outputs 5V and can be easily turned on/off through a built in Arduino input (combined BEC and MOSFET/relay)? I'm trying to reduce assembly time as I have to build a bunch of these.

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

The most common thing with servos is that they twitch when power is applied. Some are
much worse than others. The GWS servos I have are especially bad.

That's if you aren't even sending pulses to the servo. Then, you have the problem that the
servo will almost never be in the exact position corresponding to the first pulse it gets, and
it will go to the commanded position at maximum speed. So, 2 uncontrollable movements,
(a) one at power up, and (b) the other with the first command pulse.

For (a), try different servos. For (b), you might use some sort of external position feedback,
such as another pot, or alternately, take advantage of the fact that servos have poor torque
when the pulses aren't coming in at a full 50-Hz rate. IOW, send the first few pulses at a slow
update rate so the servo takes a while to get over to its commanded position.

Any good solutions?

Posting your code might help.

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.