Week 8 – Lab Report – Program a servo using an Arduino Microcontroller and the sweep servo code from the Arduino website

Program a servo using an Arduino Microcontroller and the sweep servo code  from the Arduino website

Objective:

Assemble a servo environment using an Arduino Microcontroller, a servo, and open source code available through the Arduino website.

Materials:

Arduino microcontroller Duemilanove                      index

Hitec HS-422 Servo

hitec-hs-422

3 jumper cables

Ethernet Cable

Code from Arduino.cc

Diagram

Screen shot 2013-10-24 at 10.56.33 AM

Code

// Sweep
// by BARRAGAN <http://barraganstudio.com>
// This example code is in the public domain.

#include <Servo.h>

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 < 90; 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(100);                       // waits 15ms for the servo to reach the position
}
for(pos = -30; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees
{
myservo.write(pos);              // tell servo to go to position in variable ‘pos’
delay(100);                       // waits 15ms for the servo to reach the position
}
}

Method:
1.- Attach the ethernet cable to the usb port on your computer. 2.-Connect the jumper cables as per diagram, the yellow to pin 9, the black to ground, and the red to power. 3.- Open the Arduino environment, go to examples, scroll to servo tab, and choose the sweep example. 4.-Go to tools scroll to board and choose the Duemilanove board in this case, again go to tools, scrool to serial port, and choose the serial port. 5.- Now  you’re ready to test your hardware, start by uploading your code into the Arduino, and verify it, by clicking the check button, if that’s ok, click the upload button right of the verify. If that goes well the servo should start spinning.

Results:

The servo now should start spinning to one side and the to the other. The code has a loop so it will work continually as long as the loop proves right. you can tamper with the delay increasing or decreasing its millisecond values, that will speed the movement of the servo gear. You can also remove the for loops statement with another one and it will do something else. In every situation the servo will either change speed or direction. according to the values you change in the code.

 

 

 

This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply