Servo Control with an Arduino Board

Bit of a change of pace from the normal graphic design stuff, but i’ve been working with electronics for a couple of months now and am currently working on an MSc project using servo motors to move nails in a pin board. This requires a servo motor to be controlled in 4 directions (I will be using 2 motors) to control a plate which moves pins in a certain direction..

ANYWAY, the point of posting this is that I was unable to find an adequate or complete solution on the web so thought I would provide mine including circuit diagrams.

Things you will need…

Below is a list of the cheap parts that I am using to do this project.

  • prototyping board
  • wire in three colours
  • 2x push buttons – im using ones with four legs
  • 2x 220ohm resistors
  • Arduino duemilanove board
  • usb cable
  • standard Servo Motor

Construction

this is constructed as follows

circuit

I am very aware this is an awful photograph and will create a circuit diagram sometime soon…

this is a basic circuit really, each button has its own signal pin on the arduino board, here I have used 2 and 3, and the servo motor has its signal (the blue wire) plugged into pin 9 (PWM)

each button has a live and earth cable, red and black respectively, and a resistor (omitting this will cause the arduino board to turn off and on repeatedly). The earth pin and the signal pin should be on the same side of the switch as otherwise this will cause an opposite signal, i.e. pushing the button means off rather than on.

Arduino Code

Below is the arduino Code that I wrote, this is an adapted sketch that comes with the arduino, it causes the servo to turn to the left and to the right depending on which button you press (obviously buttons can be substituted for other inputs..)

#include <Servo.h>

Servo myservo; // create servo object to control a servo

int pos = 0; // variable to store the servo position
const int buttonPin = 2; //sets pin 2 as button
const int buttonPin2 = 3; //sets pin 3 as button
const int ledPin = 13; // sets pin 13 as LED for testing purposes
int buttonState = 0; //sets button 1 as off
int buttonState2 = 0; // sets button 2 as off

void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
pinMode (buttonPin, INPUT); // sets button as input
pinMode (ledPin, OUTPUT); // sets led as output
myservo.write(90); // starts servo at 90 degrees
}

void loop()
{
buttonState = digitalRead(buttonPin);
buttonState2 = digitalRead(buttonPin2);

if (buttonState == HIGH)
{
digitalWrite(ledPin, HIGH);
for(pos = 90; pos < 110; pos += 1) // goes from 90 degrees to 110 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

}

 delay (1500);

 for(pos = 110; pos>=90; pos-=1) // goes from 110 degrees to 90 degrees
{
myservo.write(pos); // tell servo to go to position in variable ‘pos’
delay(15); // waits 15ms for the servo to reach the position
}
}
else {
digitalWrite(ledPin, LOW);
}

if (buttonState2 == HIGH)
{

digitalWrite(ledPin, HIGH);
for(pos = 90; pos >=70; pos -= 1) // goes from 90 degrees to 70 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
}
delay (1500);
for(pos = 70; pos <=90; pos+=1) // goes from 70 degrees to 90 degrees
{
myservo.write(pos); // tell servo to go to position in variable ‘pos’
delay(15); // waits 15ms for the servo to reach the position
}
}
else {
digitalWrite(ledPin, LOW);
}
}

Leave a Reply