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

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.

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:

arduino-servo-potentiometer-final

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

arduino-potentiometer

arduino-servo

The white servo wire still goes to digital pin 9 PWM and the potentiometer white wire goes to the Analog 0 pin

So if you have all that setup then we just need to use the example sketch below to get it working:

// Controlling a servo position using a potentiometer (variable resistor)
// by Michal Rinott

#include

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
}

This content is published under the Attribution-Noncommercial-Share Alike 3.0 Unported license.

Technorati Tags: , , , ,

  1. Arduino – Control a DC motor with TIP120, potentiometer and multiple power supplies
  2. Arduino + Processing – Make a Radar Screen – Part 3: Visualising the Data from Sharp Infrared Range Finder
  3. Arduino + Processing: Make a Radar Screen to Visualise Sensor Data from SRF-05 – Part 2: Visualising the Data
  4. Arduino – (Very) Basic motion tracking with 2 PIR sensors
  5. Arduino + Processing – Make a Radar Screen to Visualise Sensor Data from SRF-05 – Part 1: Setting up the Circuit and Outputting Values
  6. Arduino – Basic Theremin meets Processing!
  7. Arduino – Basic Persistance of Vision
  8. Arduino – motion triggered camera
  9. Arduino – Sonic range finder with SRF05
  10. Using Processing to Send Values using the Serial Port to Arduino