Servo Troubles

Hey everyone,

So I've got a servo hooked up to direct-drive a dimmer switch. Simple enough, right. However, the alignment is hard to get right, because the dimmer switch has a sweet spot that I want the servo to operate between.

This leaves me with a problem: it seems that when I run the "Knob" example in Arduino, my alignment gets screwed up. Funny thing is, when I run the "Sweep" example, I can run it for hours on end without any alignment issues.

So here's the question: is my servo just crappy (I've tested four of the same brand with the same results)? Or is there something happening in the software, resetting the "zero" point for the servo that I cannot see?

Any advice would be much appreciated. Thanks!

the dimmer switch has a sweet spot that I want the servo to operate between.

Does this mean that you want the servo to be limited to a certain range of motion? Like between 37 and 80 degrees?

when I run the "Knob" example in Arduino, my alignment gets screwed up.

That's kind of vague. Perhaps all the servo needs is a stern lecture on proper behavior. Probably not. What do you mean by screwed up?

Or is there something happening in the software ... that I cannot see?

You didn't post the code, so we can't see, either.

Sorry for the missing details.

Basically, I’m having trouble getting the servo to consistently report to given positions. For example, when using the “Knob” example in Arduino (code below) every time I run a given servo.write, lets says servo.write(10);, the servo does not necessarily move to the same position every time. It seems as if every time I run the software, it will go to a new physical position a good deal off of where it reported servo.write(10); was before.

The odd thing is when I run the Sweep example in Arduino (code below) the servo does in fact report to the same physical position for each value of the servo.Write(); function.

This leaves me with the question: is my servo crappy, and therefore the Sweep example is just coincidentally getting “close enough” each time its run, or is my servo okay and something is going on in the software I cannot see.

Knob Example

// Controlling a servo position using a potentiometer (variable resistor) 
// by Michal Rinott <http://people.interaction-ivrea.it/m.rinott> 

#include <Servo.h> 
 
Servo myservo;  // create servo object to control a servo 
 
int potpin = 0;  // analog pin used to connect the potentiometer
int val;    // variable to read the value from the analog pin 
 
void setup() 
{ 
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object 
} 
 
void loop() 
{ 
  val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023) 
  val = map(val, 0, 1023, 0, 179);     // scale it to use it with the servo (value between 0 and 180) 
  myservo.write(val);                  // sets the servo position according to the scaled value 
  delay(15);                           // waits for the servo to get there 
}

Sweep Example

// 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'm guessing it's not the servo. With the Sweep code, you are sending the servo to a precise location, and waiting for it to get there before moving it again.

With the Knob code, the servo location is based on a potentiometer setting. The potentiometer may not be wired correctly, it may not be linear, and it may not be accurate (i.e. repeatable).

What kind of potentiometer are you using, and how is it connected?