how_to_use_a_stepper_motor
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
how_to_use_a_stepper_motor [2017/01/31 20:49] – [The Stepper Motor] walkeradmin | how_to_use_a_stepper_motor [2017/02/04 21:57] – [Python Code Examination] walkeradmin | ||
---|---|---|---|
Line 4: | Line 4: | ||
\\ | \\ | ||
{{: | {{: | ||
- | \\ | ||
- | \\ | ||
- | <color red>This is work in progress, when this warning disappears, my work here is done : | ||
\\ | \\ | ||
\\ | \\ | ||
Line 27: | Line 24: | ||
==== The Stepper Motor ==== | ==== The Stepper Motor ==== | ||
\\ | \\ | ||
- | A stepper motor contains several coils (depending on the type) that can be ' | + | A stepper motor contains several coils (depending on the type) that can be ' |
\\ | \\ | ||
\\ | \\ | ||
Line 54: | Line 51: | ||
\\ | \\ | ||
\\ | \\ | ||
- | {{: | + | {{: |
\\ | \\ | ||
\\ | \\ | ||
Line 68: | Line 65: | ||
\\ | \\ | ||
\\ | \\ | ||
- | {{: | + | {{: |
- | {{: | + | {{: |
\\ | \\ | ||
\\ | \\ | ||
Line 157: | Line 154: | ||
Firstly, here is the code, the breakdown of the code is further down this page. | Firstly, here is the code, the breakdown of the code is further down this page. | ||
\\ | \\ | ||
- | <file> | + | <sxh [py][; options for SyntaxHighlighter]> |
# | # | ||
+ | # Written in Python 2.7 | ||
+ | |||
+ | |||
# Import required libraries | # Import required libraries | ||
import sys | import sys | ||
Line 226: | Line 226: | ||
# Wait before moving on | # Wait before moving on | ||
time.sleep(WaitTime) | time.sleep(WaitTime) | ||
- | </file> | + | </sxh> |
\\ | \\ | ||
\\ | \\ | ||
Line 238: | Line 238: | ||
The first part is where we import the libraries to allow python to use the GPIO (rpi.gpio), the time functions (sleep) and system functions (sys) | The first part is where we import the libraries to allow python to use the GPIO (rpi.gpio), the time functions (sleep) and system functions (sys) | ||
\\ | \\ | ||
- | <file> | + | <sxh [py][; options for SyntaxHighlighter]> |
import sys | import sys | ||
import time | import time | ||
Line 246: | Line 246: | ||
The next line tells python that we are referring to the GPIO in terms of its GPIO numbering (BCM) not the physical pin numbers (BOARD) | The next line tells python that we are referring to the GPIO in terms of its GPIO numbering (BCM) not the physical pin numbers (BOARD) | ||
\\ | \\ | ||
- | <file> | + | <sxh [py][; options for SyntaxHighlighter]> |
GPIO.setmode(GPIO.BCM) | GPIO.setmode(GPIO.BCM) | ||
</ | </ | ||
Line 252: | Line 252: | ||
Next we need to define which GPIO connections we are using (the GPIOxx numbers). This project requires four GPIO connections, | Next we need to define which GPIO connections we are using (the GPIOxx numbers). This project requires four GPIO connections, | ||
\\ | \\ | ||
- | <file> | + | <sxh [py][; options for SyntaxHighlighter]> |
StepPins = [6, | StepPins = [6, | ||
- | </file> | + | </sxh> |
The numbers 6,13,19 & 26 refer to GPIO6, GPIO13, GPIO19 and GPIO26. | The numbers 6,13,19 & 26 refer to GPIO6, GPIO13, GPIO19 and GPIO26. | ||
\\ | \\ | ||
Line 260: | Line 260: | ||
Now we have to configure the GPIO pin modes. | Now we have to configure the GPIO pin modes. | ||
\\ | \\ | ||
- | <file> | + | <sxh [py][; options for SyntaxHighlighter]> |
for pin in StepPins: | for pin in StepPins: | ||
print "Setup pins" | print "Setup pins" | ||
GPIO.setup(pin, | GPIO.setup(pin, | ||
GPIO.output(pin, | GPIO.output(pin, | ||
- | </file> | + | </sxh> |
We start a loop, and step through all the values in the previous section <color red> | We start a loop, and step through all the values in the previous section <color red> | ||
\\ | \\ | ||
Line 271: | Line 271: | ||
Now we define a List that contains a list of values. | Now we define a List that contains a list of values. | ||
\\ | \\ | ||
- | <file> | + | <sxh [py][; options for SyntaxHighlighter]> |
Seq = [[1,0,0,0], | Seq = [[1,0,0,0], | ||
| | ||
Line 280: | Line 280: | ||
| | ||
| | ||
- | </file> | + | </sxh> |
There are four binary values, these correspond to the GPIO outputs. The list is used as an order to turn the GPIO outputs on and off at the correct time (in the correct sequence). If this sequence is incorrect, or the stepper motor is wired incorrectly, | There are four binary values, these correspond to the GPIO outputs. The list is used as an order to turn the GPIO outputs on and off at the correct time (in the correct sequence). If this sequence is incorrect, or the stepper motor is wired incorrectly, | ||
\\ | \\ | ||
Line 286: | Line 286: | ||
The next line defines a counter, the length of the counter is set to the number of entries in the dictionary called seq. In this case it is 8. | The next line defines a counter, the length of the counter is set to the number of entries in the dictionary called seq. In this case it is 8. | ||
\\ | \\ | ||
- | <file> | + | <sxh [py][; options for SyntaxHighlighter]> |
StepCount = len(Seq) | StepCount = len(Seq) | ||
- | </file> | + | </sxh> |
\\ | \\ | ||
The next line sets the direction. | The next line sets the direction. | ||
\\ | \\ | ||
- | <file> | + | <sxh [py][; options for SyntaxHighlighter]> |
StepDir = 1 # Set to 1 or 2 for clockwise | StepDir = 1 # Set to 1 or 2 for clockwise | ||
# Set to -1 or -2 for anti-clockwise | # Set to -1 or -2 for anti-clockwise | ||
- | </file> | + | </sxh> |
Use +1 for Anti-Clockwise and -1 for Clockwise | Use +1 for Anti-Clockwise and -1 for Clockwise | ||
\\ | \\ | ||
Line 301: | Line 301: | ||
The next section prompts the user for a numerical input. | The next section prompts the user for a numerical input. | ||
\\ | \\ | ||
- | <file> | + | <sxh [py][; options for SyntaxHighlighter]> |
if len(sys.argv)> | if len(sys.argv)> | ||
WaitTime = int(sys.argv[1])/ | WaitTime = int(sys.argv[1])/ | ||
else: | else: | ||
WaitTime = 10/ | WaitTime = 10/ | ||
- | </file> | + | </sxh> |
This section then takes that input and if it is greater than 1 divides it by 1000 (so 2 would = 0.002). If it is not >1 then it is just calculated to 0.001. If you enter 0 here, the WaitTime will be zero, and the Stepper Motor rotor will not turn because the coils are changing state too fast. | This section then takes that input and if it is greater than 1 divides it by 1000 (so 2 would = 0.002). If it is not >1 then it is just calculated to 0.001. If you enter 0 here, the WaitTime will be zero, and the Stepper Motor rotor will not turn because the coils are changing state too fast. | ||
\\ | \\ | ||
\\ | \\ | ||
This line simply ensure that the variable StepCounter starts at zero. | This line simply ensure that the variable StepCounter starts at zero. | ||
- | <file> | + | <sxh [py][; options for SyntaxHighlighter]> |
StepCounter = 0 | StepCounter = 0 | ||
- | </file> | + | </sxh> |
\\ | \\ | ||
Start a loop | Start a loop | ||
\\ | \\ | ||
- | <file> | + | <sxh [py][; options for SyntaxHighlighter]> |
while True: | while True: | ||
- | </file> | + | </sxh> |
While True simple means loop unconditionally forever. | While True simple means loop unconditionally forever. | ||
\\ | \\ | ||
Line 325: | Line 325: | ||
These next two lines are commented out, they print to the screen the values of the StepCounter and the Seq. You can uncomment these if you are having issues, as they are useful for debugging. | These next two lines are commented out, they print to the screen the values of the StepCounter and the Seq. You can uncomment these if you are having issues, as they are useful for debugging. | ||
\\ | \\ | ||
- | <file> | + | <sxh [py][; options for SyntaxHighlighter]># print StepCounter, |
- | # print StepCounter, | + | |
# print Seq[StepCounter] | # print Seq[StepCounter] | ||
- | </file> | + | </sxh> |
When you uncomment these lines, you will notice that the rotor will run a lot slower, as this uses way more CPU time (well on the Pi Zero that is the effect anyway). | When you uncomment these lines, you will notice that the rotor will run a lot slower, as this uses way more CPU time (well on the Pi Zero that is the effect anyway). | ||
\\ | \\ | ||
\\ | \\ | ||
The next section is a for loop that goes through each list [1,0,0,1] and sets each GPIO Pin [6, | The next section is a for loop that goes through each list [1,0,0,1] and sets each GPIO Pin [6, | ||
- | <file> | + | <sxh [py][; options for SyntaxHighlighter]> |
for pin in range(0,4): | for pin in range(0,4): | ||
xpin=StepPins[pin]# | xpin=StepPins[pin]# | ||
Line 341: | Line 340: | ||
else: | else: | ||
GPIO.output(xpin, | GPIO.output(xpin, | ||
- | </file> | + | </sxh> |
For fault finding there is an embedded print statement that will output the Seq to the screen (so [1,0,0,1] for example). Uncomment this for fault finding. As previously mentioned, this may impact your Stepper Motor rotor speed. | For fault finding there is an embedded print statement that will output the Seq to the screen (so [1,0,0,1] for example). Uncomment this for fault finding. As previously mentioned, this may impact your Stepper Motor rotor speed. | ||
\\ | \\ | ||
Line 347: | Line 346: | ||
This next line does quite a lot in real terms. | This next line does quite a lot in real terms. | ||
\\ | \\ | ||
- | <file> | + | <sxh [py][; options for SyntaxHighlighter]> |
StepCounter += StepDir | StepCounter += StepDir | ||
- | </file> | + | </sxh> |
Firstly StepCounter (goes from 0-7 because our list is 8 elements long) is affected by the value of StepDir (where you set it to +1 or -1) StepCounter += StepDir means take the value of StepCounter, | Firstly StepCounter (goes from 0-7 because our list is 8 elements long) is affected by the value of StepDir (where you set it to +1 or -1) StepCounter += StepDir means take the value of StepCounter, | ||
\\ | \\ | ||
Line 358: | Line 357: | ||
The next block of code is waiting for the StepCounter to reach the value of StepCount. | The next block of code is waiting for the StepCounter to reach the value of StepCount. | ||
\\ | \\ | ||
- | <file> | + | <sxh [py][; options for SyntaxHighlighter]> |
if (StepCounter> | if (StepCounter> | ||
StepCounter = 0 | StepCounter = 0 | ||
if (StepCounter< | if (StepCounter< | ||
StepCounter = StepCount+StepDir | StepCounter = StepCount+StepDir | ||
- | + | </sxh> | |
- | </file> | + | |
Remember, earlier in this program we said StepCount = len(Seq) (so in this example where we have 8 elements [1,0,0,1] etc) StepCount will = 7 (0 to 7 is 8). | Remember, earlier in this program we said StepCount = len(Seq) (so in this example where we have 8 elements [1,0,0,1] etc) StepCount will = 7 (0 to 7 is 8). | ||
\\ | \\ | ||
Line 373: | Line 371: | ||
The last line is the sleep statement. | The last line is the sleep statement. | ||
\\ | \\ | ||
- | <file> | + | <sxh [py][; options for SyntaxHighlighter]> |
time.sleep(WaitTime) | time.sleep(WaitTime) | ||
- | </file> | + | </sxh> |
This waits for the value of WaitTime. WaitTime was calculated by dividing the value you entered at the command line when the program started by 1000. So if you entered the value 3, then the WaitTime value is 0.003. So between each coil state change we will wait 3mS. | This waits for the value of WaitTime. WaitTime was calculated by dividing the value you entered at the command line when the program started by 1000. So if you entered the value 3, then the WaitTime value is 0.003. So between each coil state change we will wait 3mS. | ||
\\ | \\ |
how_to_use_a_stepper_motor.txt · Last modified: 2023/03/09 22:35 by 127.0.0.1