SERVO - How to eliminate micromotion at startup?

Hello everyone, I have a question, I have a servo and it performs a micromovement at startup. Can this be eliminated somehow??

Here is my code and servo connection.

If you set the potentiometer on the second condition and press start/stop, you will also notice this servo movement in the animation :wink:
I tried to put a resistor on the signal line but unfortunately no change.

Thank you in advance for your help.

EDIT:
I'm simulating a PWM signal, that's why I'm using myservo.writeMicroseconds();

When you attach() a servo it moves to its default angle of 90 degrees. However, if you do a servo.write() before the attach() then it moves to that position when attached

I suspect what is happening is that the initial twitch is being caused by the move to 90 degrees being immediately interrupted by wherever your code tells the servo to move to

If so, then the solution may be to write() the required angle to the servo before the attach()

Did you try that in the 'real world' too?
What you see at the beginning is the movement from the initial position of wokwi ( when starting ) to the initial value of your sketch.
You can easily see that, if you insert a delay before your first movement/attach in setup:

   Serial.begin(115200);
   delay(1000);
   myservo.writeMicroseconds(1500);

Then you see it moving to the 'wokwi' initial position, and 1sec later to your initial position.
In the real world there is no such fixed initial position. The servo will move from where it is positioned when starting ( from the position you last set it to ) to the first initial position of your sketch.

wokwi doesn't always tell the real truth with external hardware :wink:

Real servos often perform a short random movement when switched on.

@MicroBahner I wrote that I have this micromovement physically at my place and behaves similarly on the Wokwi.
Servo writes in position 1500ms without potentiometer. And when I turn on the power, I have micromovement forward and backward a few degrees to the 1500ms position.
This is not a software problem, but as if the motor started and only then received the PWM signal. I tried various solutions and failed to eliminate this traffic on startup.

Did you try setting the servo initial position before using attach() ?

Yes, that's not unusual with real servos as i wrote too. But what you see in wokwi is something completely different ( even if it looks similarly ).
You can try to connect a resistor with about 4.7k between the controlling input and the power input of the servo. This helps in many cases. But there are servos where it is nearly impossible to suppress this micro-movement when powering up.

You can also try putting an NPN transistor between ground and the ground pin of the servo and connect that to one of your GPIO of your microconroller, making it a power switch for the servo. Don't pull that pin high until after you attach the servo and write a new position to it. Then the servo only receives power when it is trying to be moved to a new position. As soon as it powers up it is moving to this new position. You can then pull the power low before you call "detach" but that is likely to cause a small of amount of jitter as well. That's the solution I used to reduce the amount of jitter but you're never going to eliminate 100% of the jitter. It's just the nature of servos.

My experience with servos is limited, but I've found that a servo does a little jerk when first powered up no matter what is happening on the control line. It could just be the cheap-ass servos I've used, but it always happens. I can tie the control line high or low, or whatever, and I still get that jerk.

I think the issue is if the servo's power source and the Arduino Uno's power are enabled at the same time, then there'll be about 65ms (boot-up time for UNO) where the servo won't be getting a valid pwm signal. This represents 3 pwm periods where the servo will react to the invalid state of the pwm signal.

If the servo's power-up is enabled after boot-up of the Arduino, then I think the start-up jitter will be eliminated.

One possible remedy might be to use a separate buck-boost converter with enable to power the servo, where the UNO controls both the enable for servo power and (of course) the servo's pwm signal.

Make sure that the dc-dc converter is sized appropriately for the servo you're using.