149
Chapter 13 Motor & Driver
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
i m port
RPi
.
GPIO
a s
GPIO
i m port
smbus
i m port
time
address
=
0x48
bus
=
smbus
.
SMBus
(
1
)
cmd
=
0x40
# define the pin connected to L293D
motoRPin1
=
13
motoRPin2
=
11
enablePin
=
15
d e f
analogRead
(
chn
):
value
=
bus
.
read_byte_data
(
address
,
cmd
+
chn
)
r e turn
value
d e f
analogWrite
(
value
):
bus
.
write_byte_data
(
address
,
cmd
,
value
)
Summary of Contents for Ultimate Starter Kit
Page 1: ...Free your innovation Freenove is an open source electronics platform www freenove com ...
Page 117: ...117 Chapter 9 Potentiometer RGBLED www freenove com support freenove com Hardware connection ...
Page 155: ...155 Chapter 14 Relay Motor www freenove com support freenove com Hardware connection OFF 3 3V ...
Page 173: ...173 Chapter 16 Stepping Motor www freenove com support freenove com Hardware connection ...
Page 239: ...239 Chapter 22 Matrix Keypad www freenove com support freenove com Circuit Schematic diagram ...
Page 240: ...Chapter 22 Matrix Keypad 240 www freenove com support freenove com Hardware connection ...