Saturday 15 October 2011

CIRC-04: A Single Servo

Purpose:
To create a loop of a servo motor spinning 180 degrees, then resetting back to 0.

Equipment:

  • 1x Arduino Uno
  • 1x Breadboard
  • 5x Wires
  • 1x 3 Pin Header
  • 1x Mini Servo
Reference

Program Details:
In terms of new hardware, this program introduces the Pin Headers. This was seen in previous experiments, but was only used to attach the 'breadboard layout sheet' to the breadboard. But in this experiment, it is being used to connect the Servo Motor to the breadboard. It acts like a middleman, and though it isn't necessary, it does help connect two objects that have only holes, no pins. The main new piece of hardware being introduced here is the Servo Motor. A servo motor is a motor which has a lot of torque and precision. It also takes 3 inputs: voltage, data, and ground.

In terms of software, a new library is introduced. This library is called Servo.h and is imported like so: #include <Servo.h>. This also introduces the concept of creating objects, as it requires you to create an object of type Servo to control the methods in the class. A new method introduced here is Servo.attach; which designates which pin works with the servo. Also we use a method call Servo.write, which is used to tell the servo which degree to go to.

This is a very simple circuit to build. You can cut out the breadboard entirely by attaching the Pin 9 to data, 5V to Signal, and Ground to Ground. The circuit they ask you to do has a lot of middle-manning, and this way it is less cumbersome.

Time to Complete:
10 minutes to build, 5 minutes to code.

Results:
When we finished putting the circuit together, the servo was not spinning. This is because the Servo had tipped over, and was no longer attached to the bread 3 Pin Header. So we decided to hold the Servo in place, which then functioned as programmed.

Photo:


Tips:
The 3 pin header is not neccesary, you can just connect the Voltage, Data, and Ground directly to the servo. 

Further Work:
You could adjust how far the motor spins or how quickly it spins. You can adjust the degree by adjusting the for loop. Make pos < 90 as the condition to get 90 degrees of rotation. You can also adjust the speed by changing the delay time in to a smaller number for a faster rotation, or a bigger number for a slower rotation.

Program Modification
The program is the exact same as the one from the website:

Program

#include <Servo.h>;                      //Imports the library
Servo myservo ;                            //Creates an object of type Servo to control the new methodds

int pos = 0;                                 //Variable to control position

void setup()  {
    myservo.attach(9);                   //Pin 9 now controls the Servo
}

void loop() {
    for(pos = 0; 0 < 180 ; pos = pos+1)             //Resets to 0, then goes to 180 by increments of 1
    {
        myservo.write(pos);                                   //move to position pos
        delay(15);                                                 //Waits for servo to reach the positon
    }

     for(pos = 0; 0 < 180 ; pos = pos+1)             //Counts from 180 down to 0 by increments of 1
    {
        myservo.write(pos);                                   //move to position pos
        delay(15);                                                 //Waits for servo to reach the positon
    }      
}

No comments:

Post a Comment