Introduction: Using Servos With Arduino Made Easy !

Servomotors or simply servos are essential components of a robot which convert electrical energy into mechanical energy.They are widely used in the field of RC hobby and robotics. But unlike simple geared motors , these servos are made by integrating a geared motor with a position feedback circuit which allows us to control the rotation of the shaft precisely. To drive a servo , we need a controller which provides PWM signals to the servo which in turn determines the angle of rotation. Using an arduino to do this is pretty easy job, all you need is :

1. A servo ( To make your own servo , check this out : make your own servo ! )

2. Arduino ( and the default "knob.ino" sketch )

3. A bunch of jumper wires ( male to male )

4. A 10k potentiometer

5. A USB cable

Step 1: Prepare the Potentiometer

The potentiometer has three pins one is for the wiper and the other two have 10k resistance between them. The wiper should be connected to the analog 0 pin on the arduino , one of the remaining two pins should be connected to +5v and the other should be grounded. The potentiometer has been soldered on a protoboard to make it "arduino friendly" ( you can use a breadboard too ).

Based on how you rotate the the wiper arm , the analog values on the analog 0 pin will change and therefore the angle of rotation of the servo will also change.

Step 2: Upload the Program and Attach the Servo

I've made some minor changes in the program :

pin 15 ( analog 1 ) and pin 16 ( analog 2 ) are used as power source for the potentiometer board.

The arduino reads values from potentiometer between 0 and 1023

" val = analogRead(potpin); "

Then it converts or "maps" the values from 0-1023 to 0-179 ( in degrees )

" val = map(val , 0 , 1023 , 0 , 179 ); "

Finally , it writes these values to the servo.

CONNECTIONS :

1. Connect the potentiometer to the arduino ( as mentioned above )

2. Attach the yellow/orange signal pin of servo to pin 9 on arduino.

3. Connect the red wire to VIN pin* and the brown one to ground.

4. Take power from USB.

5. Upload the attached sketch to arduino.

***IF you are using external power source ( other than USB ) then first convert the voltage to 5v.

>> some important tips


Step 3: Troubleshooting and Tips

TIPS :

1. Do Not take +5v from any pin on arduino or from the +5v pin on arduino , I'm saying this because sometimes servos tend to draw high current from the source which can seriously damage the microcontroller. Instead use lithium batteries or any other batteries to power-up your servo ( USB power also works quite well ).

2. If your arduino keeps on resetting and the servo behaves abnormally , try adding capacitors to your power supply.

3. You can use optoisolators / optocouplers to electrically isolate the servo from microcontroller. This keeps the high current away from the microcontroller and stops the noises from damaging the controller.

At last , feel free to ask your questions

and if you find my instructable helpful , please do vote for me :)

Comments

author
TheCoffeeDude (author)2015-12-24

Love Arduino tutorials. Great one!

author
electro18 (author)TheCoffeeDude2016-02-18

Thanks ! :)

author
dtare (author)2015-09-16

What if i need to control multiple servos..with multiple potentiometers...what would be the code?

author
bocabearing (author)dtare2015-10-28

Hello. I got an instructable where I show how to control 3 servos with 3 potentiometers. Maybe it can help you. https://m.instructables.com/id/Simple-Robotic-Arm-Out-of-Cardboard-Pieces/

author
dtare (author)bocabearing2015-11-14

thanks dude :)

author
ibenkos (author)2014-10-17

I made it and made a door lock out of it :)

author
nitishdash2010 (author)ibenkos2014-12-30

how? can you share?

author
farsadheidari (author)2014-10-21

thank you very good

author
DCengineer (author)2014-08-15

How much noise do you get? I've tried working with servos before, and they tend to get a lot of noise from the PWM. This is partially due to servo quality, however, I think a small capacitor on the signal line can help. Unfortunately, I have no working servos at this time, I'll have to try building one like you said.

author
electro18 (author)DCengineer2014-08-15

While using these micro servos , I usually get a lot of noise followed by jittering. Adding a bunch of capacitors to the power pins turns out to be helpful but I've not tried adding them to the signal pin.

