Index>Robot Kit>Alter All in One Kit for RPi>Lesson 4 How to Control 180° Servo
No related product information!

Lesson 4 How to Control 180° Servo

1134

    In this lesson, we will learn how to control 180° Servo.

4.1 Components used in this course


image.png



4.2 Introduction of 180° Servo 

What is a servo?

The servo is a position (angle) servo driver, which is suitable for those control systems that require constant angle changes and can be maintained. It has been widely used in high-end remote control toys, such as airplanes, submarine models, and remote control robots.

We use a 180° servo in this lesson, which can move between 0° and 180°. It is used in the Alter Dog quadruped robot product of Alter's polymorphic products. Since the 180° servo can use the PWM signal to control the rotation angle of a certain mechanism, it is a more commonly used module in robot products. The walking robot, robotic arm and pan/tilt are all driven by the it. It is used as a power unit for Alter Dog,too.

On the Raspberry Pi driver board Robot HAT, there is a PCA9685 chip specially used to control the servo. The Raspberry Pi uses I2C to communicate with the PCA9685. It controls the servo by sending pulse signals from the microcontroller. These pulses tell the servo mechanism of the servo where to move. The picture of the 180° servo is as follows:

image.png


4.3 Wiring diagram (Circuit diagram)

When the 180°Servo module is in use, it needs to be connected to the servo interface on the RobotHAT driver board. The yellow wire is connected to the yellow pin, the red wire is connected to the red pin, and the brown wire is connected to black pin,as shown below:

image.png



4.4 How to control 180°Servo 

4.4.1Run the program of this course

1. Open the terminal software MobaXterm:


image.png


2. Log in to your Raspberry Pi (the way to log in to Raspberry Pi has been introduced in Lesson 1):

image.png


3. Enter the Course Code folder, this folder stores the sample code program to control the robot, enter the following command:

cd adeept_alter/02CourseCode/01ComponentCode

image.png


4. Enter the command to display the contents of the current directory:

ls

image.png


5. The 03Servo180 folder stores the sample code of this course. Enter the command to enter this folder:

cd  03Servo180

image.png


6. Enter the command to display the contents of the current directory:

ls

image.png


7.When using the 180°Servo module, we need to install the Python dependency library needed to control the 180°Servo: Adafruit_PCA9685, enter the following command in the console of the command window:

sudo pip3 install adafruit-pca9685

image.png


8.180Servo_01.py is a python program.You can run this program on the Raspberry Pi by directly by typing the following commands:

sudo  python3  180Servo_01.py

image.png


9. After successfully running the program, you will observe that the servo will rotate regularly.

10. If you want to terminate the running program, you can press the shortcut key Ctrl+C on the keyboard.


    4.4.2 The main code program of this lesson

After the above hands-on practice, you already know how to use and run our course sample code program. Then you must be curious to know how our code program is programmed on the Raspberry Pi to control the 180° servo. Let's learn about the main code program together. Here we use Sublime IDE to view and edit the code program of this course. Please refer to the content of Lesson 2 for the specific method.

image.png


In the above code, set_pwm_freq(50) is used to set the frequency of PWM to 50Hz. This setting depends on the model of the servo. The servo used by our robot products needs to be controlled by a 50Hz PWM signal. If you use other servos, we need to refer to the specific servo documentation to set this value.

pwm.set_pwm(3, 0, 300) is used to control the rotation of a servo to a certain position. 3 is the port number of the servo, which corresponds to the number marked on the RobotHAT drive board.Pay attention to the occasion that do not insert the ground wire, VCC and signal wire in the reverse direction when connecting the servo with the driver board. Brown to black, red to red, and yellow to yellow; 0 is the deviation value for controlling the rotation of the servo, but our program does not use this function to correct the deviation (the reason for the error of the servo can be referred to 4.2 precautions for structural assembly); 300 is the PWM duty cycle value to be set. Depending on the servo, the servo angle represented by this value is also different. The PWM duty cycle range of the servo we use is about 100 to 560, which corresponds to a rotation range of about 0° to 180°.

The above code cannot control the rotation speed of the servo. If we want a servo to slowly reciprocate between two positions, we need to use the method of increasing or decreasing the variable to control it.


image.png


Using the above code can make the servo rotate slowly back and forth between 300 and 400, but this method of controlling the servo also has great drawbacks. When the program is executed to the slow motion part of the servo, it will be blocked, which will seriously affect the program. Therefore, a multi-threaded solution is provided in our robot product program to solve this problem.