Controlling a Servo with Arduino

Very simple basics of building a circuit to control a servo using Arduino and PWM

For this you will need:

Arduino Board – I’m using a Duemilanova ATMEGA328
Arduino Sketch software – I’m using version 0015
Arduino Servo Library found here save it to lib/targets/libraries if you don’t already have it
A Servo that requires no more than a 5V supply I’m using a Futuba S3113
A Potentiometer (A dimmer switch)
Breadboard & Connector wires
Previously used Arduino, at least to do basic blinking LED

So we’re going to assume that you’ve already used the sketch software and uploaded at least your first sketch to the Arduino board.

Controlling a Servo with Arduino

First lets start with just connecting a servo to the board.

I’ve used some connecting wires to connect between the servo pins to the ardunio board. The red wire is positive, the black negative/ground and the white is what relays the feedback/instructions.

The red wire goes to the +5v pin, the black goes to the GND pin on the Arduino and I’m going to put the white wire to pin 9 PWM.

Thats it! So now you just need to upload the code below to the board. You can use this example sketch also provided with Arduino

 

Arduino Servo Sketch

// Sweep
// by BARRAGAN

#include //include the servo libary

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

So you should see the servo move!

If you got this far lets now add in a potentiometer to control the servo, so when you turn the potentiometer it will turn the servo.

The potentiometer, it has 3 pins like the servo, the outside pins are for the supply (+/-) and the middle pin is for the control/ feedback.

First lets setup the breadboard for the circuit, all red wires are positive, black are negative and the 2 white wires are the feedback to the Arduino chip.

Here’s the final setup I did below:

Near the start of the circuit I’ll add the potentiometer and at the end of the cicuit I’ll put the servo.

 

For more detail: Controlling a Servo with Arduino


About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top