Introduction: Using Servos

Picture of Using Servos

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

Picture of 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

Picture of 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

Picture of 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

wire

Step 5: Using the Sensor With the Servo

Picture of 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

Picture of 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

Picture of 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.

Comments

IdireesS (author)2016-09-30

hello
I need a program for running servo motor for particular time ( more than 360 degree)
and return to its origin

kristopher168598 (author)2016-02-10

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.

SaberA3 (author)kristopher1685982016-07-24

Hello there
void loop() {

for (pos = 0; pos <= 90; 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(10); // waits 15ms for the servo to reach the position

}

delay(1000);

for (pos = 90; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees

myservo.write(pos); // tell servo to go to position in variable 'pos'

delay(10); // waits 15ms for the servo to reach the position

}

delay(1000);

}

That's what you want hope you understand it cause its way too simple good luck

TheMexicanChip (author)2015-12-22

i am new to using servos. How and what do i need to get it to move? All i have is the server

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.

( https://www.arduino.cc/en/Tutorial/Sweep)

tmercados (author)2015-12-07

great instructable.

can I use a 10k potentiometer in step 6?

MutatedShark3023 (author)2015-07-12

Hey Higgs Boson,

I was just stuck in a part about what is a shield, is it like a program or something

Thanks

frederico.dos.santos (author)2015-05-20

Running on Proteus.

frederico.dos.santos made it! (author)2015-05-20

I made it till step 3!

shill32 (author)2015-05-09

Nice, simple and good introduction. Thanks

MisterE1 (author)2015-04-07

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!

kdlaster (author)2015-01-31

I have to ask. What kind of awesomeness is that breadboard? It looks like built in features to make life much easier!

Higgs Boson (author)kdlaster2015-02-01

It came as a Radio Shack starter electronics kit I got years ago. It certainly has been very handy for prototyping.

Mawtees (author)2014-10-08

how do you pull of the female jack?

Higgs Boson (author)Mawtees2014-10-13

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.

Mawtees (author)2014-10-08

how do you pull of the female jack?

goyaam jaim (author)2014-08-23

hi , i want to make robot , can u help me to guide me how can i use it for back and forth movement

Ro_Usr (author)2014-08-20

will it work with a potentiometer?

peng.s.lim (author)2014-08-12

Hi, may i know for step 6, where do i put in the potentialmeter? Please advise me on it. Thanks!

omar elattal (author)2014-05-14

one more thing Can you help me how to control a one servo using a JOYSTICK [OR] KEYPAD

Higgs Boson (author)omar elattal2014-05-23

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.

omar elattal (author)2014-05-14

man you are so great THANK YOU i searched about this a lot :)

mushfiq1996 (author)2014-04-09

Help ASAP. Emergency Please. How i can rotate my servo to a specific angle for reading from amoisture sensor?

Higgs Boson (author)mushfiq19962014-04-09

The easiest thing would be to use a microcontroller.

Higgs Boson (author)Higgs Boson2014-04-09

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.

Paulg42 (author)2013-12-10

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

Liammc-c (author)2013-11-20

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?
any comments will be helpful- thanks

ataylor999 (author)2013-10-23

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?

Step by step instructions would be great! Thanks!

Higgs Boson (author)ataylor9992013-11-10

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.

infanati (author)2013-06-25

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?

Higgs Boson (author)infanati2013-06-25

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.

infanati (author)Higgs Boson2013-06-26

this is the code that im using for my servos, and I have also added a pic of my connections to the arduino.

#include // Include servo library

Servo servoLeft; // Declare left and right servos
Servo servoRight;

void setup() // Built-in initialization block
{
pinMode(10, INPUT); pinMode(9, OUTPUT); // Left IR LED & Receiver
pinMode(3, INPUT); pinMode(2, OUTPUT); // Right IR LED & Receiver

tone(4, 3000, 1000); // Play tone for 1 second
delay(1000); // Delay to finish tone

servoLeft.attach(13); // Attach left signal to pin 13
servoRight.attach(12); // Attach right signal to pin 12
}

