Controlling a Servo using a 10k pot and Arduino

Introduction

Here we will control a servo by rotating a 10k potentiometer, with Arduino Uno as controller. But before going through this tutorial, i suggest you go through this basic introductory article on Servos.

The 10k pot is connected to one of the analog pins and the servo is connected to one of the pwm pins of the arduino board.

A servo can be made to turn to any angle between 0 to 180 degrees by using Arduino’s inbuilt Servo library.

Here we map the analog reading between 0-1023 obtained from the 10k pot to an angle between 0 – 90 degrees, by using arduino’s inbuilt map() function and use the mapped value to run the servo. This makes the servo rotate  in accordance with the rotation of the 10k pot.

Breadboard Setup

Arduino-servo-pot-interfacing_bb.png
Breadboard setup

 

Note: A servo has three wires +VCC, GND and SIGNAL . Different servos use different color codes to indicate +VCC, GND and SIGNAL .

a4228312-146-user38_pic55606_1294883044
Servo Color codes

Schematic Diagram

Arduino-servo-pot-interfacing_schem
Schematic diagram

Code

#include<Servo.h>

Servo myServo; //Declaring a Servo object
int servoPin = 10; //declaring a variable to store the pin number to which the servo is connected
int potValue = 0; //declaring a variable to store potentiometer analog value
int potPin = 0; //10k pot connected to pin 0 of analog port
int servoPosition = 0; //declaring a variable to store the angle for servo rotation
void setup() {
  // put your setup code here, to run once:
  myServo.attach(servoPin); //attaching the servo object to the servo pin
}

void loop() {
  // put your main code here, to run repeatedly:
 potValue = analogRead(potPin); //get the analog value of 10k pot
 servoPosition = map(potValue,0,1023,0,180); //map the analog value of the 10k pot from a value between 0 - 1023 to a value between 0 - 180 and store it in a variable
 myServo.write(servoPosition); //turn the servo by the angle value stored in servoPosition variable
 delay(100);

}

 

 

 

Advertisements

One thought on “Controlling a Servo using a 10k pot and Arduino

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s