Servo issues

I am having an issue programming for my servos; I did a search but did not see anything about this.

I am using Cirrus CS101 4g Micro Servos, which are coreless servos.

Just to test them out, I hooked one up to my Arduino pro mini 328 and ran the example sketch "knob" which has you control a servo with a potentiometer. I was using a 10k potentiometer and the only thing I changed in the program was I switched the signal pin for the servo to pin 10.

Well, my issue is, when I load that sketch, the servo just spins around and around.

I then added serial.print function to the program to double check if there was actually a signal being sent to the servo that was determinate on the potentiometer, and there was.

Then, while searching, I found this in the description of the servo library:

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.

So I changed the pin the servo was on to 11, but I still had the same issue.

Anyone have any idea what's going on? I figured this was going to be the easy part of my project :(

Thanks for the help again. . .

Well, my issue is, when I load that sketch, the servo just spins around and around.

If it really is spinning around and around then perhaps you have a continuous rotation servo. These can continuously spin in one direction or another, to stop the servo you need to write a value of around 90 degrees, although the exact value varies with the servo used.

I have not used that cirrus servo but a normal servo intended for use in model aircraft will not spin around and around.

A simple test you can try is to modify the knob example by removing the code in loop between the curly brackets – in other words, don’t write anything to the servo in loop, just attach the servo to the correct pin in setup. The servo library will write a value of 90 to the servo when it starts so the servo should move to that position and stay there.

If your servo is making erratic movements with that test then a likely cause is a problem with the wiring.

Good luck.

BTW, the statement quoted simply means that you can’t use the analogWrite function on pins 9 and 10, however the servo library uses a method other than analogWrite to produce the control pulses so you can connect a servo on these or any other pins.

Thank you for the reply.

I tested the servo like you said, by putting library and servo commands in the program but not putting anything in the loop().

Once again, it just spun continuously. Now, if I unplug the digital pin, but leave the power and ground attached, it obviously stops. So this makes me think its a programming issue.

mbburkit, can you clarify what you mean by continuously spinning? Is it really continually spinning without stopping or changing direction?

If so, perhaps do a search on continuous rotation servos for a detailed explanation of how to get the to stop rotating.

If not, can you say more about the kind of movements the servo is making?

Here, I took a video of it. I guess its not spinning continuously. It will spin about half way, then take a bunch of steps, then spin again.

http://s66.photobucket.com/albums/h279/mburkit/PKE%20meter/?action=view¤t=M4V02249.flv

Please watch the video and tell me what you think. Thanks again!

That looks like a continuous rotation servo. Try using the knob example sketch and see if there is a position that will stop the rotation.

Nope, no spot on the potentiometer would stop the servo. I also had the Serial.println(val); line in the code to see what values the potentiometer was giving (so I could tell you where the servo would stop at what value), but the potentiometer seems to be working fine.

so I'm stumped.

You know what, I think it might just be that servo. I decided to plug the other one in and it seems fine! I'm going to do some tests on it to see if that's the case.

That would just be my luck I plug in the "bad" servo first and loose a nights sleep over it.

Okay, well I still have an issue. One servo just continuously spins and the other does nothing (but I can hear it humming).

So since I was getting 2 different erratic behaviors from the servos, I thought "maybe its the servos," since a friend of mine told me they were aren't the best brand.

So I bought 2 Futaba S3114's and brought them home to see if it was a hardware issue.

I plugged on in with my program not having any thing in the loop() to do with the servo but having everything else (library, assigning a pin for the servo, etc.). The servo corrected it self to its 0 point.

But that's all that the servo would do. I loaded the "knob" sketch and nothing.

Then I decided to use my multimeter to check that all all my connections where okay. While doing this, I realized that when I have the servo hooked up to my arduino, my 5v+ power source drops to 2.25v'ish. So now I think I have something messed up? Should I consult the hardware forum?

Drive the servos from a separate battery pack (6 volts). Hook the positive of the pack to the positive on the servos (red wire, usually), hook the negative of the pack to the negative on the servos (black, usually), hook a wire from the negative of the pack to the ground on the Arduino, then hook the Arduino signal wire to the signal wire (white usually) on the servos.

The servos are consuming a ton of current while starting up and running, which causes all manner of strange stuff to happen on the Arduino (bad sensor reads, resets, etc). You need to put them (and really, any kind of higher current device) on a "separate" supply (or pull the current directly from an external supply and run it through your own larger current capable regulator (if needed) before running it to the device).

Hope this makes sense and helps you!

Sounds like you have a wiring problem, can you post a picture of how things are connected up?

Okay, its sort of a mess because its on a bread board and the servos are part of a bigger project.

I’m basically working on the electronics of a movie prop, the PKE Meter from Ghostbusters.

Here is the picture of my bread board prototype so far.

Just to clarify what you are seeing in the picture:

Pin 13 is being used as a 5v+ power source for:

  • The Potentiometer that is wired to A0, digital pin13 and GND
  • 4017 decade counter which is hooked up to Dpin7, Dpin13, and GND
  • 2 resistive contact switches that are using 10k resistor, each which are also connected to digital pins 11-12 (respectively)

At any given time, there are ever only 1 LED on and the contact switches on the 5v+ coming from pin13.

There are 2 LEDs wired to blink on Dpin7 and GND

And there’s a switch that is used to switch between 2 different sets of LEDs wired to the 4017 (which will be for 2 different LED displays).

Here’s a video I made last night about the functionality of the electronics, for a friend to see:

Video

And lastly, here is my code so far:

#include <Servo.h>

