In this instructable, I am going to show you what a servo motor is, how to use it, and ideas for starting projects using it. I used arduino to control my servo, I added how to use a 555 in some of the later steps.

Step 1: What Is a Servo Motor

If you are like me, then you knew very little about servo motors, and how to use them, so we should start from the beginning. A Servo motor uses pulse width modulation (pwm) from a microcontroller or a 555 timing IC (or something different I haven't heard about) to know what position to move its horn to. They can move both clockwise or counterclockwise thanks to an H bridge which is hardwired into them. Most Servos, unlike conventional electric motors do not move in continuous rotations. the standard servo moves anywhere between 0 and 180 degrees, which make them useful for animatronics and robotics. The servo has three wires coming out of it which usually ends in a female jack. the wire colors are black, which gets connected to ground, red which gets connected to the positive power supply, and white or yellow which gets connected to the output of the microcontroller or 555 IC, and receives the pwm. Okay now that you know the basics, lets get started

Step 2: Testing the Servo

the first thing that you should do is make sure your servo motor is working. because the servos wires end in a female header, you cannot plug it into the arduino (unless you have a shield. insert solid core wires into the headers, so you can attach it to the pins of the arduino (or anything else). When you downloaded your programming environment for arduino, it should have two examples for the servo. The one we are going to use first is called sweep. Go to the "open" icon next to save near the top of the window on the environment. click on it, and a list of files should come up. go down to the one that says servo, and put your mouse over it. two files should come out of it. one called "sweep" and one called "knob". click on the one called sweep, compile the code and upload it to your board. if everything is connected correctly, the servo should begin to go back and forth from 0 to 180 degrees. If you cannot find the code, copy this:

// Sweep
// by BARRAGAN <http://barraganstudio.com>
// This example code is in the public domain.

#include <Servo.h>

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

int pos = 0;    // variable to store the servo position

void setup()
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object

void loop()
  for(pos = 0; pos < 180; pos += 1)  // goes from 0 degrees to 180 degrees
  {                                  // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  for(pos = 180; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position

be sure to plug in the white wire to digital pin 9, the black wire to one of the ground pins on arduino, and the red wire to the 5v pin on the arduino board.


Step 3: New Code

Now that you know your servo works, you can begin to incorporate sensors into the mix. the first thing you should do is use a potentiometer. Use anything between 10 and 100k. keep the servo attached as it was using the sweep example. attach the top pin on the pot to 3.3v on the arduino. Connect the bottom pin to ground on the board. Connect the center of wiper pin of the pot to A0 (the first analog pin) on arduino. go to "open" on the IDE again. Go to servo and open "Knob". compile the code and upload it to your board.  When the program is running you will be able to control the position of the servo with a potentiometer. If you cannot find the code, copy this:

// Controlling a servo position using a potentiometer (variable resistor)
// by Michal Rinott <http://people.interaction-ivrea.it/m.rinott>

#include <Servo.h>

Servo myservo;  // create servo object to control a servo

int potpin = 0;  // analog pin used to connect the potentiometer
int val;    // variable to read the value from the analog pin

void setup()
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object

void loop()
  val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023)
  val = map(val, 0, 1023, 0, 179);     // scale it to use it with the servo (value between 0 and 180)
  myservo.write(val);                  // sets the servo position according to the scaled value
  delay(15);                           // waits for the servo to get there

Step 4: Prepare the Sensor

The sensor that I used that would probably work very well was the QRD1114 reflective sensor which has a discrete infrared LED, and phototransistor. to get the sensor to work, you must connect it in a very specific way. you could use other reflective sensor, but this one just seemed convenient, and I am using it in another project so they are readily available for me. 

to setup this sensor you will need:

one QRD-1114

one 10k resistor

one 68 or 100ohm resistor


Step 5: Using the Sensor With the Servo

Now that you have your sensor setup so that it will work for with arduino, you can replace the pot with it to make a servo that moves when something is in front of it and moves back when nothing is in front of it. use the same code that you did for the potentiometer. but this time instead of using the pot, connect pin one on the qrd to A0 on the arduino board. connect the two resistors to 3.3v, and connect pin 4 and 2 on the qrd to the ground on the board. run the program and when there is something about 1 cm from the sensor the motor will turn one way, and when the object moves away from the sensor the servo will move back. try adding a photo cell to make a light sensitive servo. using sensors with the servo, you can make robots, animatronics, and more. Have fun!

Step 6: Using a 555 and a Potentiometer

this is a simple circuit using a 555 to control the servo with a potentiometer. The circuit was originally meant as a tone generator, but with some simple modifications it became a servo tester. follow the schematic and it should work, but if you have any questions or problems, leave a comment and I will try to help you.'

to make this circuit you are going to need:
-one 555 timer
-one .1 µf ceramic disk capacitor
-one 1k resistor
-one 100 ohm resistor
-one 100k potentiometer
- a lot of jumper wires

Step 7: Using a 555 and Push Buttons

I like this 555 circuit more than the one that uses the pot. the servo acts less spastic, and is easier to control. When you hit one button the servo will go clock wise and when you hit the other button it will go counter clock wise.

for this circuit you are going to need:
-one 555 timer
-one .1 µf capacitor
-one 1k resistor
-two 100 ohm resistor
-one 33k resistor
-two push buttons

build the circuit from the previous step accept do not add the pot, add the push buttons and resistors in place of it.
hello<br>I need a program for running servo motor for particular time ( more than 360 degree)<br>and return to its origin<br>
<p>Hello, I kind of need help in making the motor move 90 degrees clockwise and counter clockwise, both taking 5 seconds, and when it goes down clockwise, I want it to delay for another 5 seconds. I was thinking if there was anyway you could help me.</p>
<p>Hello there <br>void loop() {</p><p> for (pos = 0; pos &lt;= 90; pos += 1) { // goes from 0 degrees to 180 degrees</p><p> // in steps of 1 degree</p><p> myservo.write(pos); // tell servo to go to position in variable 'pos'</p><p> delay(10); // waits 15ms for the servo to reach the position</p><p> }</p><p> delay(1000);</p><p> for (pos = 90; pos &gt;= 0; pos -= 1) { // goes from 180 degrees to 0 degrees</p><p> myservo.write(pos); // tell servo to go to position in variable 'pos'</p><p> delay(10); // waits 15ms for the servo to reach the position</p><p> }</p><p> delay(1000);</p><p>}</p><p>That's what you want hope you understand it cause its way too simple good luck </p>
<p>i am new to using servos. How and what do i need to get it to move? All i have is the server </p>
<p>Awesome! You will need an Arduino Uno, a clone retails at $5.00. In addition, you need jumper wires, which are fairly cheap. After you have all of this, you must download the Arduino IDE (free). Input code and Upload. </p><p>( https://www.arduino.cc/en/Tutorial/Sweep)</p>
great instructable.<br><br>can I use a 10k potentiometer in step 6?
<p>Hey Higgs Boson,</p><p>I was just stuck in a part about what is a shield, is it like a program or something</p><p>Thanks</p>
<p>Running on Proteus.</p>
<p>I made it till step 3!</p>
<p>Nice, simple and good introduction. Thanks</p>
<p>Hi, does the 555 push button version require an arduino? I couldn't work out if it used one or not. Thanks for the answer!</p>
I have to ask. What kind of awesomeness is that breadboard? It looks like built in features to make life much easier!
<p>It came as a Radio Shack starter electronics kit I got years ago. It certainly has been very handy for prototyping. </p>
<p>how do you pull of the female jack?</p>
<p>You don't. you can just insert jumper wires directly into it, but if you really don't want it you can cut it off and strip the wires.</p>
<p>how do you pull of the female jack?</p>
hi , i want to make robot , can u help me to guide me how can i use it for back and forth movement
<p>will it work with a potentiometer?</p>
<p>Hi, may i know for step 6, where do i put in the potentialmeter? Please advise me on it. Thanks!</p>
<p>one more thing Can you help me how to control a one servo using a JOYSTICK [OR] KEYPAD</p>
<p>I'm sure you can find code all over the internet for arduino, but you could modify the knob example for them since most joystick modules are nothing but two potentiometers. You would need to change how you map your values since the pots probably start centered for zero instead of at on extreme, but it should be pretty straight forward.</p>
<p>man you are so great THANK YOU i searched about this a lot :)</p>
<p>Help ASAP. Emergency Please. How i can rotate my servo to a specific angle for reading from amoisture sensor?</p>
<p>The easiest thing would be to use a microcontroller. </p>
<p>Using the arduino you could map the values from your sensor to 180 degrees and then use the Servo.write command to move your servo to an angle corresponding to the reading. that is pretty much what is done in the Knob example under servos in the arduino ide. You would just replace the pot with your new sensor and maybe rework some of the values to get the desired effect.</p>
Hi, Im Fairly new to electronics and I just cant get this circuit to work and I need it for a school project !! Does the potentiometer HAVE to be 100k ohm in order for it to work ? because I was using a 10k and the it was only when I attached the battery that it jolted a degree or two in one direction but that was it !!
Hi, i am currently completing my L3 extended diploma in mechanical and electrical engineering and i have to complete a project of my choice. I need to turn a Continuous Rotation servo without any push buttons, the circuit will need to turn on the servo after 4 hours have passed, any ideas for the circuit? <br>any comments will be helpful- thanks
Hey, I have a quick question. I have 3 servos that we have taken the casing and gears off of. we need to attach propellers to the motors and test that the servos are working. what is a quick and easy way to do this? <br> <br>Step by step instructions would be great! Thanks!
You could use any of the methods shown here. This should get the motor running, but with the Servo's potentiometer removed you may run into problems with direction control. If you just want forward and backwards control then leave the pot connected and just glue it in the 90 degree position. This way any signal past 90 degrees will result in one direction, and anything below whill result in the opposite direction.
Hi, I started on a project using IR sensors but for some reason my servos only keep moving forward even if the sensor detects an object ahead. I checked the code and it seems to be fine. the connections are according to the codes. so im not quite sure whats going on there. any ideas?
Try sending the values from the sensor to the serial read on the IDE. If there is a problem with your connections these values will likely be sporadic. Also, it would be good to see how it responds to objects near it. Although it would be intuitive that the value would increase when an object is near, it may decrease (depending on how it is connected). That would be a good starting point if you're sure it's not the code.
this is the code that im using for my servos, and I have also added a pic of my connections to the arduino. <br> <br>#include // Include servo library <br> <br>Servo servoLeft; // Declare left and right servos <br>Servo servoRight; <br> <br>void setup() // Built-in initialization block <br>{ <br> pinMode(10, INPUT); pinMode(9, OUTPUT); // Left IR LED &amp; Receiver <br> pinMode(3, INPUT); pinMode(2, OUTPUT); // Right IR LED &amp; Receiver <br> <br> tone(4, 3000, 1000); // Play tone for 1 second <br> delay(1000); // Delay to finish tone <br> <br> servoLeft.attach(13); // Attach left signal to pin 13 <br> servoRight.attach(12); // Attach right signal to pin 12 <br>} <br> <br>void loop() // Main loop auto-repeats <br>{ <br> <br> int irLeft = irDetect(9, 10, 38000); // Check for object on left <br> int irRight = irDetect(2, 3, 38000); // Check for object on right <br> <br> if((irLeft == 0) &amp;&amp; (irRight == 0)) // If both sides detect <br> { <br> maneuver(-200, -200, 20); // Backward 20 milliseconds <br> } <br> else if(irLeft == 0) // If only left side detects <br> { <br> maneuver(200, -200, 20); // Right for 20 ms <br> } <br> else if(irRight == 0) // If only right side detects <br> { <br> maneuver(-200, 200, 20); // Left for 20 ms <br> } <br> else // Otherwise, no IR detects <br> { <br> maneuver(200, 200, 20); // Forward 20 ms <br> } <br>} <br> <br>int irDetect(int irLedPin, int irReceiverPin, long frequency) <br>{ <br> tone(irLedPin, frequency, 8); // IRLED 38 kHz for at least 1 ms <br> delay(1); // Wait 1 ms <br> int ir = digitalRead(irReceiverPin); // IR receiver -&gt; ir variable <br> delay(1); // Down time before recheck <br> return ir; // Return 1 no detect, 0 detect <br>} <br> <br>void maneuver(int speedLeft, int speedRight, int msTime) <br>{ <br> // speedLeft, speedRight ranges: Backward Linear Stop Linear Forward <br> // -200 -100......0......100 200 <br> servoLeft.writeMicroseconds(1500 + speedLeft); // Set left servo speed <br> servoRight.writeMicroseconds(1500 - speedRight); // Set right servo speed <br> if(msTime==-1) // if msTime = -1 <br> { <br> servoLeft.detach(); // Stop servo signals <br> servoRight.detach(); <br> } <br> delay(msTime); // Delay for msTime <br>} <br>
It's interesting that you connected the LEDs to I/O pins instead of directly to a power rail. I/O pins can't always supply enough current to drive the LEDs at full brightness which could be causing your problem. You may try connecting them to power and ground rails directly (with a resistor of course). What sensor are you using? Is the photo transistor in the same package as the LED?
even with the LEDs connected directly to the power supply the servos still only turn one way. The phototransistors are separate from the LEDs. I have used black tape around the LEDs so that the light is only emitted forward and not directly at the PT.
Are you absolutely sure the light from the LEDs is reaching the phototransistors when reflected from objects?
i think so. I even used a camera to check if the LEDs were bright enough when hitting the object. I also did another test by places a white LED on the power line going to the PT and it was lighting up every time my hand came close. so I'm not sure why is it that it's not working with the Arduino. I even tried using another arduino but the problem continues.
Try connecting the emitter of the photo transistor to ground, and the collector to power through a 10k resistor. Connect the I/O pin between the resistor and and the collector pin. You may need to change it in the code so that it responds to obstacles on a low value instead of a high value.
Hi, I'm trying to make a obstacle avoiding robot(well, sort of) but having some issues with and was hoping you could help. <br>I've successfully made the robot avoid static obstacles moving through a standard course. However, I was wondering if there is a way to latch the angle of the servo so as to free my controller to use the sensors to sense the environment so it's able to react to the changes in the environment(moving obstacle). <br>All these should be happening while the robot is moving around the obstacles. Your help is greatly appreciated. Thank you.
I'm a little confused, if you could please clarify a little, that would help a lot. So you have a sensor mounted to a servo (I think). Do you want to get it to lock onto moving obstacles to detect them, or do you want to lock the angle at a certain point so that your sensor is free to take readings?
Hi, thank you for some simple instructions to help me understand servos. I am a complete novice when it comes to servos, arduinos and everything involved with them. I was wondering how possible it would be to connect 2 servos to one pot, switch, throttle, etc. Also if it is possible to have one servo go lets say 90 degrees (only for an example) clockwise and the other servo go 90 degrees counter clockwise at the same time and vise versa? Thanks
Using arduino that would be completely possible. You could maybe even pull it off with another more passive driver if you know what you are doing, but tweeking the code would be much easier.
Hello, I am very new to arduino and I am looking to use sensors to cause a servo motor to turn one way when something is close and the other way when something isn't just as your example above does. However, I am trying to get the sensor distance to be greater so that the approach of human interaction causes the motor to move, rather than the range only being 1cm. I was considering using an ultrasonic sensor rather than infrared, are you able to give me any advice? Any help would be hugely appreciated?
The range finder would be better suited, but you also may consider a PIR sensor, as they are easy to use, but are more of a motion sensor than a distance sensor. It really depends on what you want the end result to be as to which sensor you use.
which electrical thing contain this servo motor..how can find it??????????<br><br><br>RATAN BANGLADESH.....
try a local hobby store if not radio shack. any place that specializes in rc stuff...<br>
Unfortunately servo's are not used often in standard equipment. The are however used in remote controlled airplanes, helicopters, cars and boats to facilitate the operation of the vehicle, like steering and acceleration. One can buy the remotes and normally you get the receiver and a couple of servo's with. You can also buy all these units separately.
I was able to find mine at radio shack, so they are pretty common. you don't need to take anything apart to find them. if they are not at your local store, try amazon or ebay.
could you hook up the arduino with multiple servos and pots to control them and if so can you post a comment with the code
While Higgs is getting more servos, Ill try to help you understand a bit. When you create the servo object (Servo myservo;) this is creating one servo object with the name of &quot;myservo&quot;. If you wanted 3 servos, you would need three of those lines, but with different names, such as:<br><br>Servo myservo1;<br>Servo myservo2;<br>Servo myservo3;<br><br>you would also need more potentiometers, so youd need to add more potpins, and have the potentiometers hooked up to their respective pins, I think that would look like this:<br><br>int potpin1 = 0;<br>int potpin2 = 1;<br>int potpin3 = 2;<br><br>where potpin1 is what we are calling analog pin 0, but will control myservo1. You also need more variables to store the data from the potpins, so make those too.<br><br>int val1;<br>int val2;<br>int val3;<br><br>the numbers will correspond with the servos of the same number.<br><br>now you just need to add the commands for checking all 3 servos into the loop, which would look like this:<br><br>void loop()<br>{<br> val1 = analogRead(potpin1); // reads the value of potentiometer 1<br> val1 = map(val1, 0, 1023, 0, 179); // scale it<br> myservo1.write(val1); // sets servo 1's position according to the scaled value<br> val2 = analogRead(potpin2); // reads the value of potentiometer 2<br> val2 = map(val2, 0, 1023, 0, 179); // scale it<br> myservo2.write(val2); // sets servo 2's position according to the scaled value<br> val3 = analogRead(potpin3); // reads the value of potentiometer 3<br> val3 = map(val3, 0, 1023, 0, 179); // scale it<br> myservo3.write(val3); // sets servo 3's position according to the scaled value<br> delay(15); // delays for 15ms to let the servos catch up<br>}<br><br>I think this would work, but someone check over my work because I'm a complete noob at arduino coding. Someone with an arduino and 3 servos should write this and see if it works :p
before you can start reading and writing you also need to attach the servo objects to their pins ie:<br><br>void setup() {<br> myservo1.attach(9); //attach servo 1 to pin 9<br> myservo2.attach(10); //attach servo 2 to pin 10<br> myservo3.attach(11); //attach servo 3 to pin 11<br>}<br><br>etc.

About This Instructable




Bio: Science is my passion. I find myself constantly working on countless experiments, from low energy particle accelerators to good old simple electronics. I also like ... More »
More by Higgs Boson:350kv Van de Graaff Generator How to see alpha particles Simple Jacob's Ladder 
Add instructable to: