Arduino Servo Motor Tutorial

A servomotor is a rotary actuator or linear actuator that allows for precise control of angular or linear position, velocity and acceleration.
Servomotors are used in applications such as robotics, CNC machinery or automated manufacturing.
This tutorial will show you how to use the principals of a servo motor with your Arduino board.
Background:
This code moves the shaft of a RC servo motor back and forth across 180 degrees.
This example makes use of the Arduino servo library.

Components:
1 Arduino Board
1 Servo Motor
Hookup wires

Directions:
1.Servo motors have three wires: power, ground, and signal. The power wire is typically red, and should be connected to the 5V pin of your Arduino board. The ground wire is typically black or brown and should be connected to a ground pin on the Arduino board. The signal pin is typically yellow, orange or white and should be connected to pin 7 on the board.
2. Input following code into your Arduino Sketch and compile.
3. Plug USB A/B Cable into your laptop and connect it to your Arduino.
4. Press the upload program to run the code. Notice how the Servo motor will move 0 to 180 degrees clockwise, pause, then move 180 degrees in the reverse direction.

Code:

// This code will move the servo back and forth from 0-180 degrees.
#include

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

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

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

void loop() {
for (var = 0; var <= 180; var += 1) { // goes from 0 degrees to 180 degrees // in steps of 1 degree servoobj.write(var); // tell servo to go to position in variable ‘var’ delay(15); // waits 15ms for the servo to reach the position } for (var = 180; var >= 0; var -= 1) { // goes from 180 degrees to 0 degrees
servoobj.write(var); // tell servo to go to position in variable ‘var’
delay(15); // waits 15ms for the servo to reach the position
}
}

corra02

Leave a Reply

Your email address will not be published.