How to Control the Rotation of Servo with Arduino?
1.Introduction
Servo is a kind of motor that can rotate very precisely. It has been widely used in toy cars, RC helicopters, airplanes, robots, etc. In this project, we will use the PLUS mainboard to control the rotation of the servo.
2.Components Required
|
|
|
---|---|---|
Keyestudio Plus Mainboard*1 |
Servo*1 |
USB Cable*1 |
3.Component Knowledge
Servo:
The servo is a kind of position servo driver, which is mainly composed of housing, circuit board, coreless motor, gear and position detector. The working principle is that the receiver or microcontroller sends a signal to the servo, which has an internal reference circuit that generates a reference signal with a period of 20ms and a width of 1.5ms, and compares the DC bias voltage with the voltage of the potentiometer to output voltage difference. The IC on the circuit board determines the direction of rotation, and then drives the coreless motor to start rotation and transmits the power to the swing arm through the reduction gear, while the position detector sends back a signal to determine whether it has reached the positioning. It is suitable for those control systems that require constant change of angle and can be maintained.
When the motor rotates at a certain speed, the potentiometer is driven by the cascade reduction gear to rotate so that the voltage difference is 0 and the motor stops rotating. The angle range of general servo rotation is 0 to 180 degrees.
The pulse period for controlling the servo is 20ms, the pulse width is 0.5ms to 2.5ms, and the corresponding position is -90° to +90°. The following is an example of a 180 degree servo.
Servo motors have many specifications, but they all have three connecting wires, which are brown, red, and orange (different brands may have different colors). The brown is GND, the red is the positive power supply, and the orange is the signal line.
4.Wiring Diagram
Servo |
Plus mainboard |
---|---|
Red Wire |
5V |
Brown Wire |
G |
Orange Wire |
9(S) |
5.Code
Note: The library files need to be installed in the code. If you have already added the Servo library files, ignore the process of adding the library files below.
Decompress the library files in the folder, that is, put the decompressed “Servo” folder into “Arduino libraries” under the compiler installation directory.
After successful placement, you need to restart the compiler, otherwise the compilation will not work.
e.g :C:\Program Files\Arduino\libraries
/*
Servo Rotation
http//www.keyestudio.com
*/
#include
Servo myservo;// define the name of the servo
void setup()
{
myservo.attach(9);// select the pin of the servo(9)
}
void loop()
{
myservo.write(0);// set the rotation angle of the motor
delay(500);
myservo.write(45);// set the rotation angle of the motor
delay(500);
myservo.write(90);// set the rotation angle of the motor
delay(500);
myservo.write(135);// set the rotation angle of the motor
delay(500);
myservo.write(180);// set the rotation angle of the motor
delay(500);
}
6. Result
After upload the code to the Plus Mainboard, wire up and power on, the servo will rotate 0°, 45°, 90°, 135°, and 180°.