ls
4. Enter the command and press Enter to run the program:
sudo python3 move.py
5. After running the program successfully, you will observe that the Motor will rotate
for about 1 second and then stop, and the program will also stop. If you need the
motor to rotate again, you need to run the program again.
7.4.2 The main code program of this lesson
For the complete code, please refer to the file move.py.
1. import time
2. import RPi.GPIO as GPIO
3.
4. Motor_A_EN = 4
5. Motor_A_Pin1 = 26
6. Motor_A_Pin2 = 21
Import the dependent library, where Motor_EN = 4, Motor_Pin1 = 26, Motor_Pin2 =
21 are the parameters of the corresponding interface motorA.
1. def setup():#Motor initialization
2. global pwm_A
3. GPIO.setwarnings(False)
4. GPIO.setmode(GPIO.BCM)
5. GPIO.setup(Motor_A_EN, GPIO.OUT)
6. GPIO.setup(Motor_A_Pin1, GPIO.OUT)
7. GPIO.setup(Motor_A_Pin2, GPIO.OUT)
8.
9. motorStop()
10. try:
11. pwm_A = GPIO.PWM(Motor_A_EN, 1000)
12. except:
13. pass