Servo Control

Hello,
I have been working on a project that requires a servo...this one:

To turn back and forth

And i have been having some trouble with it.

I have all of the connections fixed correctly to the arduino
Here is the code i am using

#include <Servo.h> 

Servo myservo; 

int pos = 0;

void setup()
{
  myservo.attach(9);
}

void loop()
{
  for(pos = 0; pos < 180; pos += 1)
  { 
    myservo.write(pos);
    delay(25);     
  }
  for(pos = 180; pos>=1; pos-=1)  
  {
    myservo.write(pos);
    delay(25);                    
  }
}

The servo does not move...at all When using USB, but it does turn when using the 9volt

When i am using the 9volt it turns randomly for a while...then turns how i specified in the code. Then after the first 180....it stops..then spins quickly back to the original position...then repeats....and sometimes moves around randomly :-?

All i need this servo to do is turn back and forth.....and it would be great if i could get it to go back and forth 90 degrees

Sounds like a current problem.

The servo does not move...at all When using USB

USB most likely cannot supply enough current to drive a servo.

When i am using the 9volt it turns randomly for a while...then turns how i specified in the code. Then after the first 180....it stops..then spins quickly back to the original position...then repeats....and sometimes moves around randomly

9v batteries likewise.
The arduino also may not be able to source enough current from its 5v pin.

The random moving and jumping around may be due to the arduino browning out, and resetting.

so do you think i could hook up two 9volts to a 5volt reg. (2 9volts to make it last longer) then just hook that up the 5volt wire, and the battery ground to the ground on the servo?

so do you think i could hook up two 9volts to a 5volt reg. (2 9volts to make it last longer) then just hook that up the 5volt wire, and the battery ground to the ground on the servo?

If you mean in "parallel" (NOT series), then yes, this could help, but you will still be fighting with the fact that a 9V stinks as a current source. Most 9V alkaline batteries have at best around a 500 maH rating, which is really small. Double it up (like you posted), you might get 1 AH.

Some of that is going to be lost in the voltage drop on the regulator (as heat); even so, your servos likely will use a bit of current, especially if turning slowly (or little) - let's say around 200 ma (could be higher or lower, depending on the servo); with a one amp hour battery, you can see that we won't get much in the way of use (5 hours continuous, roughly).

9V batteries stink for just about anything other than transistor radios and small handheld games (oh, and powering multimeters). Instead of a 9V battery, use something better like a battery pack made from AA or C-cell batteries; or an R/C pack (7.2 or 9.6 V) made from sub-C rechargeables.

ok...i have a AA battery pack, it holds four....should i use this to hook up to the 5 volt reg.?

ok...i have a AA battery pack, it holds four....should i use this to hook up to the 5 volt reg.?

Read up on your 5v regulator to see what the dropout voltage is. The 4 AA batteries should directly power the servo for a little while. I have some of that type servo (below) and it should work ok off of 6v.

So do i just hook up the ground and the power from the servo, directly up to the positive, and negative sides of the battery pack?

So do i just hook up the ground and the power from the servo, directly up to the positive, and negative sides of the battery pack?

Yes, but you also must run a wire from the battery negative to an Arduino ground pin. Then that should do it.

Lefty

So do i just hook up the ground and the power from the servo, directly up to the positive, and negative sides of the battery pack?

You also have to connect the battery and arduino grounds together, and connect the arduino servo pin to the servo control wire.

LOL! i jst figured that out...hahaah i waz like...pop in here...then....pop in there...and..BOOP...LOL it worked....ahhahahaha...

It didnt make sense to me where the output from the pin was going....then i realized it needed to go to ground also...

It didnt make sense to me where the output from the pin was going....then i realized it needed to go to ground also...

Well, that doesn't sound right, but if it is working, just consider yourself real lucky!