Introduction
A Servo Motor is a small device that has an output shaft. This shaft can be positioned to specific angular positions by sending the servo a coded signal. As long as the coded signal exists on the input line, the servo will maintain the angular position of the shaft. If the coded signal changes, the angular position of the shaft changes.
In this tutorial, you will learn how to use a servo , the circuit diagram, working of the servo etc. Here we will sweep the shaft of a servo motor back and forth across 180 degrees. You have to use the servo library to run the motor.
Items Required
- A servo motor
- Arduino Uno
- Connecting cable
- Jumpers
The Circuit
You have to make basically three connections only. First of all, connect the brown wire to the GND pin of the Uno. The red wire to the power pin, VCC. The orange wire is the signal wire which needs to be connected to a PWM pin (here D5). PWM stands for Pulse Width Modulation technique, it is used to convert the digital signal into an analog by varying the width of the Pulse. The PWM pins are used for giving the desired analog output. They are used to set the LED brightness or to run Stepper or other motor or anything which requires analog inputs.
Sketch
You would require the Servo library. Read more about libraries in Arduino. Furthermore, the shaft first moves from 0 degrees to 180 degrees and then back to 0 degrees, with a delay of 15 milliseconds after each degree.
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards
int pos = 0; // variable to store the servo position
void setup() {
myservo.attach(5); // attaches the servo on pin 5 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 >= 0; 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
}
}
There are a snap and a short video of the actual project. I hope you’ll be able to do it!