Difference between revisions of "The Use of Servo"
(typo) |
(typo) |
||
Line 138: | Line 138: | ||
} | } | ||
</source> | </source> | ||
− | * Select the | + | * Select the correct port from (Tools)→(Serial Port) in Arduino IDE after compiling and then upload the program. |
[[file:upload.JPG|500px|center]] | [[file:upload.JPG|500px|center]] | ||
*Open the serial monitor after uploading and enter any angle(0-180) in the serial input box, then click "Send". | *Open the serial monitor after uploading and enter any angle(0-180) in the serial input box, then click "Send". | ||
[[file:serial-.JPG|500px|center]] | [[file:serial-.JPG|500px|center]] | ||
− | *Result: You can precisely control rotation angle of the servo | + | *Result: You can precisely control the rotation angle of the servo using the serial port. |
===Program Debugging=== | ===Program Debugging=== |
Revision as of 19:48, 10 August 2016
ContentsOverviewUnlike ordinary motor steering gear that will only turn around, servo can rotate according to your instructions to any 0-180 DEG or stop. So it is also called the servo motor, mainly constituted by shell, circuit board, non-core motor, gear and position detector. PrincipleIt starts from the controller sending signal to the servo, getting through the circuit board chip to judge the direction of rotation, and then driving the non-core motor to rotate. The power is transmitted to the swing arm through the reduction gear while sending back signal by a position detector to judge whether it has already reached the location. The position detector is actually a variable resistance. When the servo rotates, the resistance values will be changed accordingly. By detecting the resistance values, you can know the angle of rotation. AttentionThe steering angle is within 0 -180 degrees. When the high level pulse is greater than 2.5ms, for a steering gear that has no self protection function, it'll usually cause the angle beyond the normal range. In that case, the internal DC motor will be in the stall state. In one or two minutes, it will cause the servo burning. When using it, please let it rotate among 45 degrees to 135 degrees as far as possible. Within the range, the steering angle is also more accurate. Servo Connector
SpecificationServo
Servo Connector
DevelopmentEquipment
Preparation
Experiment One: Drive the Servo
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created
#define servo_pin SDA
int pos = 0; // variable to store the servo position
void setup()
{
myservo.attach(servo_pin); // attaches the servo on pin SDA 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 >= 0; 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
}
}
Program Debugging
Experiment One: Drive the Servo
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created
#define servo_pin SDA
String inString = "";
int pos = 0; // variable to store the servo position
void setup()
{
Serial.begin(9600);
myservo.attach(servo_pin); // attaches the servo on pin 9 to the servo object
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
}
void loop()
{
while (Serial.available() > 0)
{
inString += char(Serial.read());
delay(2);
}
if (inString.length() > 0)
{
myservo.write(inString.toInt());
Serial.println(inString.toInt());
}
inString = "";
}
Program Debugging
while (Serial.available() > 0)
{
inString += char(Serial.read());
delay(2);
}
if (inString.length() > 0)
{
myservo.write(inString.toInt());
Serial.println(inString.toInt());
}
ApplicationVideo |