Arduino Servo Control

Program Download:

Arduino Sketch
single_servo.ino

The Software
           There are two main portions of code that you'll see explained and in detail below:
    -Initialization
    -Servo Control


           The first part of the code shows you how to initialize the servo and all our variables, this is important if you want to use more than 1 servo you need to declare that. We'll be using the Arduino Servo library for all out control to make things as easy as possible.

Initialization

------------« Begin Code »------------
#include <Servo.h>
 
Servo myservo;  // create servo object to control a servo 
                // a maximum of eight servo objects can be created 
 
void setup() 
{ 
  myservo.attach(0);  // attaches the servo on pin 0 to the servo object 
  delay(5000);
} 
..
...
------------« End Code »------------

           The last portion of the code is where we actually tell the servo where to move using the write function of the Arduino's Servo library.

Main Loop

------------« Begin Code »------------
...
..
void loop() 
{

    //Move To 0 Degrees
    myservo.write(10);
    delay(5000);

    //Move To 45 Degrees
    myservo.write(45);
    delay(5000);
    
    //Move To 90 Degrees
    myservo.write(90);
    delay(5000);
    
    //Move To 135 Degrees
    myservo.write(135);
    delay(5000);    
    
    //Move To 180 Degrees
    myservo.write(170);
    delay(5000);    
    
    //Move To 135 Degrees
    myservo.write(135);
    delay(5000);

    //Move To 90 Degrees
    myservo.write(90);
    delay(5000);

    //Move To 45 Degrees
    myservo.write(45);
    delay(5000);    

} 

------------« End Code »------------

           So, hopefully the software seen above makes sense and is not too far over your head. The Arduino library makes it much easier to understand how to control a servo motor compared to other microcontroller platforms that don't have a dedicated Servo Control library, now let's test this system out!