Powered By Blogger

Sunday 8 December 2013

raspberry pi: Controlling of Stepper Motors using Raspberry Pi

The GPIO pins of pi has been put to use here for controlling stepper motor with a simple python coding.

Controlling of Stepper Motors using Raspberry Pi


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)

STEP4:

CONNECT IN D WITH PIN15(GPIO 22)

STEP5: 

CONNECT 5V WITH PIN6(GND) AND PIN2(+5V)








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 libraries
import time
import RPi.GPIO as GPIO
 
# Use BCM GPIO references
# instead of physical pin numbers
GPIO.setmode(GPIO.BCM)
 
# Define GPIO signals to use
# Pins 18,22,24,26
# GPIO24,GPIO25,GPIO8,GPIO7
StepPins = [24,25,8,7]
 
# Set all pins as output
for pin in StepPins:
  print "Setup pins"
  GPIO.setup(pin,GPIO.OUT)
  GPIO.output(pin, False)
 
# Define some settings
StepCounter = 0
WaitTime = 0.5
 
# Define simple sequence
StepCount1 = 4
Seq1 = []
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 datasheet
StepCount2 = 8
Seq2 = []
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 use
Seq = Seq1
StepCount = StepCount1
 
# Start main loop
while 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.