Arduino and Servo

Hey guys!

I bought Arduino Mega and Servos (Small) http://www.sparkfun.com/commerce/produc … ts_id=9065

I tried the Servo Library from the Arduino, it doesn’t work. It just moves a bit and stops. I don’t know whats wrong. I thought maybe the servo is not working. I inserted another servo. Same thing. Please help me!

Thanks!

Can you post the code you’re using with an explanation of how you have everything hooked up?

Servo’s don’t rotate 360 degrees. Usually they are fed a pulse width modulated signal which tells them what position they should be in. Changing the pulse width will change the position of the servo head.

I used the Servo library example Sweep Code

// Sweep
// by BARRAGAN <http://barraganstudio.com> 

#include <Servo.h> 
 
Servo myservo;  // create servo object to control a servo 
                // a maximum of eight servo objects can be created 
 
int pos = 0;    // variable to store the servo position 
 
void setup() 
{ 
  myservo.attach(9);  // 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(15);                       // waits 15ms for the servo to reach the position 
  } 
  for(pos = 180; pos>=1; 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 
  } 
}

I connected the White Pin to the 9 pin in the board, and Red pin to the +5 on the board, and black to the ground

I’m not sure if the arduino can supply enough power to run a servo directly. Are you using a motor shield of some sort? Did you recieve any specs with the motor? The product page for the servos don’t have any data regarding the motor’s power requirements.

The PWM pin doesn’t power the servo; it just controls it.

I tried connecting the Power and Ground externally to the battery and then connecting the signal to the Arduino Board. But it doesn’t work that way either.

If it’s not a power related issue, I would guess that the servo isn’t getting a changing PWM signal. An oscilloscope, if you have one available, would show if the Arduino is doing the right thing or if you have a defective servo. I haven’t done it with an Arduino, but you might try manipulating the PWM channel directly instead of using the servo library.

I don’t have an Oscilloscope at home, but I do have at my University (by the way, I am Electronics Engineering Student).

but you might try manipulating the PWM channel directly instead of using the servo library.

And how exactly that would be done? I am sorry, but I am kinda noob at Servos, actually the servos aren’t available in my country, I have bought from Sparkfun for the first time, I have no experience of them. And I really want help to get these Servos started.

Anybody who has used Servos and Arduino, could help me. Thanks guys![/quote]

PWM is commonly used for LED fading:

http://arduino.cc/en/Tutorial/Fading

Some minor modifications to that example should get you going - or - at the very least, the example sketch should give you some ideas of how to adapt your example to using analogWrite() instead of the servo library. At the least, you’ll determine if it’s a problem with the library or not.

When I use the Fading Example on the Servo, the servo rotates about a few degrees and then stops, and then it rotates more and then stops. After it reaches its extreme point, it no longer rotates.