How to control a Servo

by Oscar
In this one, servo is programmed to be controlled by two buttons, one turns servo to the left and the other one turns it to the right. When the servo is turning, corresponding LED will be switched on to indicate the operation.

Some of the links on this page are affiliate links. I receive a commission (at no extra cost to you) if you make a purchase after clicking on one of these affiliate links. This helps support the free content for the community on this website. Please read our Affiliate Link Policy for more information.

Result:

 
 
 
Because Arduino has built-in library for controlling servo, which makes servo a really easy kit to use. In this project, I will be using a servo to turn the IR sensor around constantly.
This is the circuit Diagram:
This is the code:

// Oscar’s Project

// 
// There are 2 input buttons (turn left and right), when button is pressed, the servo turns and corresponding LED is lit up.

#include  

Servo myservo;  // create servo object to control a servo 
 // a maximum of eight servo objects can be created 

int pos = 90;    // variable to store the servo position 
const int maxDeg = 160;
const int minDeg = 5;

const int leftPin = 3;
const int rightPin = 2;

const int led1Pin = 6; // indicator
const int led2Pin = 5; // indicator

const int outputPin = 9; // pwm function will be disabled on pin 9 and 10 if using servo

int leftPressed = 0;
int rightPressed = 0;

void setup() 
{ 
myservo.attach(outputPin);  // attaches the servo on pin 9 to the servo object 
pinMode(leftPin, INPUT);
pinMode(rightPin, INPUT);
pinMode(led1Pin, OUTPUT);
pinMode(led2Pin, OUTPUT);
} 

void loop() 
{ 
leftPressed = digitalRead(leftPin);
rightPressed = digitalRead(rightPin);
 
 if(leftPressed){
 if(pos < maxDeg) pos += 3;
myservo.write(pos);              // tell servo to go to position in variable ‘pos’ 
digitalWrite(led1Pin,HIGH);
 }
 else
digitalWrite(led1Pin,LOW);
 
 if(rightPressed){
 if(pos > minDeg) pos -= 3;
myservo.write(pos);              // tell servo to go to position in variable ‘pos’ 
digitalWrite(led2Pin,HIGH);
 }
 else
digitalWrite(led2Pin,LOW);
 
delay(15);                       // waits 15ms for the servo to reach the position 

}

Leave a Comment

By using this form, you agree with the storage and handling of your data by this website. Note that all comments are held for moderation before appearing.

5 comments

massam 5th October 2015 - 7:10 am

Hi Oscar

Thank you very much for this great tutorial. The code works perfect, but I have one problem that I was not able to resolve.

I need the left push to turn left from position 0 up to 45 degrees left, and then with the right push to return to zero position and then turn right from the 0 up to 45 degrees right as well.

Basically, it has to turn left 45 and right 45 from the 0 position every time either push buttons pressed.

Appreciate your help in advance.

massam

Reply
massam 5th October 2015 - 7:07 am

Hi and thank you for this great tutorial. The code works perfect, but I have one problem that I was not able to resolve.

I need the left push to turn left from position 0 up to 45 degrees left, and then with the right push to return to zero position and then turn right from the 0 up to 45 degrees right as well.

Basically, it has to turn left 45 and right 45 from the 0 position every time either push buttons pressed.

Appreciate your help in advance.

massam

Reply
Franz 17th August 2014 - 4:28 pm

Hi,

Thank you for this tutorial.
I took this as a base for another project but it’s not working like expected..
Basically I have 4 Buttons and a button-push should move the servo another 90 Deg. Therefore, I specified minDeg = 0 and maxDeg = 360 (I hacked the servo to turn 360 Deg) and int pos = 0.
I also tried to change the if statements but unfortunately it’s not working.
Any Idea how I could this make work?

Reply
Oscar 18th August 2014 - 10:02 am

I think the servo library in Arduino only support 0 to 180 rotation. I have never done any hacking to servo to turn 360 degree, so I am not sure.

Reply