Python Code 13.1.1 Motor
First observe the project result, and then analyze the code.
1. Use cd command to enter 13.1.1_Motor directory of Python code.
cd ~/Freenove_Ultimate_Starter_Kit_for_Raspberry_Pi/Code/Python_Code/13.1.1_Motor
2. Use python command to execute python code “Motor.py”.
python Motor.py
After the program is executed, shift the potentiometer, then the rotation speed and direction of the motor
will change with it. And when the potentiometer is turned to midpoint position, the motor stops running.
When away from the middle position, the motor speed will increase. When to both ends, motor speed reach
to maximum. When the potentiometer is turned to different side of the middle position, the motor will run
with different direction. Meanwhile, the terminal will print out ADC value of the potentiometer, the motor
direction and the PWM duty cycle used to control motor speed.
The following is the code:
im port RPi. GPIO as GPIO
im port smbus
im port time
address = 0x48
bus=smbus.SMBus(1)
cmd=0x40
# define the pin connected to L293D
motoRPin1 = 13
motoRPin2 = 11
enablePin = 15
def analogRead(chn):
value = bus.read_byte_data(address,cmd+chn)
r eturn value
def analogWrite(value):
bus. write_byte_data(address,cmd,value)