Servo moves erratically when told to go to a certain position?

Hi i’ve been having some troubles with a MG996R servo.

I have the servo connected up to Pin 11 of my arduino uno,
The servo has a rating of 4.8~7.2 V and has a stall current of 2.5A
The datasheet said that it had a duty cycle of 1~2ms and a pwm period of 20ms.
So i put a seperate power supply (4AA batteries) for the servo and used a separate power supply for the arduino uno as well. (tried both Computer usb and 9v power supply cord).

I used the following code


#include <Servo.h>
Servo joint1;
void setup() {
joint1.attach(11);
}
void loop() {
joint1.write(90);
}


what this did was just move the servo arm about 100 deg clockwise and anticlockwise repeatedly.

i though it may be that the servo’s pwm period wasnt actually 20ms so ive tried the following code to output my own PWM


int servoPin = 11;
void setup() {
pinMode(servoPin, OUTPUT);
}

void loop() {
digitalWrite(servoPin, HIGH);
delayMicroseconds(1500);
digitalWrite(servoPin, LOW);
delayMicroseconds(18500);
}


i’ve tried every LOW output width from 15000 to 18500 of 100 intervals and noticed that a LOW output of about 16480 microseconds seemed to work somewhat best but when it goes to the desired position it jitters violently.

the most success ive had so far was to send 2 PWM waves every 0.5 seconds 20 times to move to one position. So ive made a code below


#define servoPin 11
int t=0;
int x=0;
void setup() {
pinMode(servoPin, OUTPUT);

}

void loop() {
for (x=0; x<=20; x+=1){
for (t=0; t<=2; t+=1){
digitalWrite(servoPin, HIGH);
delayMicroseconds(610);
digitalWrite(servoPin, LOW);
delayMicroseconds(15870);
}

delay(500);
}
x=0;
for (x=0; x<=20; x+=1){
for (t=0; t<=2; t+=1){
digitalWrite(servoPin, HIGH);
delayMicroseconds(1480);
digitalWrite(servoPin, LOW);
delayMicroseconds(15000);
}

delay(500);
}

x=0;
for (x=0; x<=20; x+=1){
for (t=0; t<=2; t+=1){
digitalWrite(servoPin, HIGH);
delayMicroseconds(2380);
digitalWrite(servoPin, LOW);
delayMicroseconds(14100);
}

delay(500);
}
x=0;
for (x=0; x<=20; x+=1){
for (t=0; t<=2; t+=1){
digitalWrite(servoPin, HIGH);
delayMicroseconds(1480);
digitalWrite(servoPin, LOW);
delayMicroseconds(14130);
}

delay(500);
}

x=0;
}


which moves the servo from 0deg to 90deg to 180deg to 90deg and loops over. however, it still showed some erratic movements. Also my servo is now locked up. and i kind of figured this wouldnt be an ideal way to control a servo. First i thought it was a servo problem so ive tried with 4 different servos all of them showing the same results. (3 of them are toast now…). Is this a problem with my code? or would it be that the servos from this company are just crap servos? would there be a way to fix this? Thank you guys so much for your input. Ive tried googling different solutions but most servo tutorials just use the servo.h library which couldn’t help solve my problem.

Why are you not using the Arduino Servo library?

Have you the GND for the servo power supply connected to the Arduino GND - they should be connected.

...R

PS ... When posting code please use the code button </>
codeButton.png

so your code 
looks like this

and is easy to copy to a text editor See How to use the Forum

The most likely reason why your first code (using Servo.h that works for thousands of us) is doing what you describe is that you are writing to the servo so frequently (every time round loop). Try putting a delay(1000) after the write() to give the servo time to move. Or just put the write() in setup() so it only writes once if you only want one move.

The other possibility is that the power supply is inadequate. Bad batteries, bad connections (e.g. if you are connecting the servo via a breadboard) or bad grounding are all possible.

Steve