void loop() // Main loop auto-repeats
{

int irLeft = irDetect(9, 10, 38000); // Check for object on left
int irRight = irDetect(2, 3, 38000); // Check for object on right

if((irLeft == 0) && (irRight == 0)) // If both sides detect
{
maneuver(-200, -200, 20); // Backward 20 milliseconds
}
else if(irLeft == 0) // If only left side detects
{
maneuver(200, -200, 20); // Right for 20 ms
}
else if(irRight == 0) // If only right side detects
{
maneuver(-200, 200, 20); // Left for 20 ms
}
else // Otherwise, no IR detects
{
maneuver(200, 200, 20); // Forward 20 ms
}
}

int irDetect(int irLedPin, int irReceiverPin, long frequency)
{
tone(irLedPin, frequency, 8); // IRLED 38 kHz for at least 1 ms
delay(1); // Wait 1 ms
int ir = digitalRead(irReceiverPin); // IR receiver -> ir variable
delay(1); // Down time before recheck
return ir; // Return 1 no detect, 0 detect
}

void maneuver(int speedLeft, int speedRight, int msTime)
{
// speedLeft, speedRight ranges: Backward Linear Stop Linear Forward
// -200 -100......0......100 200
servoLeft.writeMicroseconds(1500 + speedLeft); // Set left servo speed
servoRight.writeMicroseconds(1500 - speedRight); // Set right servo speed
if(msTime==-1) // if msTime = -1
{
servoLeft.detach(); // Stop servo signals
servoRight.detach();
}
delay(msTime); // Delay for msTime
}

Higgs Boson (author)infanati2013-07-02

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?

infanati (author)Higgs Boson2013-07-03

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.

Higgs Boson (author)infanati2013-07-03

Are you absolutely sure the light from the LEDs is reaching the phototransistors when reflected from objects?

infanati (author)Higgs Boson2013-07-04

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.

Higgs Boson (author)infanati2013-07-05

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.

Goh Tai Wei Glen (author)2013-07-01

Hi, I'm trying to make a obstacle avoiding robot(well, sort of) but having some issues with and was hoping you could help.
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).
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?

silentskorn (author)2013-06-26

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

Higgs Boson (author)silentskorn2013-06-27

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.

r_ray (author)2012-12-13

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?

Higgs Boson (author)r_ray2012-12-15

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.

ratankumarsaha (author)2011-12-31

which electrical thing contain this servo motor..how can find it??????????


RATAN BANGLADESH.....

Tariq802 (author)ratankumarsaha2012-03-19

try a local hobby store if not radio shack. any place that specializes in rc stuff...

smitlrx (author)ratankumarsaha2011-12-31

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.

Higgs Boson (author)smitlrx2011-12-31

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.

nilved (author)2012-01-28

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

A.C.E. (author)nilved2012-02-28

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 "myservo". If you wanted 3 servos, you would need three of those lines, but with different names, such as:

Servo myservo1;
Servo myservo2;
Servo myservo3;

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:

int potpin1 = 0;
int potpin2 = 1;
int potpin3 = 2;

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.

int val1;
int val2;
int val3;

the numbers will correspond with the servos of the same number.

now you just need to add the commands for checking all 3 servos into the loop, which would look like this:

void loop()
{
val1 = analogRead(potpin1); // reads the value of potentiometer 1
val1 = map(val1, 0, 1023, 0, 179); // scale it
myservo1.write(val1); // sets servo 1's position according to the scaled value
val2 = analogRead(potpin2); // reads the value of potentiometer 2
val2 = map(val2, 0, 1023, 0, 179); // scale it
myservo2.write(val2); // sets servo 2's position according to the scaled value
val3 = analogRead(potpin3); // reads the value of potentiometer 3
val3 = map(val3, 0, 1023, 0, 179); // scale it
myservo3.write(val3); // sets servo 3's position according to the scaled value
delay(15); // delays for 15ms to let the servos catch up
}

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

Tariq802 (author)A.C.E.2012-03-19

before you can start reading and writing you also need to attach the servo objects to their pins ie:

void setup() {
myservo1.attach(9); //attach servo 1 to pin 9
myservo2.attach(10); //attach servo 2 to pin 10
myservo3.attach(11); //attach servo 3 to pin 11
}

etc.

About This Instructable

297,516views

291favorites

License:

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 GeneratorHow to see alpha particlesSimple Jacob's Ladder
Add instructable to: