Today, we will use the Arduino development board to control a servo motor, creating a simple 2-degree-of-freedom gimbal. Based on its rotation characteristics, the gimbal can be divided into a horizontal gimbal that can only rotate left and right, and an omnidirectional gimbal that can rotate both left and right as well as up and down. Generally, the horizontal rotation angle is 0° to 350°, and the vertical rotation angle is +90°. The constant-speed gimbal has a horizontal rotation speed typically between 3° and 10°/s, while the vertical speed is around 4°/s. The variable-speed gimbal has a horizontal rotation speed ranging from 0° to 32°/s and a vertical rotation speed of 0° to 16°/s.
The working principle of the gimbal is to use the motion of the motor to achieve the corresponding actions of the platform. Today, we will use the SG90 9g servo to create a simple gimbal.
The SG90 is a commonly used small servo in Arduino designs. Its small size and low cost make it suitable for applications with lighter loads. Of course, the main reason for choosing it is its affordability. However, the SG90 servo has a rotation angle of only 180 degrees, so the simple gimbal cannot achieve 360-degree rotation.
Speaking of servos, let’s briefly introduce what a servo is. A servo is a position (angle) servo actuator, suitable for control systems that require continuously changing angles that can be maintained. The term ‘servo’ is a common name; its essence is a servo motor. Servos can be divided into analog servos and digital servos. Analog servos require continuous sending of destination PWM signals to rotate to the specified position. For example, if I want it to rotate 90 degrees, I need to continuously send the 90-degree PWM signal until it reaches the specified position to stop; digital servos only require one destination PWM signal to rotate to the specified position. For example, if I want it to rotate 90 degrees, I only need to send the 90-degree PWM signal once for it to rotate to 90 degrees.
The SG90 is an analog servo, so when using it, you need to continuously send position pulse signals to the servo. Although we can control the servo using Arduino by calling existing driver libraries, we will also provide a brief introduction to the working principle of servos.
Inside the servo, there is a reference voltage. The PWM signal generated by the microprocessor enters the servo through the signal line, creating a DC offset voltage, which is compared with the internal reference voltage of the servo to obtain the voltage difference output. The positive and negative voltage differences are output to the motor driver chip, determining the forward and reverse rotation. When the servo starts to rotate, the internal potentiometer is driven by a cascade of reduction gears to rotate until the voltage difference is zero, at which point the motor stops rotating.
After understanding the basic working principle of the servo, we can start writing the program.
Search for “servo” in the library management interface to find the servo library and install the first one. Next, we can use the library to drive the servo. As usual, we will first test with the example program.
There are two example programs: one adjusts the servo position based on analog input values, and the other moves the servo back and forth between 0 and 180 degrees. We will use the second example program for testing. Upload the program to the Arduino development board.
Now the servo drive has been implemented, and we can proceed to the operation part. For the operation part, we will use a 2-axis joystick module. This module has two axes of analog output and one digital output, allowing us to detect the joystick’s control direction through the analog output and the pressing control through the digital output.
Connect the two analog output ports of the module to the analog input ports of the Arduino, and then connect the servo. The wiring for the experiment is shown in the diagram below. The two servos are connected to pins 2 and 3 of the Arduino, and the joystick module’s analog outputs are connected to analog input pins 0 and 1 of the Arduino. This completes the hardware circuit setup.
After setting up the hardware circuit, we can start programming. The main idea of the programming is to use the X-axis value of the joystick to control the angle of one servo and the Y-axis value of the joystick to control the angle of the other servo. Therefore, the first step of programming is to detect the X and Y axis values of the joystick. Then adjust the rotation position of the servos based on the detected joystick data.
Here is the program.
Here is the operation video. The two servos have not been fixed, and this is just a simple demonstration.
Program source code download link:
Link: https://pan.baidu.com/s/1gNMDiJZSSNOiSApRCeDu4Q
Extraction code: rsqm
Leave a Comment
Your email address will not be published. Required fields are marked *