I have several 180-degree servos from Elegoo arduino kits, and they all go out of control when commanded with
myservo.write(0);
They spin continuously until sent a new command. The problem does not occur with sending it to 30, 90, 180 degrees, etc.
The problem turns out to be that the default parameters in the servo package are not appropriate for these servos. I fixed the problem by attaching them with
myservo.attach(3, 800, 2500);
instead of the default of
myservo.attach(3);
The last 2 parameters set the width of the control pulses in microseconds. The default values, as given in the Servo package documentation, are 544 and 2400.
Generic servo documentation says the pulse widths should be between 1000 and 2000 microseconds, with a 20 msec period. using 1000 & 2000 in the attach call works, but results in less than 180 degrees rotation. Adjusting the minimum and maximum pulse widths lets you control both the range of motion and location of the endpoints in the standard servo.write(...) call.
The exact parameters will probably vary somewhat from manufacturer to manufacturer, and possibly even within individual devices, since the control hardware is generally analog circuitry. The specific devices I have are all labeled "“Tower Pro Micro-Serv 99 SG99”. Presumably using 544 for the zero-point rotates the motor past the point at which the internal comparison voltage wraps around, causing the device to think it at 180 degrees instead of 0, and starting a new rotation. Using 800 and 2500 keeps the device in range.
Interestingly, the "sweep.ino" tutorial works with these devices, but only because immediately after slewing the motor down to 0 degrees, it immediately starts sweeping in the positive direction. Adding a delay when the device reaches 0 sends the motors spinning out of control. The sweep also rotates a bit more than 180 degrees with the default parameters.