There are many specifications of steering gears, but all steering gears have three external wires, which are distinguished by three colors: brown, red and orange. Due to different brands of steering gear, the colors will be different. The ground wire is brown, the power wire is extremely red and the signal wire is orange.
The rotation angle of the steering gear is realized by adjusting the duty ratio of PWM (pulse width modulation) signal. The period of standard PWM (pulse width modulation) signal is fixed at 20ms(50Hz). Theoretically, the pulse width distribution should be between 1ms and 2ms, but in fact, the pulse width can be between 0.5ms and 2.5ms, and the pulse width is 0 ~ 180 relative to the steering gear angle. It is worth noting that due to different brands of steering gear, the rotation angle of different brands of steering gear will be different for the same signal.
There are two ways to control the steering gear with Arduino. One is to generate square waves with different duty ratios through Arduino's common digital sensor interface, and simulate PWM signals to locate the steering gear. The other is to use Arduino's own servo function to directly control the steering gear. The advantage of this control method is that it can be programmed, but the disadvantage is that it can only control two steering gears, because the function of Arduino itself can only use the digital 9, 10 interface. Arduino's driving ability is limited, so when it is necessary to control the steering gear above 1, an external power supply is needed.
Method 1:
Connect the steering gear to the digital 9 interface.
Write a program to make the steering gear rotate to the position corresponding to the number of angles input by the user, and print and display the angles on the screen.
int servo pin = 9; //Define that the digital interface 9 is connected to the signal line of the servo steering gear.
Int myangle// defines the angle variable.
Int pulse width; //Define the pulse width variable
int val
Void servo pulse (int servo pin, int my angle)// defines a pulse function.
{
Pulse width = (myangle *11)+500; //Convert the angle to a pulse width value of 500-2480.
DigitalWrite (servo pin, high level); //Adjust the interface level of the steering gear to the highest level.
Delay microsecond (pulse width); //The number of microseconds of delay pulse width value
DigitalWrite (servo pin, low level); //Adjust the interface level of the steering gear to low.
Delay (20 pulse width/1000);
}
Invalid setting ()
{
PinMode(servopin, output); //Set the steering gear interface as the output interface.
serial . begin(9600); //Connect the serial port with baud rate of 9600.
serial . println(" servo = o _ seral _ simple ready ");
}
Void loop()// Converts a number from 0 to 9 into an angle from 0 to 180, and causes the LED to blink a corresponding number of times.
{
val = serial . read(); //Read the value of the serial port
if(val & gt; 0 ' & amp& ampval & lt='9')
{
val = val-' 0 '; //Convert the characteristic quantity into a numerical variable
val = val *( 180/9); //Convert numbers into angles
Serial.print ("mobile servo to");
Serial.print(val,DEC);
serial . println();
for(int I = 0; I & lt=50; I++) // Give the steering gear enough time to turn to the specified angle.
{
Servopin pulse (val); //Reference pulse function
}
}
}
Method 2
Firstly, the servo function of Arduino and its sentences are analyzed, and several common sentences of steering gear function are introduced.
1, attach —— Set the interface of the steering gear. Only the number 9 or 10 interface can be used.
2.write- a statement used to set the steering gear rotation angle, which can be set from 0 to 180.
3.read()- a statement used to read the angle of the steering gear, which can be understood as reading the last write () command.
The value.
4. Attachment ()-Judge whether the parameters of the steering gear have been sent to the interface where the steering gear is located.
5.detach()- separates the steering gear from its interface, which can be used as a PWM interface.
Note: The writing format of the above statement is "Steering gear variable name". Specific statement () ",for example: myservo.attach(9).
Just connect the steering gear to the digital 9 interface.
Reference source program b:
# include & ltServo.h & gt// defines the header file. One thing to note here is that you can click Sketch & gt Import Library & gt Servo to call the servo function, or directly enter # include.
Servo myservo// defines the variable name of the steering gear.
Invalid setting ()
{
my servo . attach(9); //Defines the interface of the steering gear (9, 10 can be used, but only two can be controlled).
}
Invalid loop ()
{
my servo . write(90); //Set the rotation angle of the steering gear.
}