Pages: [1]   Go Down
Author Topic: Newbe Need help with a simple Servo Problem  (Read 871 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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   
   }
Logged

0
Offline Offline
Tesla Member
***
Karma: 141
Posts: 9470
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
This has to be something simple.

Servos generally are not successfully used when powered from the arduino 5v. Could this be your problem?
Logged

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

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17261
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Offline Offline
Faraday Member
**
Karma: 80
Posts: 3795
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.


* one servo.jpg (889.78 KB, 2838x1473 - viewed 25 times.)
« Last Edit: February 17, 2013, 10:27:46 am by JimboZA » Logged

Although I answer in good faith, remember always: Your Mileage May Vary.
Get me on LinkedIn: za.linkedin.com/in/jimbrownza/

Orange, NSW, Australia
Offline Offline
Jr. Member
**
Karma: 0
Posts: 50
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
//////////////////////////////////////////////////////////////////////
// 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;}
  }
Logged

Offline Offline
Faraday Member
**
Karma: 80
Posts: 3795
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Although I answer in good faith, remember always: Your Mileage May Vary.
Get me on LinkedIn: za.linkedin.com/in/jimbrownza/

Orange, NSW, Australia
Offline Offline
Jr. Member
**
Karma: 0
Posts: 50
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Jimboza,

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

Thanks
Logged

0
Offline Offline
Tesla Member
***
Karma: 141
Posts: 9470
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Servo test code.

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);  //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
  }
}


Logged

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

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: [1]   Go Up
Jump to: