Controlling of stepper motor through Pi
The basic idea of using stepper motor here goes back to its variety of its usages in robotics applications. Coming straight to our project i.e. how to control a stepper motor using Raspberry Pi?
Broadly, the project is divided into two parts:
- interfacing GPIO pins with the stepper motor
- python script
Raspberry PI GPIO pinout
INTERFACING WITH PI
STEP1:
CONNECT IN A WITH PIN11 (GPIO 17)
STEP2:
CONNECT IN B WITH PIN12 (GPIO 18)
STEP3:
CONNECT IN C WITH PIN13(GPIO 21)
The motor connects to the controller board with a pre-supplied connector. The controller board has 4+2 pins that need to be connected to the Pi header (P1).
The P1-XX references above represent the Pi header pins I used. These are defined in the Python example below in the StepPins list so if you use different pins be sure to update the Python list as well. You can use other GPIO pins if required just remember to update your Python script.
Python script
Here is a copy of the script I used to rotate the stepper motor. It uses version 0.2.0 of the RPi.GPIO library and defines a 4-step and 8-step sequence.
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
| # Import required librariesimport timeimport RPi.GPIO as GPIO# Use BCM GPIO references# instead of physical pin numbersGPIO.setmode(GPIO.BCM)# Define GPIO signals to use# Pins 18,22,24,26# GPIO24,GPIO25,GPIO8,GPIO7StepPins = [24,25,8,7]# Set all pins as outputfor pin in StepPins: print "Setup pins" GPIO.setup(pin,GPIO.OUT) GPIO.output(pin, False)# Define some settingsStepCounter = 0WaitTime = 0.5# Define simple sequenceStepCount1 = 4Seq1 = []Seq1 = range(0, StepCount1)Seq1[0] = [1,0,0,0]Seq1[1] = [0,1,0,0]Seq1[2] = [0,0,1,0]Seq1[3] = [0,0,0,1]# Define advanced sequence# as shown in manufacturers datasheetStepCount2 = 8Seq2 = []Seq2 = range(0, StepCount2)Seq2[0] = [1,0,0,0]Seq2[1] = [1,1,0,0]Seq2[2] = [0,1,0,0]Seq2[3] = [0,1,1,0]Seq2[4] = [0,0,1,0]Seq2[5] = [0,0,1,1]Seq2[6] = [0,0,0,1]Seq2[7] = [1,0,0,1]# Choose a sequence to useSeq = Seq1StepCount = StepCount1# Start main loopwhile 1==1: for pin in range(0, 4): xpin = StepPins[pin] if Seq[StepCounter][pin]!=0: print " Step %i Enable %i" %(StepCounter,xpin) GPIO.output(xpin, True) else: GPIO.output(xpin, False) StepCounter += 1 # If we reach the end of the sequence # start again if (StepCounter==StepCount): StepCounter = 0 if (StepCounter<0): StepCounter = StepCount # Wait before moving on time.sleep(WaitTime) |
In this example the wait time is set to 0.5 seconds. To make the motor turn faster you can reduce this value. I found I could reduce it to 0.004 before the motor stopped working. If the script runs too fast the motor controller can’t keep up. This performance may vary depending on your motor and its controller.
The 4 step sequence is faster but the torque is lower. It’s easy to stop the rotation by holding the motor spindle. The 8 step sequence is slower but the torque is much higher. For my turntable application I prefer the torque over speed so I will be using the 8 step sequence.





No comments:
Post a Comment