Servo Motor Twitching

I’m new to this, I’ve searched (maybe I’ve missed it though), but I recently got a mini analog servo (powered by 4.6 - 6.0v) and I hooked it up to the 5v power on the arduino, and A0 (I’m using an arduino uno v3). Now, I have it working with the servo library. However, when I tell it to turn more than ~159 degrees, when it stops, it still tries to keep turning (and makes a ticking noise like it’s still trying to turn even if it’s at 180). How do I assure that this doesn’t happen? Thanks in advance!

#include <Servo.h>

Servo servo1; Servo servo2; 

void setup() {
  servo1.attach(14);  
  servo2.attach(15);

  Serial.begin(19200);
  Serial.println("Ready");
}

void loop() {

  static int v = 0;

  if ( Serial.available()) {
    char ch = Serial.read();

    switch(ch) {
      case '0'...'9':
        v = v * 10 + ch - '0';
        break;
      case 's':
        servo1.write(v);
        v = 0;
        break;
      case 'w':
        servo2.write(v);
        v = 0;
        break;
      case 'd':
        servo2.detach();
        break;
      case 'a':
        servo2.attach(10);
        break;
    }
  }
}

Servo test code.

// zoomkat 10-22-11 serial servo test
// type servo position 0 to 180 in serial monitor
// or for writeMicroseconds, use a value like 1500
// for IDE 0022 and later
// Powering a servo from the arduino usually *DOES NOT WORK*.

String readString;
#include <Servo.h> 
Servo myservo;  // create servo object to control a servo 

void setup() {
  Serial.begin(9600);
  myservo.writeMicroseconds(1500); //set initial servo position if desired
  myservo.attach(7, 500, 2500);  //the pin for the servo control, and range if desired
  Serial.println("servo-test-22-dual-input"); // so I can keep track of what is loaded
}

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) {
    Serial.println(readString);  //so you can see the captured string 
    int n = readString.toInt();  //convert readString into a number

    // auto select appropriate value, copied from someone elses code.
    if(n >= 500)
    {
      Serial.print("writing Microseconds: ");
      Serial.println(n);
      myservo.writeMicroseconds(n);
    }
    else
    {   
      Serial.print("writing Angle: ");
      Serial.println(n);
      myservo.write(n);
    }

    readString=""; //empty for next input
  } 
}

Thanks for the code. I wasn't necessarily looking for code, as much as I was looking for an explanation of why what I'm doing isn't right, as well as some info it looks like I may be missing on how servos work or how to control them.

Based on my experience there is some variability between servos. I suspect it is juddering because it is trying to turn farther than the internal physical stop permits. I would experiment to find the minimum and maximum positions it can move to without judder.

If necessary you may be able to open up the servo and carve a bit of plastic off the end stops to allow a little more movement. But don't blame me if you break it!

...R

Thanks for the advice! This is what I've done basically, and I've noticed that I can't really turn it past ~155 degree without the buzzing. BUT, even when turning that far, sometimes it will slow down at the 155 mark (where I told it to stop) and just slowly keep buzzing till it reaches close to 180.. Not sure if this is something with the servo library or the example code above or what.

I'm not sure if you mean 155 degrees as measured by the physical angle the servo arm turns or if you mean the "instruction" that you put in the Arduino code - eg. Servo.write(155);

If the servo won't smoothly run from limit to limit there must be something wrong with it. You should be able to establish the limits by turning the arm (slowly) manually when it is not connected to the power.

...R

Two things to know about servos and arduino:

  • Many servos can’t turn through 180 degrees - you will have to find out by experiment how far yours can go and make sure your code doesn’t ask it to exceed its limits.
  • Powering a servo from the arduino is marginal at best - it needs its own power supply with the ground connected to the arduino.

So, I can turn the arm manually without messing up anything internally while there’s no power hooked up to it?

amb0027: So, I can turn the arm manually without messing up anything internally while there's no power hooked up to it?

Maybe, or maybe not. The small servos require much more care when turning by hand to prevent stripping the gears. I posted servo code you can use to find the turn limits of a servo without turning by hand.

zoomkat:

amb0027: So, I can turn the arm manually without messing up anything internally while there's no power hooked up to it?

Maybe, or maybe not. The small servos require much more care when turning by hand to prevent stripping the gears. I posted servo code you can use to find the turn limits of a servo without turning by hand.

I couldn't really see the difference in what your code did and what my original code did. I had the same results

Rather than saying you "had the same results" it would be more practical to tell us what tests you have carried out and what the results were.

...R

Robin2: Rather than saying you "had the same results" it would be more practical to tell us what tests you have carried out and what the results were.

...R

What I've tried and the results I've obtained are in my original post.

I couldn't really see the difference in what your code did and what my original code did. I had the same results

Bummer, your servo may be damaged.

There have been several suggestions from other contributors. have you not bothered to try any of them?

...R

Robin2: There have been several suggestions from other contributors. have you not bothered to try any of them?

...R

I've tried everything suggested besides an alternate power supply (As I do not have one), however, I seriously doubt the power is the problem, as it works fine from 0 - 155 but will mess up at any angle past 156. I'm not sure what you're suggesting.

For example you never responded to Post #5 and your latest post repeats the confusing information that gave rise to Post #5.

...R

If you go out of range servos will start doing weird things, try using the servo.h library with values in microseconds between 1000 us to 2000 us. 1500 us should be center for most servos. Also it is best to power the servos with a separate 4.8 vdc to 6.0 vdc power supply and not the 5 vdc from the Arduino board. A lot of servos can draw over 1 amp when a load is applied which is way too much for the regulator on the Arduino boards. Make sure your grounds from the Arduino and the separate power supply are common (tied together) so you get a good signal source from the Arduino to the servos logic. Also if your servo is overloaded mechanically it will also twitch..

F.Y.I the servo.h library see's values over 200 as microseconds and not degrees of servo rotation. So you can always play with the values in micro seconds and see what happens for you particular servo..

I've tried everything suggested besides an alternate power supply (As I do not have one), however, I seriously doubt the power is the problem, as it works fine from 0 - 155 but will mess up at any angle past 156. I'm not sure what you're suggesting.

The are NO STANDARD VALUES. Typical servos have a range to mechanical stops of 100-200 degrees. Every servo is different. In addition the relationship between the "angle" specified by the Arduino library and the actual servo angle is very approximate.

As an example, I just grabbed a servo at random from my parts box - Turnigy 1160A. Its true mechanical range is 165 deg as measured with a protractor. If I sweep from 0 to 180 with the Arduino library, both stops are hit. I can tell from a slight vibration and hum at the stop. The effect disappears if I sweep from 15 to 170. 170-15=155 degrees. The actual movement is 165 degrees.

The behavior you see sounds perfectly normal to me. You need to calibrate for your application. These hobby servos are NOT precision instruments.

Joe

If it is a question of the rotation hard stops, remove the top cover of the servo and observe the rotation range between the hard stops.