67
11 How to Control DC Motor
●If the Raspbian image version you installed is Raspbian Full provided by the official website, you do not
need to install other dependent libraries. We only need to control the GPIO port of the Raspberry Pi for simple
high and low levels and PWM to control the L298N chip on Motor HAT, thus controlling the direction and speed
of the motor.
●When you use the Motor HAT driver board to connect the motor, because the current required by the
motor is relatively large, try not to use a USB cable to power the Raspberry Pi and the driver board. There is a
2pin Vin interface on the driver board, which can be used
import
time
import
RPi.GPIO
as
GPIO# Import the library used to control GPIO
GPIO.cleanup()
# Reset the high and low levels of the GPIO port
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
BCM encoding to define the GPIO port
'''
The following code defines the GPIO used to control the L298N chip. This definition is different for different Raspberry Pi
driver boards.
'''
Motor_A_EN
=
4
Motor_B_EN
=
17
Motor_A_Pin1 =
14
Motor_A_Pin2 =
15
Motor_B_Pin1 =
27
Motor_B_Pin2 =
18
def
motorStop():
# Stop motor rotation
GPIO.output(Motor_A_Pin1, GPIO.LOW)
GPIO.output(Motor_A_Pin2, GPIO.LOW)
GPIO.output(Motor_B_Pin1, GPIO.LOW)
GPIO.output(Motor_B_Pin2, GPIO.LOW)
GPIO.output(Motor_A_EN, GPIO.LOW)
GPIO.output(Motor_B_EN, GPIO.LOW)
# Ignore some insignificant errors
# There are three encoding methods for the GPIO port of the Raspberry Pi, we choose
Need help?
Do you have a question about the AWR Adeept Wheeled Robot and is the answer not in the manual?