Introduction: How to Program a Servo With Arduino

Hi everyone and first of all I want to say that...

I have a YouTube channel!

And my first video is about programming servos(I think it's obvious). Well this is a very simple project for arduino beginners (anyway it's more difficult than LEDs projects) and I wanted to make a tutorial about it so that beginners know how to program a servo and more people get interested in arduino.

Step 1: Video!

First of all I recommend you to watch the video. Please remember to subscribe, I know it's a small channel but remember it's new and I know that with your help it will grow up really quickly.

Here's the link:

Step 2: Materials

You'll need:

-An Arduino board

-A servo

-3 jumper wires

-A-B USB cable

-Your computer with the arduino software

Step 3: Prepare the Servo

First of all take your servo and the jumper wires and connect them to it so that you can connect the servo to the arduino. I'm connecting wires of the same colour so that I won't get confused later. In my case(and in most sevos) the colour code of the wires is:

Yellow:signal

Red: for the 5 volts

Black/Brown:ground

Step 4: Connect the Servo to the Arduino Board

Now connect your servo to the arduino. Connect the yellow wire to a digital pin(in my case 3), then connect the red wire to the 5 volts and the black wire to the GND. Then take the USB cable and connect it to your computer.

Step 5: The Code

Here's the code. There's a small explanation of it in the video.

#include
int servoPin=3;
Servo Servo1;
void setup(){
Servo1.attach(servoPin);
}
void loop(){
Servo1.write(0);
delay(2000);
Servo1.write(45);
delay(1000);
Servo1.write(90);
delay(1000);
Servo1.write(180);
delay(1000);
}

Step 6: Program Your Own Servo!

Now you know how to program a servo!

Comments

author
Volthaus+Electronics+Laboratory made it!(author)2017-06-20

Thanks for your well written Instructable. Handy information.

author
AhmedL31 made it!(author)2017-04-10

good work but can i control the speed of the servo if yes how ??

author
harz6 made it!(author)2016-02-10

Good job bro... Do you know to how to control a 230v operating stepper motor with relay using arduino??

author
TheBetaRayBill made it!(author)2016-08-21

Well it depends on the motor, do you have the stepper model number?

author
fituro made it!(author)2016-02-21

Sorry but I don't know how to control it. And also thanks!

author
pyroblaster70 made it!(author)2016-02-04

well done brother! now i know

author
fituro made it!(author)2016-02-06

Thanks!

author
nax made it!(author)2016-02-02

Thanks!

I've had a servo ready to go for a week or so, hadn't gotten around to figuring out how to program it.

As you can see in the video, your instructions worked.

One
thing, though - the sketch you have for copying is a little garbled, so
it didn't compile the first time I tried. Copy/pasting into the
Instructables web form seems to mess up code. I couldn't get it to work
either.

But the code you have a picture of does work, and it
was easy to fix the file. I posted the fixed file in case anybody else
wants it. The code (unless I made a mistake) is exactly what is in your
picture.

Servo_Test.ino20160201_173807.mp4
author
fituro made it!(author)2016-02-06

Good job! I'm glad to see that the tutorial was useful for you. And also thanks for uploading the fixed code.

author
vandenbrande made it!(author)2016-02-02

Thats because it copys some unwanted stuff like (-) at the end of your code sometimes. I believe you need to save it as UTF-8 when saving it from notepad or any text editor. then you wont have the problem for example this is the end of the code

delay(50);

}

}

}- <--- and there you have your problem.

author
Heyup made it!(author)2016-02-01

Well done on your tutorial :) Just a bit of advice, Try to make the sound quality better, and also leave the music out! people would much rather listen to your clear explanation. Apart from that fituro, Well Done mate :) .. John ...

author
fituro made it!(author)2016-02-01

Thank you very much for your advice. I know the audio quality isn't as good as I would like to so I'm going to try to fix that in the upcoming tutorials. Thanks againg for your comment I really like to see the opinions to try to make it better the next time ;-)

author
fituro made it!(author)2016-02-01

*again

author
curiosity36 made it!(author)2016-02-01

Along the lines that heyup suggests, move away from the mic a bit for more clear verbal instruction and keep the music WAY in the back ground. You've done a fine job. Keep it up. There is still a need for the basics. There are new Arduinista being born every day.

author
fituro made it!(author)2016-02-01

Thank you too curiosity36

About This Instructable

7,196views

177favorites

License:

More by fituro:DIY The Flash T-shirt | FituroDIY Dotted Mug |FituroBlinking LED| Fituro
Add instructable to: