Tutorial
Table Of Contents
- 1. Premise
- 2. Raspberry Pi System Installation and Developmen
- 3 Log In to The Raspberry Pi and Install The App
- 4 Assembly and Precautions
- 5 Controlling Robot via WEB App
- 6 Common Problems and Solutions(Q&A)
- 7 Set The Program to Start Automatically
- 8 Remote Operation of Raspberry Pi Via MobaXterm
- 9 How to Control WS2812 RGB LED
- 10 How to Control The Servo
- 11 How to Control DC Motor
- 12 Ultrasonic Module
- 13 Line Tracking
- 14 Make A Police Light or Breathing Light
- 15 Real-Time Video Transmission
- 16 Automatic Obstacle Avoidance
- 17 Why OpenCV Uses Multi-threading to Process Vide
- 18 OpenCV Learn to Use OpenCV
- 19 Using OpenCV to Realize Color Recognition and T
- 20 Machine Line Tracking Based on OpenCV
- 21 Create A WiFi Hotspot on The Raspberry Pi
- 22 Install GUI Dependent Item under Window
- 23 How to Use GUI
- 24 Control The WS2812 LED via GUI
- 25 Real-time Video Transmission Based on OpenCV
- 26 Use OpenCV to Process Video Frames on The PC
- 27 Enable UART
- 28 Control Your AWR with An Android Device
- Conclusion
64
10 How to Control The Servo
10.1 Control The Steering Gear to Rotate to A Certain Angle
● Since the servo can use the PWM signal to control the rotation angle of a mechanism, it is a more
commonly used module on robot products. Walking robots, robotic arms and gimbals are all driven by the
servo. In our Raspberry Pi The driver board Motor HAT has a dedicated PCA9685 chip for controlling the servo.
The Raspberry Pi uses I2C to communicate with the PCA9685. You only need to install the Raspberry Pi driver
board Motor HAT on the Raspberry Pi, and the Raspberry Pi will be connected to the PCA9685. No other wires
are required for connection.
● The Raspberry Pi uses Python code to control the steering gear, and requires third-party libraries
Adafruit_PCA9685, Adafruit-PCA9685Project address, if you run the installation script of the robot software,
you do not have to manually install it again, if you do not have the security of running the robotTo install the
script, use the following command to install Adafruit_PCA9685 for Python3 in the Raspberry Pi:
sudo pip3 install adafruit-pca9685
●After the installation, you can use the Python3 code in the Raspberry Pi to control the servo:
import Adafruit_PCA9685 # Import the library used to communicate with PCA9685
import time
pwm = Adafruit_PCA9685.PCA9685() # Instantiate the object used to control the PWM
pwm.set_pwm_freq(50) # Set the frequency of the PWM signal
while 1
:
# Make the servo connected to the No. 3 servo port on the Motor HAT drive board reciprocate
pwm.set_pwm(3, 0, 300)
time.sleep(1)
pwm.set_pwm(3, 0, 400)
time.sleep(1)
●In the above code, set_pwm_freq (50) is used to set the PWM frequency to 50Hz. This setting depends
on the model of the servo. The servo used by our robot product needs to be controlled by a 50Hz PWM signal.
If you use other The value of the servo needs to be set by referring to the specific servo documentation.