Servo Motor Does not spin the full 180 degrees

I’m trying to program the servo to rotate 180 degrees. It only seems to be turning about 20 degrees or so and I can’t really tell if its actually doing anything. I thought it was my code so I tried using the given sweep sketch to test if the motor actually worked. This code does not spin it correctly either. Any help would be appreciated. I want it to spin 180 degrees and then hold that position for 5 seconds and then go back to its original state. The end goal is to use 2 motors at once.

/* Sweep
by BARRAGAN http://barraganstudio.com
This example code is in the public domain.

modified 8 Nov 2013
by Scott Fitzgerald

*/

#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(5); // attaches the servo on pin 9 to the servo object
}

void loop() {
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree

myservo.write(pos); // tell servo to go to position in variable ‘pos’
delay(1); // waits 15ms for the servo to reach the position
}
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable ‘pos’
delay(15); // waits 15ms for the servo to reach the position
}
}

Not all servos (I'd say very few) turn a full 180 degrees.

If you provide a link to the servos you're using, someone might be able to provide additional information about them.

HobbyKing's HXT900 servos are one of the few servos I've found which turn a full 180 degrees.

I've said many times I dislike the use of degrees as a servo position. Servos rarely have exactly a 180 degree range of motion. The degree figures are very rarely accurate. You get much better control of the servo (almost a factor of 10) if you use microseconds.

Try and make your own code using Jeremy Blum’s Tutorial here: Tutorial 05 for Arduino: Motors and Transistors - YouTube</titl Here’s my code you can try:

Srvo.ino (354 Bytes)

Servo test code you might use to carefully determine the internal mechanical limits of your servo. Note that most servo issues are caused by trying to power the servo from the arduino, which often causes the arduino to crash and reset.

// zoomkat 3-28-14 serial servo incremental test code
// using serial monitor type a character (s to increase or a 
// to decrease) and enter to change servo position 
// (two hands required, one for letter entry and one for enter key)
// use strings like 90x or 1500x for new servo position 
// for IDE 1.0.5 and later
// Powering a servo from the arduino usually *DOES NOT WORK*.

#include<Servo.h>
String readString;
Servo myservo;
int pos=1500; //~neutral value for continuous rotation servo
//int pos=90;

void setup()
{
  myservo.attach(7, 400, 2600); //servo control pin, and range if desired
  Serial.begin(9600);
  Serial.println("serial servo incremental test code");
  Serial.println("type a character (s to increase or a to decrease)");
  Serial.println("and enter to change servo position");
  Serial.println("use strings like 90x or 1500x for new servo position");
  Serial.println();
}

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) {
    if(readString.indexOf('x') >0) { 
      pos = readString.toInt();
    }

    if(readString =="a"){
      (pos=pos-1); //use larger numbers for larger increments
      if(pos<0) (pos=0); //prevent negative number
    }
    if (readString =="s"){
      (pos=pos+1);
    }

    if(pos >= 400) //determine servo write method
    {
      Serial.println(pos);
      myservo.writeMicroseconds(pos);
    }
    else
    {   
      Serial.println(pos);
      myservo.write(pos); 
    }
  }
  readString=""; //empty for next input
}

First ensure your hardware setup is correct - separate 6V or so supply capable of at least 1A is necessary for one servo - scale up the current for several servos. Never use 5V USB to power a motor or servo.