177
Chapter 16 Stepping Motor
Python Code 16.1.1 SteppingMotor
First observe the project result, and then analyze the code.
1.
Use cd command to enter 16.1.1_SteppingMotor directory of Python code.
cd ~/Freenove_Ultimate_Starter_Kit_for_Raspberry_Pi/Code/Python_Code/16.1.1_ SteppingMotor
2.
Use python command to execute code "SteppingMotor.py".
python SteppingMotor.py
After the program is executed, the stepping motor will rotate 360° clockwise and then 360° anticlockwise,
circularly.
The following is the program code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
i m port
RPi
.
GPIO
a s
GPIO
i m port
time
motorPins
=
(
12
,
16
,
18
,
22
)
#define pins connected to four phase ABCD of stepper
motor
CCWStep
=
(
0x01
,
0x02
,
0x04
,
0x08
)
#define power supply order for coil for rotating
anticlockwise
CWStep
=
(
0x08
,
0x04
,
0x02
,
0x01
)
#define power supply order for coil for rotating
clockwise
d e f
setup
( ):
p r int
'Program is starting...'
GPIO
.
setmode
(
GPIO
.
BOARD
)
# Numbers GPIOs by physical location
f o r
pin
i n
motorPins
:
GPIO
.
setup
(
pin
,
GPIO
.
OUT
)
#as for four phase stepping motor, four steps are a cycle. the function is used to drive
the stepping motor clockwise or anticlockwise to take four steps
d e f
moveOnePeriod
(
direction
,
ms
):
f o r
j
i n
range
(
0
,
4
,
1
):
#cycle for power supply order
f o r
i
i n
range
(
0
,
4
,
1
):
#assign to each pin, a total of 4 pins
i f
(
direction
= =
1
) :
#power supply order clockwise
GPIO
.
output
(
motorPins
[
i
],((
CCWStep
[
j
]
= =
1
< <
i
)
a n d
GPIO
.
HIGH
o r
GPIO
.
LOW
))
e l se
:
#power supply order anticlockwise
GPIO
.
output
(
motorPins
[
i
],((
CWStep
[
j
]
= =
1
< <
i
)
a nd
GPIO
.
HIGH
o r
GPIO
.
LOW
))
i f
(
ms
<
3
):
#the delay can not be less than 3ms, otherwise it will exceed
speed limit of the motor
ms
=
3
time
.
sleep
(
ms
*
0.001
)
#continuous rotation function, the parameter steps specify the rotation cycles, every
four steps is a cycle
d e f
moveSteps
(
direction
,
ms
,
steps
) :
f o r
i
i n
range
(
steps
):
moveOnePeriod
(
direction
,
ms
)
#function used to stop rotating
d e f
motorStop
():
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 ...