Computers, Development Boards & Shields
Educational & Robotics Kits
Electronic Sensors
Motors & Controllers
Communication & Display
Electronic Components
Soldering & Lab Tools

Basic Servo/Microservo Calibration and operational code

Libraries

The SG90 Microservo and all of our other servos are standard Servo controllable using standard libraries available with the Arduino IDE. Just use the included library and examples for basic use.

Don’t forget to reference the wiring instructions for your particular servo since each servo may vary slightly on what pins are assigned to do.

Guides

An example of Basic Servo Control using the built in Arduino IDE Servo.h library.

// Sweep
// by BARRAGAN
// This example code is in the public domain.

#include

Servo myservo; // 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()
{
myservo.attach(9); // attaches the servo on pin 9 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>=1; 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
}
}

Each Servo will have a slightly different calibration, as such it is recommended that you use only 165 degrees of the total 180 available for use, but if you want to take advantage of the full range of motion you will have to calibrate each servo.
When your servo is over-driven (sent to a position that is beyond its max tolerance) it will vibrate as it tries to drive past its physical limits. This is bad. Over-driving your Servo can have various adverse effects ranging from physical to electrical damage.
By manually entering positions until your servo hits its overdrive limits you can tell exactly what positions to drive to for full 180 degree coverage without over-driving your Servo.

#include
Servo myservo;

int myservopin = 7;//change this to match the pin you are connecting to the control

void setup()
{
myservo.attach(myservopin);
Serial.begin(9600);
}

void loop()
{
Serial.println("Command me!");
myservo.write(takecommand());

}

int takecommand()
{
int mynum[3]={0,0,0};

for(int i = 0;i<3;i++) { while(Serial.available() == 0); //pause until data is recieved from computer<-- this line added byte inByte = Serial.read(); switch(inByte) { case '0': mynum[i]=0; break; case '1': mynum[i]=1; break; case '2': mynum[i]=2; break; case '3': mynum[i]=3; break; case '4': mynum[i]=4; break; case '5': mynum[i]=5; break; case '6': mynum[i]=6; break; case '7': mynum[i]=7; break; case '8': mynum[i]=8; break; case '9': mynum[i]=9; break; default: mynum[i]=0; break; } } return mynum[0]*100 + mynum[1]*10 + mynum[2]; }

No comments yet.

Leave a Reply