Potentiometer + RC Servo motor

Hiya pplz ! Do any of u know where i can find example codes for a potentiometer controlling the degrees of turn for a RC Servo. Thnx

http://todbot.com/arduino/sketches/servo_move_simple/servo_move_simple.pde

Here is a modified version of the above:

int servoPin = 7;            // R/C  Servo connected to digital pin
int myAngle;                 // angle of the servo (roughly in degrees) 0-180
int pulseWidth;              // function variable
 
void servoPulse(int servoPin, int myAngle) {
  pulseWidth = (myAngle * 11) + 500;  // converts angle to microseconds
  digitalWrite(servoPin, HIGH);       // set servo high
  delayMicroseconds(pulseWidth);      // wait a very small amount 
  digitalWrite(servoPin, LOW);        // set servo low
  delay(20);                          // refresh cycle of typical servos (20 ms)
}

void setup() {
  pinMode(servoPin, OUTPUT);          // set servoPin pin as output
  Serial.begin(115200);
}

void loop() {
  myAngle= analogRead(0);             // read a potentiometer connected to analog pin 0 
  myAngle= (myAngle - 50);
  
  // Serial.println(myAngle);
  
     servoPulse(servoPin, myAngle);
  
  delay(10);
}

thnx Daniel .. but i've figured out the best code to control the servo smoothly.. thanx to http://itp.nyu.edu/physcomp/Labs/Servo instead of the sensor i changed it to potentiometer. The servo turns exactly how you turn the potentiometer's knob. Here are the codes

/*
Servo control from an analog input

The minimum (minPulse) and maxiumum (maxPuluse) values
will be different depending on your specific servo motor.
Ideally, it should be between 1 and 2 milliseconds, but in practice,
0.5 - 2.5 milliseconds works well for me.
Try different values to see what numbers are best for you.

This program uses the millis() function to keep track of when the servo was
last pulsed. millis() produces an overflow error (i.e. generates a number
that's too big to fit in a long variable) after about 5 days. if you're
making a program that has to run for more than 5 days, you may need to
account for this.

by Tom Igoe
additions by Carlyn Maw
Created 28 Jan. 2006
Updated 7 Jun. 2006
*/

int servoPin = 2; // Control pin for servo motor
int minPulse = 500; // Minimum servo position
int maxPulse = 2500; // Maximum servo position
int pulse = 0; // Amount to pulse the servo

long lastPulse = 0; // the time in milliseconds of the last pulse
int refreshTime = 20; // the time needed in between pulses

int analogValue = 0; // the value returned from the analog sensor
int analogPin = 0; // the analog pin that the potentiometer's on

void setup() {
pinMode(servoPin, OUTPUT); // Set servo pin as an output pin
pulse = minPulse; // Set the motor position value to the minimum
Serial.begin(9600);
}

void loop() {
analogValue = analogRead(analogPin); // read the analog input
pulse = (analogValue * 19) / 10 + minPulse; // convert the analog value
// to a range between minPulse
// and maxPulse.

// pulse the servo again if rhe refresh time (20 ms) have passed:
if (millis() - lastPulse >= refreshTime) {
digitalWrite(servoPin, HIGH); // Turn the motor on
delayMicroseconds(pulse); // Length of the pulse sets the motor position
digitalWrite(servoPin, LOW); // Turn the motor off
lastPulse = millis(); // save the time of the last pulse
}
}

I know this is an old thread but I just wanted to point out that not all servos will be able to handle 500 to 2500ms.

Start with 1000-2000ms and gradually increase the range. If the servo buzzes at the low or high limit, that's as far as it wants to go. A servo trying to move past it's limit will draw quite a bit (relatively) of current and can even damage the servo gears.

  • Jon