Help require for Servo Control Problems ...

Hi everyone! ^___^

I've been trying to get some basic control for some servos for the past few hours (sorry, I'm a noob at coding :~ ) and after following the tutorials, I've had no luck whatsoever. All I want the servos to do is just stay in their "0" position and not move but every time I upload the program and the servos just move off to some random positions ...
I have absolutely no idea where to go from this so I was wondering if someone could shed some light on this for me - I'd appreciate it greatly! :grin:

Thanks in advance! ^___^

Here's my code, I'm trying to run 6 servos simultaneously:

#include <Servo.h>

Servo RH; Servo RK; Servo RA; 
Servo LH; Servo LK; Servo LA;

void setup()
{
  RH.attach(4);
  RK.attach(5);
  RA.attach(6);
  
  LH.attach(7);
  LK.attach(8);
  LA.attach(9);
}

void loop()
{
    RH.write(0);
    delay(10);
    RK.write(0);
    delay(10);
    RA.write(0);
    delay(10);
    
    LH.write(0);
    delay(10);
    LK.write(0);
    delay(10);
    LA.write(0);
    delay(10);
}

AKdaBAOUS

Well the code looks ok. There is often an unexpected movement at power up- do the servos actually settle at 0 though?

You should post a schematic of the wiring... Hopefully you have the power from a source which is not the Arduino, and have the power ground hooked up to the Arduino ground.

They do briefly settle at 0 but only for about half a second (or less) before going haywire.
I'm actually using a custom made board but the servos are being powered from an external bench power supply so I don't think that's a problem ...

Do you have the grounds commoned?

Have you tried with only one servo? Maybe the supply can't provide the current and the voltage is dropping too low?

Post a schematic....

I actually haven't tried with a single servo :blush: ... I shall do that now. :sweat_smile:

AKdaBAOUS:
I actually haven't tried with a single servo :blush: ... I shall do that now. :sweat_smile:

You may be surprised how much current those suckers can pull... I'm sure you're testing under no or little load, but it still might be a couple of hundred mA each, maybe pulling 1.5A or more together. What can your supply supply?

Retrolefty, a member here, budgets 1A each to allow for heavy lifting.... ie stalling.

Edit.... btw you still haven't answered this question: are the grounds from the supply to the motors, hooked up to the Arduino ground?

Sorry about that, yep, the grounds are connected to the Arduino ground. :slight_smile:
Oh, and the supply can give out a max. of 5A.

Ok, so a single servo seems to work fine ...
I have a servo controller IC anyway and I was originally intending to use that - I just wanted to see if I could get the servos up and running directly from the Arduino but that doesn't seem to be working so I'll put a lid on it.

Thank you for your help anyway. :grin:

Ok, so I might have been too quick to give up there. :sweat_smile:
I gave the code another go and this time, I added in ...

pinMode(pin, OUTPUT)

... for each of the servos and now everything works fine. XD
As far as current pulling goes, the 6 servos in total were only pulling about 300mA - then again, they are pretty small and the frame they are fixed into is incredibly light. :grin:

Anyway, I've posted an annotated copy of the final code in case anyone else ever has this problem. Hope it helps! :wink:

// Description: Setting 6 servos in a robot's legs to their
// original "0" position. 
// Date: 29th April 2013.

// Include the relevant Servo Library.
#include <Servo.h> 

// Provide names for the servos to be controlled.
Servo RH; Servo RK; Servo RA; // Right Leg.
Servo LH; Servo LK; Servo LA; // Left Leg.

// This was the key part of getting it to work - I had to set
// the pins that the servo was connected to as OUTPUTS. Then
// it worked perfectly fine.
void setup()
{
  pinMode(2, OUTPUT);
  RH.attach(2);
  pinMode(3, OUTPUT);
  RK.attach(3);
  pinMode(4, OUTPUT);
  RA.attach(4);
  
  pinMode(5, OUTPUT);
  LH.attach(5);
  pinMode(6, OUTPUT);
  LK.attach(6);
  pinMode(7, OUTPUT);
  LA.attach(7);
}

void loop()
{
  // Gives me time to lift the robot off the table.
  delay(5000);
  
  // Sets all the servos in the Right Leg to their origin.
  RH.write(90);
  RK.write(90);
  RA.write(90);
  
  // Sets all the servos in the Left Leg to their origin.
  LH.write(90);
  LK.write(90);
  LA.write(90);
}

AKdaBAOUS

That's very strange: the standard servo examples like knob don't set the pin as an output....

I'm not too sure if this is of any relevance or not but in all the tutorials I've seen, they've all used Pins 9 & 10 ... could they be special pins in this case? (I doubt it though :slight_smile: ).

JimboZA:
That's very strange: the standard servo examples like knob don't set the pin as an output....

Your correct, one doesn't need to (or required to) set servo pins as output pins in ones main sketch as the servo library does that on it's own when one performs the servo attach function.

The reason many might use pins 9 and 10 for servos on 328P based boards is because the servo library takes over the timer that analogWrite() function uses for those two pins so might as well use those two pins for servos over other output pins where one might want to utilize analogWrite() outputs.

On boards other than the Mega, use of the library disables analogWrite() (PWM) functionality on pins 9 and 10, whether or not there is a Servo on those pins.

Lefty