Servos are ideal for embedded electronics applications because they can move to a specific position accurately. Most servos can turn 180 degrees at maximum. Some even larger ones can turn to 360 degrees. They can be used in mobile platforms for detection devices such as cameras and detectors of smart vehicles, or in robotic joints.
COMPONENT LIST
HARDWARE
The servo has three leads. The color of the leads varies between servos but the red lead is always 5V and GND will either be black or brown. The other lead is the signal lead and this is usually orange or yellow. This signal lead is connected to digital pin 9.
fig 1 Moving a Servo Circuit
CODE
Sample code
// Project - Moving a Servo
#include <Servo.h> // Declare the Servo.h library
Servo myservo; // Create a servo object
int pos = 0; // Variable pos to store the servo position
void setup() {
myservo.attach(9);
// Attach the servo on pin 9 to the servo object
}
void loop() {
for(pos = 0; pos < 180; pos += 1){
// Rotate the servo from 0° to 180°, increasing by 1° each time
myservo.write(pos);
// Write the angle to the servo
delay(15);
// Delay for 15ms to allow the servo to reach the position
}
for(pos = 180; pos >= 1; pos -= 1) {
/* Rotate the servo back from 180° to 0°, decreasing by 1° each time */
myservo.write(pos);
// Write the angle to the servo
delay(15);
// Delay for 15ms to allow the servo to reach the position
}
}
After uploading the sketch, you will see the servo sweeping back and forth from 0 to 180 degrees.
CODE REVIEW
The sketch starts from inserting <Servo.h > library.
#include <Servo.h>
This library is already in Arduino IDE. Identify it by opening
Arduino-1.0.5/ libraries/ Servo/Servo.h.
Libraries are collections of new commands that have been packaged together to make it easy to include them in your sketches. Arduino comes with a handful of of useful libraries, such as the servo library used in this example that can be used to interface to more advanced devices.
We need to create a name in the code for the servo:
Servo myservo; // Create a servo object
There is another command in the setup() function.
myservo.attach(9);
Declaring functions in the servo library is a bit different from declaring other functions. We need to declare various functions in the library including declaring the servo object and defining the function. Just like in the library, you need to point out the object so that the program can identify it. The format of the library function is as below.
Don't miss the dot sign(".") in between the word "my servo" and "attach()". “myservo” is the servo object we named before.
And the function we invoke is:
attach(pin) assigns the pin. We can use any digital pin, except 0 and 1. In this project, we have chosen digital pin 9.
In the main program, there are 2 "for" statements. The first one starts from 0 then spins to 180 degrees in 1 degree increments. The second one starts from 180 degrees and goes back to 0 in 1 degree increments.
myservo.write(pos)
Just like the previous command, you have to declare a name for this command. The parameter of this function is an angle. The unit is degrees.
If you want to know more about the functions in the servo library, visit the Arduino website:
http://ardui-no.cc/en/reference/servo
or visit the DFRobot website:
www.dfrobot.com