out-of-control servos, servo parameters

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.

This fixed my spinning SG90 servo problem with my ELEGOO UNO R3 board. Thanks for the solution.

My servo was spinning 360 deg when I delayed between position settings. I tried an external power supply (which didn't help, of course) which is what most people say to do. I was following the servo example and using your code above allows it to run using my board power supply.

I'm trying to be detailed in case others are having the same issue.

If the servos were manufactured correctly they would be physically incapable of moving beyond the range 0 - 180 degrees approx.

...R

Micro Servo S99 SG90 servo did go 360 degrees.

Customer support says it is 360 deg servo.

Because our servo is a 360-degree servo, it is normal. In 360-degree servo, PWM controls its rotation speed and rotation direction. PWM of 500-1500us controls it to rotate forward. The smaller the value, the greater the rotation speed ; PWM of 1500-2500us controlls it to rotate reverse. The greater the value, the greater the rotation speed. PWM of 1500us controlls it to stop. In use, the program must be set to 10-170 degrees to make it work properly.

They were less help than graybeard, as their solution was to only narrow the sample range to 10 <= pos <= 170 degrees, and still spins when I delay between settings, or just when I had something simple, like:

#include <Servo.h>

Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards

int pos = 0;    // variable to store the servo position

void setup() {
 myservo.attach(9);  // attaches the servo on pin 9 to the servo object
}

void loop() {
 pos = 10;
 myservo.write(pos);              // tell servo to go to position in variable 'pos'
 delay(5000);                     // waits 5000ms for the servo to reach the position
}

greybeard still has the best answer for my situation.

The only thing wrong with that customer service answer is that a genuine TowerPro SG90 micro servo is not a 360 degree servo. I've used loads of them and they move approximately 180 degrees total and you can pause them at any position as long as you like and they stay there (that's what servos do).

So wherever you got your servos from is probably selling some even cheaper knock-offs and calling them SG90s.

Steve

Jeffe:
[

Because our servo is a 360-degree servo, it is normal. In 360-degree servo, PWM controls its rotation speed and rotation direction. PWM of 500-1500us controls it to rotate forward.

That is a description of a continuous rotation servo. With those, as the quote says, it is only possible to control the speed amd direction. It is not possible to control the position directly - for example by telling the servo to move to 123 degrees.

In effect a continuous rotation servo is a geared DC motor with the motor driver included. It is not really a servo.

...R

Robin2:
That is a description of a continuous rotation servo.

Cool!

Now that I know how to control a normal servo, I can now play with a continuous rotation servo. It's Christmas again!