ARDUINO : Servo Angle Controlled by Potentiometer
by Surya Krishnakumar in Circuits > Arduino
10382 Views, 26 Favorites, 0 Comments
ARDUINO : Servo Angle Controlled by Potentiometer
This instructable shows how to control the angle of a servo motor using a potentiometer.
Part List:
1) Arduino ( any kind will do)
2) Bread board
3) Male to Male jumper wires
4) Servo motor
5) Potentiometer ( 10k should work fine)
Connecting the Potentiometer:
1) Connect the potentiometer to any three open rows on the breadboard.
2) Connect negative pin of the potentiometer to ground rail on the breadboard.
3) Connect positive pin of the potentiometer to power rail on breadboard.
4) Connect signal pin of the potentiometer to PIN0 on the Arduino.
Connecting the Servo:
1) Insert three Male to Male Jumpers into the servo's Female hub.
2) Connect the positive wire (usually red) of the Servo motor to power rail on the breadboard.
3) Connect the negative wire (usually black or brown) of the servo motor to the ground rail on the breadboard.
4) Connect the Signal wire (usually yellow, white or orange) of the Servo motor to PIN2 on the Arduino.
Connecting the Arduino:
1) Connect the 5vPIN of the arduino to the power rail on the breadboard.
2) Connect the GNDPIN of the arduino to the ground rail on the breadboard.
BREADBOARD LAYOUT:
ARDUINO CODE:
#include
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
}