Saturday, February 16, 2013

Arduino Basics - Controlling A Servo Motor With A Potentiometer

Cautionary word of advice: the limits I set in the code are based on the thresholds of my particular servo motor, you may need to adjust them to avoid damaging your motor.
[sourcecode language="c"]
pval=map(pval,0,1023,0,165); // The 165 in here may need to be adjusted
// Default limits are 0 - 180
// See below code for details
[/sourcecode]

The Circuit



Potentiometer Controlling a Servo Motor


The Code


[sourcecode language="c"]
#include <Servo.h>
Servo s;
int p = 0; // Potentiometer
int pval = 0;

void setup(){
s.attach(10);
Serial.begin(9600);
}
void loop(){
pval=analogRead(p);
pval=map(pval,0,1023,0,165); // <-- Adjust to motor thresholds
Serial.println(pval);
delay(20);
s.write(pval);
delay(10); // Smooth it a bit
}
[/sourcecode]

Circuit Implementation



Servo Potentiometer Circuit Setup

1 comment:

  1. [...] Cautionary word of advice: the limits I set in the code are based on the thresholds of my particular servo motor, you may need to adjust them to avoid damaging your motor. The Circuit The Code Circ...  [...]

    ReplyDelete