Controlling a Servo motor.!!
Servo motor is a DC motor where its rotary motion can be controlled based on the signal voltage we supply to it. Servo motor uses PWM where varying the signal gives different angles on its arm.
A typical servo motor consist of three wires namely Vcc,Ground,Signal where Vcc and ground will be hooked up for powering the motor.
Note:
A servo may malfunction if it is not supplied with enough power source .For example if your Arduino is plugged in with your Computer's USB jack then there is a possibility that the USB cannot deliver the required current to the motor.Connecting a separate battery (5v)will solve this issue.
(malfunction can be : 1) No rotations 2) Servo doesnt obey the instructions delivered to it by Arduino 3) The arm of a 180 degree doesn't come back to original position once it has reached 180 degrees etc..)
There are two types of servos
1)180 degrees motion (0-5v delivers rotations based on 180 degrees)
2)360 degrees motion (0-5v delivers rotations based on 360 degrees)
Sketch (Example program for Arduino servo library)
#include <Servo.h>
Servo myservo; // create servo object to control a servo
Servo myservo1 // a maximum of eight servo objects can be created
int pos = 0; // variable to store the servo position
void setup()
{
myservo.attach(4); // attaches the servo on pin 4 to the servo object
myservo1.attach(5);
// myservo.write(pos); //edit the pos value to have a const turn
}
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 a 180 degree servo once it has reached 180 degree the servo automatically goes back to 0 pos
for(pos = 0; pos>=180; pos++) // goes from 180 degrees to 0 degrees
{
myservo1.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
Working of the Sketch
Two servo motor signal wires are wired to pin 4 and 5 of the Arduino and a command is given to the motor to move its arm from 0 to 180 degree on both the servo .Servos are differentiated based on the objects for the class servo.
so based on the position we provide the servo changes its angle.so the loop sweeps the angle from 0 to 180 and once the servo reaches 180 it again reaches back to initial state and again runes the code,
you want to make a turn to particular angle use the code
myservo.write(39);