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
time
pwm = Adafruit_PCA9685.PCA9685()
pwm.set_pwm_freq(50)
while
1:# Make the servo connected to the No. 3 servo port on the Motor HAT drive board reciprocate
pwm.set_pwm(3, 0,
time.sleep(1)
pwm.set_pwm(3, 0,
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.
# Import the library used to communicate with PCA9685
# Instantiate the object used to control the PWM
# Set the frequency of the PWM signal
300)
400)
Need help?
Do you have a question about the AWR Adeept Wheeled Robot and is the answer not in the manual?