Lesson 4

Servos - Standard 180 Degree and Continuous 360 Degree Rotation

>> Jump to: Standard Rotation Servos
>> Jump to: Continuous Rotation Servos
https://www.arduino.cc/en/reference/servo

The servo library allows an Arduino board to control several servo motors.
Servos have integrated gears and a shaft that can be precisely controlled by sending values in several different methods.

Servo motors have three wires: power, ground, and signal. The power wire is typically red, and should be connected to the 5V pin on the Arduino board. The ground wire is typically black or brown and should be connected to a ground pin on the Arduino board.

Standard servos allow the shaft to be positioned at various angles, usually between 0 and 180 degrees.

Continuous rotation servos allow the rotation of the shaft to be set to various speeds by sending PWM signals (Pulse Width Modulation).

Ensure you attach the battery pack whenever using servos. They require sufficient power to prevent brownouts on the Arduino board which will cause undesired resetting.


In order to use a servo, you must "attach" it. This means plugging it in of course, but also in code.


First you will include the "standard" library already installed in the Arduino software.
#include <Servo.h>


Call it into existence.
Servo myservo;


Give it life in the Setup()
myservo.attach(9);


If you have two drive servos, you will need to declare both.
#include <Servo.h>

Servo myservoLeft;
Servo myservoRight;

setup() {
  myservoLeft.attach(9);
  myservoRight.attach(10);
}

void loop() {}

Standard Rotation ServosBack to Top



The following are standard parameter values to send commands to the servos:


0 Degrees is fully counter-clockwise
myservo.write(0);


45 Degrees is pointing middle-left
myservo.write(45);


90 Degrees is in the middle
myservo.write(90);


135 Degrees is pointing middle-right
myservo.write(135);


180 Degrees is fully clockwise
myservo.write(180);


Use the following code test a servo on pin 9. In theory, the servo should move to the 0, 90, 180 degree positions with a small pause between each move. Try changing the values yourself.
#include <Servo.h>

Servo myservo;

void setup()
{
  myservo.attach(9);
  myservo.write(0);
  delay(500);
  myservo.write(90);
  delay(500);
  myservo.write(180);
  delay(500);
  myservo.write(0);
  
}

void loop() {}

Continuous Rotation ServosBack to Top

An easy way to add mobility to your robot is by using servos that have no built in limitation. Continuous rotation servos will spin 360 degrees continuously. In most cases, there is three values you will use to tell the servo to turn clockwise, counter-clockwise or to stop.


The following are standard parameter values to send commands to the servos:

1000 Microseconds is fully counter-clockwise
myservo.writeMicroseconds(1000);


1500 Microseconds is in the middle (aka. the "stop" position)
myservo.writeMicroseconds(1500);


2000 Microseconds is fully clockwise
myservo.writeMicroseconds(2000);


When trying to make the servo fully stop, you will need to write 1500 Microsoconds to the servo. If the servo is still jittery, or moving slowly in one direction, you may need to adjust this value slightly. Example: 1510, 1520 or another value might be specific to your servo. Use the following code test a servo on pin 9. In theory, the servo should not turn, if it does, it should immediately stop after moving. If needed, keep changing the values and uploading the adjusted sketch.
#include <Servo.h>

Servo myservo;

void setup()
{
  myservo.attach(9);
  myservo.writeMicroseconds(1500);
}

void loop() {}



Note: Some manufactures do not follow the "1000, 1500, 2000" standard very closely so that servos often respond to values between 700 and 2300 in some cases. Feel free to increase these endpoints until the servo no longer continues to increase its range. Attempting to drive a servo past its endpoints (often indicated by a growling sound) is a high-current state, and should be avoided.

https://www.arduino.cc/en/Reference/ServoWriteMicroseconds