Introduction: Servo Tutorial

Picture of Servo Tutorial

Using an Arduino to control a servo motor with the use of two pushbuttons.

Step 1: Get the Appropriate Components

Picture of Get the Appropriate Components

Please use the links provided if you need any of the components also check out the site for other great arduino goodies, they ship worldwide for free and there service is great. Plus you support me :)

Go to the site here.

Thanks for your support.

1) Arduino Link: Arduino Compatible Uno R3 Rev3 Development Board

2) Breadboard Link: Half-size 400-Pin Electronics DIY Breadboard or 830-Point Solderless Electronics DIY Breadboard

3) Push Buttons Link: DIP P4 Sqaure Switch Push Buttons (100-Pack)

4) Jumper Cables Link: Multicolored 40-Pin DuPont Breadboard Jumper Wires (20cm)

5) Two 10k Ohm Resistors Link: DIY Universal 1/4W 1% Metal Film Resistor (600PCS)

6) Servo Motor Link: Tower Pro SG90 9g Gear Steering Servo

Step 2: Connect the Power

Picture of Connect the Power

Connect the GND and 5V

Step 3: Connect the Servo

Picture of Connect the Servo

Red to 5V

Brown/Black to GND

Orange to pin 9

Step 4: Connect the Buttons

Picture of Connect the Buttons

One of the buttons to DIGITAL 2

The other to DIGITAL 4

Step 5: Check the Circuit

Picture of Check the Circuit

Make sure the circuit is correct

Step 6: The Code

I used the Sweep example from Arduino and altered it to work.

#include <servo.h>

const int buttonPin = 2;

const int buttonPin2 = 5;

int buttonState = 0;

int buttonState2 = 0;

Servo servoA;

int position = 0;

void setup() {

servoA.attach(9);

pinMode(buttonPin, INPUT);

pinMode(buttonPin2,INPUT);

}

void loop() {

buttonState = digitalRead(buttonPin);

buttonState2 = digitalRead(buttonPin2);

if(buttonState ==HIGH && position < 180){

servoA.write(position++);

delay(5);

}

if(buttonState2 == HIGH && position > 3){

servoA.write(position--);

delay(5);

}

}

Step 7: Complete

Picture of Complete

Comments

chupo_cro (author)2017-09-29

In the first picture under the heading 'Step 5: Check the Circuit' the white wire is at the 6th position from the right, isn't that DIGITAL 5?

gavinkeeley (author)chupo_cro2017-09-30

Hi Chupo_cro,


It does look that way in the photo, I have made the change in the code.

It doesn't really matter, you could make it anyone of the digital pins... Just change the number in the code accordingly.

Thanks for pointing this out.

Regards
Gavin

AmmarD2 (author)2017-08-21

I am using a modified servo motor(TowerPro MG995) which allow me to rotate continuously. are this set up and codes still relevant for this servo? If not, what is the alternative? thx in advance

gavinkeeley (author)AmmarD22017-08-22

Hi Ammar, unfortunately the code will not work with a continuous rotation servo and there is no simple change to the code that will rectify it...

The following link discusses the topic in more depth.
https://forum.arduino.cc/index.php?topic=267166.0

Unfortunately I do not have a continuous rotation servo to figure it out with.

Regards
Gavin

TeresoM (author)2017-06-08

Dear, Gavin, thanks for sharing. I have one doubt, however: what is the function of the "int position=0;" line?

Kind regards!

gavinkeeley (author)TeresoM2017-08-03

Hi TeresoM,

The function of that line is to set the position integer to zero. If you look further down in the code we set the servos position to that integer. So 0 will be the most it can go in the one direction and 180 will be the most it can go in the other. We then increase/decrease the value of this integer depending on which button is being pressed.

Sorry for the untimely response.

Regards

Gavin

ghorgex (author)2016-12-14

Seems the servo runs too fast, how to reduce it?

gavinkeeley (author)ghorgex2017-08-03

Hi ghorgex,

To reduce the speed at which the servo rotates you can increase the value in the delay functions. i.e. Increase delay(5); to say delay(10); this will effectively halve the rotation speed.

Regards

Gavin

HunterT17 made it! (author)2017-02-07

great tutorial, I will try expanding to more servos and more delicate and complex motions. thanks!

TineL2 made it! (author)2016-09-19

Thanks. Code works wery vell. I put another servo and potenciometer to set the speed of servos.

gavinkeeley (author)TineL22016-09-19

Sure

biddlejimmy (author)2016-08-19

The code on this page will not verify. Get High was not declared in this scope. Can you help???

HectorM53 (author)2016-07-03

hey. sorry but... how can I add another servo. I tried just declaring it and then use button #2 for moving it. basically copying the first "if" sentence activasting a second servo in the same direction, but it did not work. none of the servos move. not even the first

Daoina (author)2015-12-14

If I try to use the code it's missing something with the #include part, can you help?

gavinkeeley (author)Daoina2016-04-07

Hi Daoina

I have fixed the code.

Thanks very much for pointing out the error.

It seemed to be left out when i pasted it across.

Regards

Gavin

rjlv2 (author)Daoina2015-12-15

I think it should be #include <Servo.h>. This lets you use servo motors in arduino.

DarwynnM (author)2016-04-06

servoA.attach(9);

needs to be changed to

servoA.attach(5); if you are setting the servo on pin 5.

BakulS (author)2015-07-20

there is a jitter seen on the servo... can u kindly suggest the solution for the same

JasonR23 (author)BakulS2016-01-15

I had that problem twice in the past. Once was due to not enough power, check the datasheet on your servo. The other was due to me not having the servos and arduino on a common ground. Make sure everything is hooked to the same ground.

Kyyyyy (author)BakulS2016-01-01

Try a metal gear servo

JeremyR25 (author)2015-11-06

Hi, just started with arduinos as well. Got a slight problem. I'm just using one push button to make it turn back and fort. The problem is that, after I push the button and goes to position(0deg), the servo won't stop making noise. Then, after I push it again, it goes back to the other position(180), the servo then stop making that noise. What could be the problem?

SyukronE (author)2015-09-29

Belfastman1958 (author)2015-07-17

I'm a beginner at this and i'm using an Arduino MEGA and couldn't get it to work.

Is the code and connections the same for both boards?

It should work fine for the mega

gstorey (author)2014-12-19

Only just bought an arduino and im learning the basics. Great instructable. The small thing of moving a servo is great to see the potential for future projects.

peppypickle (author)2014-12-18

great Instructable, and thanks for posting that video, too!

gavinkeeley (author)peppypickle2014-12-18

Pleasure peppypickle