author
DCengineer (author)electro182014-08-15

I'm not sure, it may be the power pin after all. However, it makes more since to me to put it on the signal pin. Just be sure not to put a big one there, that will make the signal constant (as opposed to pulses). Either way, I would suggest researching before putting a cap on the signal pin. It could cause problems.

author
electro18 (author)DCengineer2014-08-15

I don't have an oscilloscope or any other device to monitor PWM pulses but I suspect that the power source is not capable of delivering steady current.

author
billbillt (author)electro182014-08-21

Why is that??...

author
electro18 (author)billbillt2014-09-28

Hey there, billbillt !

Sorry for the late reply but I didn't get your question.

author
DCengineer (author)DCengineer2014-08-15

It could also be your supply power. If you are powering servos via USB, I would suggest not doing it. Even if you system does turn off the USB port if you draw too much (which I have had happen), running motors via your computer could damage it. The Arduino is a fairly resilient device that runs on 5v power. Computers run on various amounts (the CPU I believe typically runs on 3.3v, and RAM can run on 2 or even 1v. Moral of the story: it is a lot cheaper to replace the ATmega chip on an Arduino than it is to replace the port board, RAM, or CPU of a computer (if the safeguards don't exist or fail).

Exception: you should be fine running off of a phone or tablet charger. Simple ones (read: non-Apple) basically just output 5v at 500mA on the outer two pins. The inner pins are for data transfer.

author
electro18 (author)DCengineer2014-08-15

Yeah , I was getting the same problem since then I'm using a 12V SLA battery to power up the servo.

author
szaffarano1 (author)2014-08-22

I used a Mophie to power my Adruino Uno, for 5 servos, and 2 LEDs, and it worked great!! There are pics and a video of the completed project on here, under the Animatronic Horus helmet build. I could have used some of your additional information sooner, but it will help make this costume more functional by halloween 2014!! Thanks!!

author
electro18 (author)szaffarano12014-08-22

Good luck for your project ! :)

author
szaffarano1 (author)electro182014-08-23

Thanks! It's coming along well! I plan to use a smaller board to solder the wires to and make the electronics more permanent for the helmet. Should be more, 'wearable' for halloween!

author
electro18 (author)szaffarano12014-08-23

Sounds awesome :) ! I can't wait to see it on instructables !

author
thereyno (author)electro182014-08-23

Yeah.. Really.. Good Luck

author
robobot3112 (author)2014-08-23

Nice. In make your own servo , i cannot access the file.

author
electro18 (author)robobot31122014-08-23

You mean , the archive ?

author
robobot3112 (author)electro182014-08-23

the custom servo library.

author
electro18 (author)robobot31122014-08-23

What message are you getting from the compiler ? Did you follow the steps to install the library files correctly ?

author
robobot3112 (author)electro182014-08-23

i cannot get it on Winrar , on which it is supposedly to come .

author
electro77 (author)2014-08-22

Yes, Servos draw high current from micro-controllers. It is advised not to power more than one servo motors from Arduino. If a separate power supply is used to power more than one servo make sure that all servos and the arduino are connected to the common ground (i.e. Same GND). Nice project. keep it up.

author
electro18 (author)electro772014-08-22

Thanks :) ! That's why I insisted on using optoisolators , then you wouldn't even require to connect all the servos to common ground.

author
iuha (author)2014-08-21

Very informative

author
electro18 (author)iuha2014-08-22

Thanks :)

author
nodcah (author)2014-08-19

Informative! I was wondering why this wasn't featured earlier! =D

author
electro18 (author)nodcah2014-08-20

:D Thanks !

About This Instructable

40,307views

571favorites

License:

Bio: Hey there ! I'm Tanay , a hobbyist interested in making robots and sharing stuff. I hope that my instructables help you in solving your problems ... More »
More by electro18:USB Chameleon Lamp ( Color replicating )Your Own Color Sensor using LEDsMeasure RPM - Optical Tachometer
Add instructable to: