Go Down

Topic: Servo gone crazy: moves without me sending command. (Read 2 times) previous topic - next topic



I have two of these little servos: http://www.dealextreme.com/p/sg90-mini-servo-with-gears-and-parts-2kg-torque-35764

1- I tested them with the code bellow (from playground). They were working fine connected to an arduino:
2- Now that I almost fish assembling my robot and have them connected to a serial servo controleur, I started programing.
3- All my servos work fine expect for these two. They move like crazy without me sending any command.
4- I thought it might be some kind of incompatibility with the servo controller. But when I plug them back to a stock arduino with same code as before they keep moving continuously in one direction or alternatively.

May I have damaged them somehow? Here is the model of the serial controller I used: http://www.pololu.com/catalog/product/207/resources

Any help would be welcome!

Code: [Select]
#include <Servo.h>
int minPulse1     =  0;   // minimum servo position
int maxPulse1     =  180; // maximum servo position
int turnRate1     =  10;  // servo turn rate increment (larger value, faster rate)
int minPulse2     =  0;  // minimum servo position
int maxPulse2     =  180; // maximum servo position
int turnRate2     =  10;  // servo turn rate increment (larger value, faster rate)
int buttonPin     = 13;    // pin that the trigger will be connected to
/** The Arduino will calculate these values for you **/
int centerServo1;
int centerServo2;
int pulseWidth1;     // servo pulse width
int pulseWidth2;     // servo pulse width

Servo servo1;
Servo servo2;

void setup() {
  pinMode(buttonPin, OUTPUT);
  centerServo1 = maxPulse1 - ((maxPulse1 - minPulse1)/2);
  centerServo2 = maxPulse2 - ((maxPulse2 - minPulse2)/2);
  pulseWidth1 =  centerServo1;
  pulseWidth2 =  centerServo2;
  Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
  Serial.println(" Arduino Serial Servo Control");
  Serial.println("Press a, s, d, or w to move, spacebar to center, and f to fire");

void loop() {

  // check for serial input
  if (Serial.available() > 0) {

    int data = Serial.read(); // read the incoming byte:
    digitalWrite(buttonPin, LOW);  // turn the pin off on any incoming data
case 'a' :  pulseWidth1 = pulseWidth1 - turnRate1;  break;
case 'd' :  pulseWidth1 = pulseWidth1 + turnRate1;  break ;
case ' ' :  pulseWidth1 = pulseWidth2 = centerServo1;  break;
case 's' :  pulseWidth2 = pulseWidth2 - turnRate1;  break;
case 'w' :  pulseWidth2 = pulseWidth2 + turnRate1;  break ;
case 'f' :  digitalWrite(buttonPin, HIGH); delay (1000); digitalWrite(buttonPin, LOW); break;

    // stop servo pulse at min and max
    if (pulseWidth1 > maxPulse1) { pulseWidth1 = maxPulse1; }
    if (pulseWidth1 < minPulse1) { pulseWidth1 = minPulse1; }

// stop servo pulse at min and max
    if (pulseWidth2 > maxPulse2) { pulseWidth2 = maxPulse2; }
    if (pulseWidth2 < minPulse2) { pulseWidth2 = minPulse2; }


    // print pulseWidth back to the Serial Monitor (uncomment to debug)
    Serial.print("Servo 1: ");
    Serial.print(" Servo 2: ");


Did you try connecting the 'bad' servos in place of two of the servos that 'work fine'?  If the 'bad' servos work fine when you put them in place of known working servos then either they were connected to bad channels on the controller or the software driving those channels is faulty.  If the servos misbehave with known good channels, the problem is the servos.
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e


Hello, Thanks for your answer,

Yes I did connect the 'bad' servos to the known to work channels: same issue, they move unexpectedly.
I don't know much about servos, but the only reasons I see is that there electronics went bad. Do you know any way to correct that ?



Now that I almost fish assembling my robot

I love the idea of fish assembling a robot, what sort of fish bass, bream or salmon?

The reason why a servo moves like this is that it's input line is floating. This can be due to not initialising the pins connecting the servo as an output. I see in your code you don't do this, so add those lines and try again.


Simple servo test code that can be used to test individual servos.

Code: [Select]

// zoomkat 10-14-11 serial servo test
// type servo position 0 to 180 in serial monitor
// 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() {
  myservo.writeMicroseconds(2000); //set initial servo position if desired
  myservo.attach(7);  //the pin for the servo control
  Serial.println("servo-test-22"); // 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);  // allow buffer to fill with next character

  if (readString.length() >0) {
    Serial.println(readString);  //so you can see the captured string
    myservo.writeMicroseconds(readString.toInt()); //convert readString to number for servo
    //myservo.write(readString.toInt()); //for degees 0-180
    readString=""; //empty for next input

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   8)

Go Up