Servo Tutorial

72,057

78

34

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

Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

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 = 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

2 People Made This Project!

Recommendations

  • Indoor Lighting Contest

    Indoor Lighting Contest
  • Make It Fly Challenge

    Make It Fly Challenge
  • Growing Beyond Earth Maker Contest

    Growing Beyond Earth Maker Contest

34 Discussions

0
None
ElliottWest25

1 year ago

I tried to get the code to work, but i keep getting this error:

"error compiling for board Arduino/genuino uno."

so I need some help on how to fix this. thanks!

1 reply
0
None
Steve1062840ElliottWest25

Reply 4 weeks ago

Hi, this is a late reply, but if you have not found the solution here it is:
You should just change the "servo" in the #include to "Servo".
Good luck!

0
None
nkar9

Question 6 months ago

Hello, when I press the pushbutton, it doesn't move the servo. I have already checked my wiring and I checked my code. What do you think it could be?

0
None
lazaroracingl

1 year ago

Hi everyone, I have a servo motor project and I would like some help, I'll see if I can explain;
- 2 servos, - 2 puch buttom; I want to control one of the servants and the other servant is controlled by the first one through the angle
I'll give you an example (when pressing the buttom the servo walks from ¨0 to 50¨ degrees after reaching the 50 degrees the other walks from ¨0 to 30¨ degrees when reaching the 30 degrees
when I reach 30 degrees the first servo continues from where it stopped and goes from ¨50 to 99¨ degrees I thank the help

1 reply
0
None
gavinkeeleylazaroracingl

Reply 1 year ago

Hi lazaroracingl,

What you are describing should be easy enough to achieve with a series of if and else if statements.

Something like the following should work (I haven't tested this code),

if(buttonState == HIGH){

if(position1 < 50){ //first stage

position1++;

}else if(position2 < 30 && position1 == 50){ //second stage

position2++;

}else if(postion2 == 30 && position1 < 99){ //third stage

position1++;

}

}

servoOne.write(position1);

servoTwo.write(position2);

delay(5);

Obviously, you'll have to fill in the missing code parts.

Good luck,

Gavin.

0
None
UlisesC17

1 year ago

everything works excellent! Do you have any code to move 3 or more servos? Thank you!

1 reply
0
None
gavinkeeleyUlisesC17

Reply 1 year ago

Hi UlisesC17,

Sorry for the delayed response, I don't have access to three servos but I would imagine you just define more and more servos...

E.g.

Servo servoA;

Servo servoB;

...

Servo servoN;

Then later on in the code you will have to attach them to different pins

E.g

servoA.attach(9);

servoB.attach(10);

You will also have to declare variables to keep track of the individual positions etc.

Goodluck...

Regards

Gavin

0
None
AndreșC176

1 year ago

My servo keeps rotating back to a some kind of initial position..any ideas why?

0
None
ElliottWest25

1 year ago

figured it out, i had to capitalize the S at the beginning

0
None
ElliottWest25

1 year ago

also, here's the entire message.

Arduino: 1.8.5 (Windows Store 1.8.10.0) (Windows 10), Board: "Arduino/Genuino Uno"

C:\Users\smell\Documents\Arduino\sketch_oct25a\sketch_oct25a.ino:1:19: fatal error: servo.h: No such file or directory

#include <servo.h>

^

compilation terminated.

exit status 1

Error compiling for board Arduino/Genuino Uno.

This report would have more information with

"Show verbose output during compilation"

option enabled in File -> Preferences.

0
None
chupo_cro

1 year ago

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?

1 reply
0
None
gavinkeeleychupo_cro

Reply 1 year ago

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

0
None
AmmarD2

1 year ago

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

1 reply
0
None
gavinkeeleyAmmarD2

Reply 1 year ago

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

0
None
TeresoM

2 years ago

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

Kind regards!

1 reply
0
None
gavinkeeleyTeresoM

Reply 2 years ago

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

0
None
ghorgex

2 years ago

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

1 reply
0
None
gavinkeeleyghorgex

Reply 2 years ago

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

0
None
biddlejimmy

2 years ago

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

0
None
HectorM53

3 years ago

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