Introduction: Controlling an RC Servo Motor With an Arduino and Two Momentary Switches
The name says it all. Controlling an RC car servo motor with an Arduino and some resistors, jumper wires, and two tactile switches. I made this the second day I got my Arduino, so I'm pretty proud of myself.
Step 1: Parts List
Okay, your going to need the following:
Arduino-$30-35 USD
Find out where to buy those here.
Jumper Wires-$8.50 USD
I got mine from Amazon
Resistors- Pennies a piece
Get em from Radio Shack, Digi-Key, Mouser, Jameco, etc.
Your goin to need two around 100 ohms (brown black brown) and two around 10k ohms(brown black orange). These don't have to be exact.
Servo Motor- $10 USD
Yes, I know this isn't the cheapest one on the internet. Tower Hobbies
Breadboard- $9-$30 USD, Depending on the size.
Amazon
Tactile Switch- $0.20 USD
Only 6,427 left on Digi-Key I just salvaged mine...
Step 2: The Circuit
The circuit is fairly simple. You should be able to throw it on a breadboard in five minutes like I did. Make sure it makes no sense to your less geeky family, and looks like a wad of something you pulled off a drain snake. Yum.
Step 3: The Program/Sketch
Here's my code that I used. I might explain it later, I'm kind of lazy. Thats what this and this are for.
#include <Servo.h>
Servo myservo;
int button7=0;
int button6=0;
int pos=90;
void setup()
{
pinMode(7, INPUT);
pinMode(6, INPUT);
myservo.attach(9);
}
void loop()
{
button7=digitalRead(7);
button6=digitalRead(6);
myservo.write(pos);
delay(5);
pos=constrain(pos,0,180);
if(button7==1 && button6==0)
{
pos++;
}
if(button7==0 && button6==1)
{
pos--;
}
}
Any bugs, glitches? I don't notice any...
Step 4: It Works(or Doesn't)! And, Coming Soon.....
It hopefully works for you, if it doesn't post a comment. We of the instructable community are usualy good at helping people. Hoping to add a video sometime soon. Might just post a video of an Arduino controlling a servo in another project, since I've moved on to bigger and better things. So have fun with this, modify it, heck go out and make money off of it and then tell me! That would just make my day.
25 Comments
8 years ago on Introduction
Hi can anyone help me use this with a NRF24L01? any help would be greatful
10 years ago on Introduction
Hello Geeklord - was wondering just off hand if you could possibly help me to utilize 2 joysticks with x, y, and z axis to control 2 servos. Each servo will work off one ps2 style joystick. Servos will be set to pan and tilt. Any thoughts or assistance would be much much appreciated.
Tony
12 years ago on Step 4
how do you keep it going in one direction ?
13 years ago on Step 3
The Constrain should either be directly before the write, or after the if.
13 years ago on Step 1
A jumper kit is nice, but you can also salvage them from different kabels.
13 years ago on Introduction
Looks cool!
I got an Duemilanove in my XMas stocking with a Servo Shield and have tried my darndest to load a rudimentary motor sketch, to no avail.
I've gotten error after error with the #include <Servo.h> and am wondering if you could shed some light?
I have several instances of the arduino folder (thinking that I might have a path issue). One on the root (c:/), one in my documents, and one wherever the unzipper defaults it to. I have tried different paths, and only get different errors, and none of them are very helpful. I see that you just have a plain "#include <Servo.h>", just like the instructions on the Arduino site said, but that not working for me. Any help would be appreciated.
I'm going to give myself an epileptic seziure if all I can do on my new toy is change the blink rate on an LED (which is all I've done). : (
Thanks!
Reply 13 years ago on Introduction
Hang in there... I'm not sure what you mean by Servo shield, do you mean the adafruit motor shield? Anyway, I don't know if you can type in the #include for the servo library, it seems like you should be able to, but what I did is Sketch>Import Library>Servo in the IDE. Also, have you looked the Servo library documentation? It explains a couple of the quirks with Arduino 0017 (but they make it better over all). when I made this I had the servo signal attached to pin 9, but they've changed the library since then, so now you can attach a servo on any pin. Here's the link -> http://arduino.cc/en/Reference/Servo .
sorry for the late reply...
Reply 13 years ago on Introduction
I actually figured this out a couple of days ago. You are right, I can't just type in the #include. It has to be imported from the sketch library as you mention. Crazy huh? Maybe that can be fixed on the next software upgrade. I think I'll put in a suggestion on their site.
I program for a living, and this just wasn't intuitive to me. All other programming languages I've used would have picked up on the special characters when doing an include, and would have "included" it on the fly.
Either way, thanks for your insight and response. It was spot on.
Reply 13 years ago on Introduction
Huh, I never knew you couldn't just type in the #include. Well, problem solved.
13 years ago on Introduction
Man, i was about to read through this instructable...but allsteps is gone, its too frustrating...
14 years ago on Introduction
So i suppose you got it to upload the code now ;)
Also, I found the best deal, From the arduino website so it is legit, its 28 Dollars, you save like 3-5 Dollars!
http://store.gravitech.us/ardu.html
That is where i am buying mine, Good i'ble Rated ;)
14 years ago on Introduction
i just bought an arduino Duemilanove and two servos, sooo pumped to try this!
Reply 14 years ago on Introduction
Cool, its a lot of fun. You know you only needed one right?
Reply 14 years ago on Introduction
oh i know, i'm working on a project and i needed two
Reply 14 years ago on Introduction
Cool
14 years ago on Introduction
does each servo go the opposite direction as the other one? like when one is goin left the other is goin right?
Reply 14 years ago on Introduction
aahhhh, there is only one servo. The two different pictures on the front are just different angles.
Reply 14 years ago on Introduction
ohhh... then How to i make what i explaned? :P
Reply 14 years ago on Introduction
aahhh, attach another servo motor to + and-, and attach its signal pin to digital 10. add something like Servo myservo2; to the beginning of the code, put myservo2.attach(10); and another variable for its position in the beginning too. Then do myservo2.write(whatever you named the other variable); in the loop. Then in the two if comands put a variablename++ or --; the opposite of whatever is being done to the variable pos. I might write the code and try that out later.
Reply 14 years ago on Introduction
the myservo2.attach(10) goes in void Setup() actually.