How to Install Arduino for Windows
How to Install Arduino for Mac OS X
Troubleshooting Your Arduino Electronics Project

How to Create Sweeping Movements with the Arduino

You can create sweeping movements on a servomotor with the Arduino. A servo motor sweeps from 0º to 179º and then back again, in a similar way to the movement of an old rotary clock.

The Sweep sketch

You need:

  • An Arduino Uno

  • A servo

  • Jump wires

The wiring for a servo is extremely simple because it comes with a neat, three-pin socket. To connect it to your Arduino, simply use jump wires between the Arduino pins and the servo sockets directly or use a set of header pins to connect the socket to your breadboard. The servo has a set of three sockets with wires connected to them, usually red, black, and white.

image0.jpg

All the calculations and readings to move the motor are done on the circuitry inside the servo itself, so all that is needed is power and a signal from the Arduino. Red is connected to 5V on the Arduino to power the motor and the circuitry inside it; black is connected to GND to ground the servo; and white is connected to pin 9 to control the servos movement.

The colors of these wires can vary, so always check the datasheet or any available documentation for your specific motor. Other common colors are red (5V), brown (GND), and yellow (signal).

image1.jpg

Complete the circuit as described and open the Sweep sketch by choosing File→Examples→Servo→Sweep. The Sweep sketch is as follows:

// 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 < 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
 }
}

After you have found the sketch, press the Compile button to check the code. The compiler should, as always, highlight any grammatical errors in red in the Message Area when they are discovered.

If the sketch compiles correctly, click Upload to upload the sketch to your board. When the sketch has finished uploading, your motor should start turning backward and forward through 180 degress, doing a dance on the table.

If nothing happens, you should double-check your wiring:

  • Make sure that you’re using pin 9 for the data (white/yellow) line.

  • Check that you have the other servo wires connected to the correct pins.

The Sweep sketch breakdown

At the start of this sketch, a library is included. This is the servo library and will help you to get a lot out of your servo with very little complex code.

#include <Servo.h>

The next line makes a servo object. The library knows how to use servos but needs you to give each one a name so that it can talk to each servo. In this case, the new Servo object is called myservo.

Using a name is similar to naming your variables; that is, they can be any name as long as they are consistent throughout your code and you don’t use any names that are reserved by the Arduino language, such as int or delay.

Servo myservo; // create servo object to control a servo
    // a maximum of eight servo objects can be created

The final line in the declarations is a variable to store the position of the servo.

int pos = 0; // variable to store the servo position

In setup, the only item to set is the pin number of the Arduino pin that is communicating with the servo. In this case, you are using pin 9, but it could be any PWM pin.

void setup()
{
 myservo.attach(9); // attaches the servo on pin 9 to the servo object
}

The loop performs two simple actions, and both are for loops. The first for loop gradually increases the pos variable from 0 to 180. Because of the library, you can write values in degrees rather than the normal 0 to 255 used for PWM control. With every loop, the value is increased by 1 and sent to the servo using a function specific to the servo library, <servoName>.write(<value>).

After the loop updates the value, a short delay of 15 milliseconds occurs while the servo reaches its new location. In contrast to other outputs, after a servo is updated it starts moving to its new position instead of needing to be constantly told.

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
 }

The second for loop does the same in the opposite direction, returning the servo to its start 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
 }
}

This is the most simple servo example, and it’s a good idea to test whether the servo is working correctly.

  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
How to Define the Setup for Your Keypad Entry System Arduino Project
Arduino Projects Schematic Symbols
What You Need for a Keypad Entry System Arduino Project
How to Detect Movement with the Arduino
How to Complete Your First Arduino Sketch
Advertisement

Inside Dummies.com