Servo servo1; // create servo object to control servo 1
Servo servo2; // create servo object to control servo 2
const int Screendisplay = 7; // LED connected to digital pin 07
const int Screenindicator = 8; // LED connected to digital pin 08
const int winghalfLED = 11; // digital pin for the wings at half LED
const int wingfullLED = 12; // digital pin for the wings at full LED
const int pin6 = 6; // digital pin set as a 5v+ (for the servos)
const int pin13 = 13; // digital pin set as the 5v+ (for the POT, 4017, and contact switches)
int resistance1; // variable for the resistance change on the first resistive switch
int resistance2; // variable for the resistance change on the second resistive switch
int potpin = 0; // analog pin used to connect the potentiometer
int resistswitch1 = 1; // analog pin used for the resistive switch 1
int resistswitch2 = 2; // analog pin used for the resistive switch 2
int val; // variable to read the value from the analog pin
int pos1 = 0; // sets default position of servo 1
int pos2 = 0; // sets default position of servo 2
int ledState = LOW; // ledState used to set the LED
long previousMillis = 0; // will store last time LED was updated
long interval = 750;

// The setup() method runs once, when the sketch starts

void setup() {
// initialize the digital pin as an output:
pinMode(Screendisplay, OUTPUT);
pinMode(Screenindicator, OUTPUT);
pinMode(winghalfLED, OUTPUT);
pinMode(wingfullLED, OUTPUT);
pinMode(pin6, OUTPUT);
pinMode(pin13, OUTPUT);
servo1.attach(10);
servo2.attach(9);
}
// the loop() method runs over and over again,
// as long as the Arduino has power

void loop()
{
//makes pin 6 and 13 the 5v power source of the board
digitalWrite(pin6, HIGH);
digitalWrite(pin13, HIGH);
//Digital Pin 7 - 4017 decade counter for the screen display
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 0, 1023, 10, 100);
digitalWrite(Screendisplay, HIGH); // set the LED on
delay(val); // wait for the val of the pot
digitalWrite(Screendisplay, LOW); // set the LED off
delay(val); // wait for the val of the pot

//Digital Pin 8 - blinking LED
if (millis() - previousMillis > interval){
previousMillis = millis();

// if the LED is off turn it on and vice-versa:
if (ledState == LOW)
ledState = HIGH;
else
ledState = LOW;

digitalWrite(Screenindicator, ledState); // set the LED with the ledState of the variable
}

// Resistive switches and indicator LEDs
resistance1 = analogRead(resistswitch1);

if (resistance1 >= 1 && resistance2 == 0)
{
digitalWrite(winghalfLED, HIGH);
}
else
{
digitalWrite(winghalfLED, LOW);
}

resistance2 = analogRead(resistswitch2);

if (resistance2 >= 1 && resistance1 == 0)
{
digitalWrite(wingfullLED, HIGH);
}
else
{
digitalWrite(wingfullLED, LOW);
}
}

Cool project.

What is the power source? As chr0sh2 says, you may want to try powering the servo from a battery or use a more powerful 5v power supply as a test to see if this is the problem.

There is a post here that has information on wiring using a battery: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1250070061/6#6

That's what it was, a power issue. I hooked the servo up to a 9v battery regulated to 5v and then ran the "sweep" code, and it worked!

Now, my issue is, setting the delay for the servo to move to a certain position without that delay affecting the rest of the program (i.e. the LEDs on the potentiometer).

Is the delay(15) code that is used in both the "knob" and "sweep" examples necessary for the servo move or can I do without it? And if I do without it, how do I modify the speed the servo gets from point A to point B?

The delay function gives the servo time to get to the commanded position before it is told to go somewhere else.

The same effect can be achieved using millis() to determine when it is time to tell the servo to move again. You'll need to keep track of when the last move command was issued, using an unsigned long variable.

The speed that the servo moves is a function of the delay at each step. Shorter delay = faster movement.

Hi,

I haven’t read this thread completely, but just to drop in some info:-

I have been playing with RC servo’s and my Arduino, and I tried the library servo.h, however in the end I resorted to simple code.

Set up a 40mS interrupt using TimedAction.h library

#include <TimedAction.h>
TimedAction Timedact01 = TimedAction(40,TimerService01); // 40mS

In that 40mS loop have the following:-

Note: AI_0 is a variable containing the value read in from a potentiomenter.

void TimerService01(){

AI_0 = map(AI_0, 0, 1023, 1000, 2000); // y=mx, 0 to 1023 scales to 1000 to 2000mS for the servo
        if (AI_0 < 1000) AI_0 = 1000; // Min = 1000mS
        if (AI_0 > 2000) AI_0 = 2000; // Max = 2000mS
        digitalWrite(DO_Pin6, HIGH);   // Turn the motor on
        delayMicroseconds(AI_0);       // Length of the pulse sets the motor position
        digitalWrite(DO_Pin6, LOW);    // Turn the motor off

} // End TimerService01

RC servo PWM freq is supposed to be 20mS, however, most will do just fine even above 100mS. Any faster than 20mS and they will jitter and do all sorts of funny things.
I chose 40mS in my prog just to fit in with everything else going on.

Ian.

So similar to how I did the blinking LEDs in my code then?

Also, I’ve noticed the servos twitch while in any position. Either when they are at “rest” at 90 or at any other position I move them to.

Is there any way to stop that?

Twitching servo's:

  • PWM freq too high?
  • Other interrupts running on the Arduino?

Ian.

Decoupling? Beefy enough PSU?

PWM freq too high?

sp. "PPM"

http://www.luolamies.org/misc/servo.html

Standard hobby RC servo pulses are just a specialized form of PWM. Typically the signal repeats every 20 ms (so 50 Hz PWM frequency), but this can vary. The pulse itself most often ranges from 1 ms to 2 ms, with ~1.5 ms being the neutral position. For servos with larger ranges, you might see pulses between 0.5 and 2.5 ms (neutral will still be around 1.5 ms).

Ian.