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

Step 1: 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

Connect the GND and 5V

Step 3: Connect the Servo

Red to 5V

Brown/Black to GND

Orange to pin 9

Step 4: Connect the Buttons

One of the buttons to DIGITAL 2

The other to DIGITAL 4

Step 5: 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 = 4;

int buttonState = 0;

int buttonState2 = 0;

Servo servoA;

int position = 0;

void setup() {


pinMode(buttonPin, INPUT);



void loop() {

buttonState = digitalRead(buttonPin);

buttonState2 = digitalRead(buttonPin2);

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




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





Step 7: Complete

<p>great tutorial, I will try expanding to more servos and more delicate and complex motions. thanks!</p>
<p>Seems the servo runs too fast, how to reduce it?</p>
<p>Thanks. Code works wery vell. I put another servo and potenciometer to set the speed of servos.</p>
<p>The code on this page will not verify. Get High was not declared in this scope. Can you help???</p>
<p>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 &quot;if&quot; sentence activasting a second servo in the same direction, but it did not work. none of the servos move. not even the first</p>
<p>If I try to use the code it's missing something with the #include part, can you help?</p>
<p>Hi Daoina</p><p>I have fixed the code.</p><p>Thanks very much for pointing out the error.</p><p>It seemed to be left out when i pasted it across.</p><p>Regards</p><p>Gavin</p>
<p>I think it should be #include &lt;Servo.h&gt;. This lets you use servo motors in arduino.</p>
<p>servoA.attach(9); </p><p>needs to be changed to </p><p>servoA.attach(5); if you are setting the servo on pin 5.</p>
<p>there is a jitter seen on the servo... can u kindly suggest the solution for the same</p>
<p>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.</p>
<p>Try a metal gear servo</p>
<p>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?</p>
<p>I'm a beginner at this and i'm using an Arduino MEGA and couldn't get it to work.</p><p>Is the code and connections the same for both boards?</p>
<p>It should work fine for the mega</p>
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.
<p>great Instructable, and thanks for posting that video, too! </p>
Pleasure peppypickle

About This Instructable




More by gavinkeeley:How to make your own Remote Control Battery Charger How to make your own Quadcopter Battery Servo Tutorial 
Add instructable to: