User Tools

Site Tools


how_to_use_a_stepper_motor

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
how_to_use_a_stepper_motor [2017/01/31 20:46] – [Troubleshooting] walkeradminhow_to_use_a_stepper_motor [2023/03/09 22:35] (current) – external edit 127.0.0.1
Line 4: Line 4:
 \\  \\ 
 {{:stepper_motor_banner_small.jpg?650|}} {{:stepper_motor_banner_small.jpg?650|}}
-\\  
-\\  
-<color red>This is work in progress, when this warning disappears, my work here is done :)</color> 
 \\  \\ 
 \\  \\ 
Line 27: Line 24:
 ==== The Stepper Motor ==== ==== The Stepper Motor ====
 \\  \\ 
-A stepper motor contains several coils (depending on the type) that can be 'energised' in a pattern to produce movement, unlike a motor that just spins constantly. Stepper Motors are used where some degree of accuracy in movement is required. For example, getting a motor to only spin for a 1/4 turn would be very tricky. A stepper motor can be 'stepped' a set number of times, this gives it much greater accuracy and repeatable results.+A stepper motor contains several coils (depending on the type) that can be 'energised' in a pattern to produce movement, unlike a motor that just spins constantly. Stepper Motors are used where some degree of accuracy in movement is required. For example, getting a normal (non stepper) motor to only spin for a 1/4 turn would be very tricky. A stepper motor can be 'stepped' a set number of times, this gives it much greater accuracy and repeatable results.
 \\  \\ 
 \\  \\ 
Line 39: Line 36:
 \\  \\ 
 \\  \\ 
-{{:stepper_motor_types.jpg?300|}}+{{:stepper_motor_types.jpg?400|}}
 \\  \\ 
 \\  \\ 
Line 54: Line 51:
 \\  \\ 
 \\  \\ 
-{{:5wiresteppermotor.jpg?140|}}+{{:5wiresteppermotor.jpg?200|}}
 \\  \\ 
 \\  \\ 
Line 68: Line 65:
 \\  \\ 
 \\  \\ 
-{{:s-l1600_stepper_motor.jpg?200|}} +{{:s-l1600_stepper_motor.jpg?250|}} 
-{{:uln2003_pcb.jpg?200|}}+{{:uln2003_pcb.jpg?250|}}
 \\  \\ 
 \\  \\ 
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]>
 #!/usr/bin/python #!/usr/bin/python
 +# 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 244: Line 244:
 </file> </file>
 \\  \\ 
-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)
 </file> </file>
 \\  \\ 
-Next we need to define which GPIO connections we are using (the GPIOxx numbers). This project requires four GPIO connections, and this tells Python which ones we are using.+Next we need to define which GPIO connections we are using (the GPIOxx numbers).  
 +This project requires four GPIO connections, and this tells Python which ones we are using.
 \\  \\ 
-<file>+<sxh [py][; options for SyntaxHighlighter]>
 StepPins = [6,13,19,26] StepPins = [6,13,19,26]
-</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 262:
 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.OUT)   GPIO.setup(pin,GPIO.OUT)
   GPIO.output(pin, False)   GPIO.output(pin, False)
-</file>+</sxh>
 We start a loop, and step through all the values in the previous section <color red>(StepPins = [6,13,19,26])</color> and set each pin mode as an output (GPIO.setup(pin,GPIO.OUT)) and set the pins low <color red>(GPIO.output(pin, False))</color>. We start a loop, and step through all the values in the previous section <color red>(StepPins = [6,13,19,26])</color> and set each pin mode as an output (GPIO.setup(pin,GPIO.OUT)) and set the pins low <color red>(GPIO.output(pin, False))</color>.
 \\  \\ 
Line 271: Line 273:
 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],
        [1,1,0,0],        [1,1,0,0],
Line 280: Line 282:
        [0,0,0,1],        [0,0,0,1],
        [1,0,0,1]]        [1,0,0,1]]
-</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, then you will get slow or zero movement in the stepper motor (and it might get pretty hot). 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, then you will get slow or zero movement in the stepper motor (and it might get pretty hot).
 \\  \\ 
Line 286: Line 288:
 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 303:
 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)>1: if len(sys.argv)>1:
   WaitTime = int(sys.argv[1])/float(1000)   WaitTime = int(sys.argv[1])/float(1000)
 else: else:
   WaitTime = 10/float(1000)   WaitTime = 10/float(1000)
-</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 327:
 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,13,19,26] accordingly. So the first Seq list item is [1,0,0,0]. So this loop reads each of the four parts of this list element, and sets GPIO Pin 6 to High, and Pins 13,19,26 to Low) The next section is a for loop that goes through each list [1,0,0,1] and sets each GPIO Pin [6,13,19,26] accordingly. So the first Seq list item is [1,0,0,0]. So this loop reads each of the four parts of this list element, and sets GPIO Pin 6 to High, and Pins 13,19,26 to Low)
-<file>+<sxh [py][; options for SyntaxHighlighter]>
   for pin in range(0,4):   for pin in range(0,4):
     xpin=StepPins[pin]# Get GPIO     xpin=StepPins[pin]# Get GPIO
Line 341: Line 342:
     else:     else:
       GPIO.output(xpin, False)       GPIO.output(xpin, False)
-</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 348:
 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, and add/subtract 1 to it, and make StepCounter the new value. 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, and add/subtract 1 to it, and make StepCounter the new value.
 \\  \\ 
Line 358: Line 359:
 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>=StepCount):   if (StepCounter>=StepCount):
     StepCounter = 0     StepCounter = 0
   if (StepCounter<0):   if (StepCounter<0):
     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 373:
 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.
 \\  \\ 
Line 386: Line 386:
 \\  \\ 
 \\  \\ 
-{{:steppermotorprototype1280x720.mp4?640x360|}}+{{:steppermotorprototype1280x720.mp4|640x360|autoplay,loop}}
 \\  \\ 
 \\  \\ 
Line 433: Line 433:
 \\  \\ 
 \\  \\ 
-{{:steporder.jpg?640|}} +{{:steppermotorstepordervideo.mp4?640x360|}}
- +
- +
-{{:steporder.jpg?640x360|}}+
 \\  \\ 
 \\  \\ 
how_to_use_a_stepper_motor.1485895604.txt.gz · Last modified: 2023/03/09 22:35 (external edit)