Go Down

Topic: Newbe Need help with a simple Servo Problem (Read 991 times) previous topic - next topic

bslook

I wired up my servo and ran the following sample sketch and all I get is a contunous jerky rotation of the server a couple of degrees at a time.   Tried a second server with the same results.  Verified the wiring several times.  This has to be something simple.  It also messes up my serial port connection each time.

Arduino Servo Test sketch
*/
#include <Servo.h>
Servo servoMain; // Define our Servo

void setup()
{
   servoMain.attach(10); // servo on digital pin 10
}

void loop()
{
   servoMain.write(45);  // Turn Servo Left to 45 degrees
   delay(1000);          // Wait 1 second
   servoMain.write(0);   // Turn Servo Left to 0 degrees
   delay(1000);          // Wait 1 second
   servoMain.write(90);  // Turn Servo back to center position (90 degrees)
   delay(1000);          // Wait 1 second
   servoMain.write(135); // Turn Servo Right to 135 degrees
   delay(1000);          // Wait 1 second
   servoMain.write(180); // Turn Servo Right to 180 degrees
   delay(1000);          // Wait 1 second
   servoMain.write(90);  // Turn Servo back to center position (90 degrees)
   delay(1000);          // Wait 1 second   
   }

zoomkat

Quote
This has to be something simple.


Servos generally are not successfully used when powered from the arduino 5v. Could this be your problem?
Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   8)

retrolefty

Yes, a very common problems beginners have with using servos is assuming you can power them directly from a arduino board without problems. Servos should have their own independent voltage source and allow the arduino to just have to control the servos rather then try and control and power them.

Lefty

JimboZA

#3
Feb 17, 2013, 04:25 pm Last Edit: Feb 17, 2013, 04:27 pm by JimboZA Reason: 1
Just in case you're wondering how to do that, have a look at the attached Fritzing pic, bslook. The secret ingredient is to tie the grounds together.
Roy from ITCrowd: Have you tried turning it off an on again?
I'm on LinkedIn: http://www.linkedin.com/in/jimbrownza

hsteve

Well, this is interesting, because I'm a newby too, and I've just bought the Arduino experimenters kit, and using the servo supplied with the kit I've wired up the power from the Arduino board and I'm getting very odd behaviour.

Sometimes it works, other times it doesn't and sometimes the servo seems to spin internally, without the arm actually moving.

The code is simple (I hope), it's just intended to rotate the servo 5 degrees every 5 seconds. Have I done anything wrong or is it all down to the power supply ?

Code: [Select]

//////////////////////////////////////////////////////////////////////
// Turns a servo motor dependant on an analog value
// A value of 0-255 gets written to the output pin
// This represents a duty cycle of 0%-100%
// This is translated as a rotation of 0-180 degress of the servo
//////////////////////////////////////////////////////////////////////

int   outputPin = 11;
float fanalogValue ;
int   analogValue ;
int   degreeRotation=0 ; // the nomber of degrees we want the servo to rotate


void setup()
  {
    pinMode(outputPin,OUTPUT);
    Serial.begin(38400);
   
  }
 
void loop()
  {
     
     
      if (degreeRotation <= 180 && degreeRotation >= 0) // can't rotate more than 180 or less than 0
      {
        fanalogValue = degreeRotation/180.0*255.0 ;
        analogValue  = fanalogValue;   
        Serial.print("Analog Value:");
        Serial.println(analogValue,DEC);
        analogWrite(outputPin,analogValue);
        degreeRotation += 5;
        delay(5000);
      }
      else {degreeRotation = 0;}
  }


JimboZA

Quote
The code is simple (I hope)


hsteve, I think you should try that with the servo library... you don't position a servo with a simple analogue write: the servo library takes care of sending pulses of the correct characteristics with a servo write command.

Have a look at this tutorial.
Roy from ITCrowd: Have you tried turning it off an on again?
I'm on LinkedIn: http://www.linkedin.com/in/jimbrownza

hsteve

Jimboza,

Much appreciated, that seems to be something I can work with.

Thanks

zoomkat

Servo test code.

Code: [Select]

// 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);  //the pin for the servo control
  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
  }
}


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

bslook

Thanks everyone for the help. 

I hooked it up to an external battery and it is working fine.

Now I know where to go with my quesitons.

Thanks again.

Go Up