Servo Motor

Servo motors with 180 degree range

They are used for turning and positioning applications.

Servo.h library is used to override PWM signals with absolute angle

Value : 0-180, specifies the absolute position

Controlling speed:

By introducing delay

Sketch

#include <Servo.h> 

Servo myservo1, myservo2;  // 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() 
{ 
  myservo1.attach(9);  // attaches the servo on pin 9 to the servo object 
  myservo2.attach(10);
} 


void loop() 
{ 
// If you are using a normal 180-degree servo, try the following. 
// The code below sweeps the servo from 0 degrees to 180 degrees
// If you notice that your servo is "trashing" when it reaches the ends of its travel
// path, increase the low end by a little (say, make it 10 degrees) and decrease the 
// top end by a little (say, make it 170 degrees).
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    myservo1.write(pos);              // tell servo 1 to go to position in variable 'pos'
//    myservo2.write(pos);              // tell servo 2 to go to position in variable 'pos'
    delay(5);                       // waits 5ms for the servo to reach the position
  }
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
    myservo1.write(pos);              // tell servo 1 to go to position in variable 'pos'
//    myservo2.write(pos);              // tell servo 2 to go to position in variable 'pos'
    delay(5);                       // waits 5ms for the servo to reach the position
  }

}

Continous servo motor

It has full range and can keep rotating

Value passed to Servo object determines the speed and direction of rotation

Value = 90 (REST). Motor have to be calibrated for this resting position by adjusting the potentiometer in the motor

Value 90 - 180 : Speed of clockwise rotation

Value 0 - 90 : Speed of anti clockwise rotation

#include <Servo.h> 

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

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

void loop() 
{ 
// Uncomment the following lines, one at a time, to see the effect that each one
// has on the speed and direction of a continuous rotation servo.

//  myservo.write(90);    // At this position, the servo does not move. 90 degrees is the resting value
 myservo.write(100);   // At this position, the servo will move slowly clockwise
//  myservo.write(145);   // At this position, the servo will move faster clockwise
//  myservo.write(180);   // At this position, the servo will move at max speed clockwise
//  myservo.write(80);    // At this position, the servo will move at slowly anti-clockwise
//  myservo.write(70);    // At this position, the servo will move at faster anti-clockwise
//  myservo.write(0);     // At this position, the servo will move at max speed anti-clockwise
}