Vous êtes sur la page 1sur 3

19.02.2016.

TRENDING

StepperMotorControlInPython
Welcome

HOME

CATEGORIES

YOU ARE AT:

Home

General

BERRYCLIP

BUY

TOOLS

TUTORIALS & HELP

CONTACT US

Search...

SITE MAP

Stepper Motor Control In Python

Stepper Motor Control In Python


BY MATT ON JULY 11, 2012

45

GENERAL

RECENT

POPULAR

NEWS

FEBRUARY 15, 2016

9 of the Best Raspberry Pi Projects

FEBRUARY 8, 2016

Usborne Releases 1980s Coding

Having played with LEDs, switches and buzzers I felt the natural next step was playing with a stepper motor

Books As Free PDFs

or two. This might form part of an idea I had to create an automated stop motion animation turn table for
rotating and photographing objects.

DECEMBER 8, 2015

Lumsing Grand A1 Plus Power Bank

There is a huge selection of motors to buy but I decided to experiment with a 28BJY-48 with ULN2003 control board.

Test

The reasons I chose this device where :

DECEMBER 6, 2015

3D Printed Adafruit Raspberry Pi

It is cheap

Zero Case Part 2

Widely available from both overseas and UK sellers


Easy to obtain with a controller board

DECEMBER 3, 2015

Small but relatively powerful


Runs on 5V

Zero Case

Easy to interface
I ordered two from 4tronix_uk on eBay and they arrived the next day. There are additional details in the Stepper
Motor 28BJY-48 Datasheet
Interfacing With The Pi

3D Printed Adafruit Raspberryy Pi

Search

Search

CATEGORIES

The motor connects to the controller board with a presupplied connector. The controller board has 4+2 pins that
need to be connected to the Pi header (P1).
5V (P1-02)
GND (P1-06)

3D Printing
Add-ons
BerryClip
Books
Camera Module

and

Cases
Inp1 (P1-11)
Inp2 (P1-15)

Events
General

Inp3 (P1-16)
Inp4 (P1-18)

Hardware
I2C

http://www.raspberrypispy.co.uk/2012/07/steppermotorcontrolinpython/#comments

1/10

19.02.2016.

StepperMotorControlInPython

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.
To rotate the stepper motor you provide a sequence of high
and low levels to each of the 4 inputs in sequence. By setting
the correct sequence of high and low levels the motor spindle

Infographics
Minecraft
Model A+
Model B+
News
Pi 2 Model B

will rotate. The direction can be reversed by reversing the

Pi Zero

sequence.

Power

Python Script

Programming
Python

Here is a copy of the stepper motor script I used to rotate the stepper motor. It uses the RPi.GPIO library and
defines a 4-step and 8-step sequence.

Raspbian
Robotics

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

#!/usr/bin/python
#Importrequiredlibraries
importsys
importtime
importRPi.GPIOasGPIO

#UseBCMGPIOreferences
#insteadofphysicalpinnumbers
GPIO.setmode(GPIO.BCM)

#DefineGPIOsignalstouse
#Physicalpins11,15,16,18
#GPIO17,GPIO22,GPIO23,GPIO24
StepPins=[17,22,23,24]

#Setallpinsasoutput
forpininStepPins:
print"Setuppins"
GPIO.setup(pin,GPIO.OUT)
GPIO.output(pin,False)

#Defineadvancedsequence
#asshowninmanufacturersdatasheet
Seq=[[1,0,0,1],
[1,0,0,0],
[1,1,0,0],
[0,1,0,0],
[0,1,1,0],
[0,0,1,0],
[0,0,1,1],
[0,0,0,1]]

StepCount=len(Seq)
StepDir=1#Setto1or2forclockwise
#Setto1or2foranticlockwise

#Readwaittimefromcommandline
iflen(sys.argv)>1:
WaitTime=int(sys.argv[1])/float(1000)
else:
WaitTime=10/float(1000)

#Initialisevariables
StepCounter=0

#Startmainloop
whileTrue:

printStepCounter,
printSeq[StepCounter]

forpininrange(0,4):
xpin=StepPins[pin]#GetGPIO
ifSeq[StepCounter][pin]!=0:
print"EnableGPIO%i"%(xpin)
GPIO.output(xpin,True)
else:
GPIO.output(xpin,False)

StepCounter+=StepDir

#Ifwereachtheendofthesequence
#startagain
if(StepCounter>=StepCount):
StepCounter=0
if(StepCounter<0):
StepCounter=StepCount+StepDir

#Waitbeforemovingon
time.sleep(WaitTime)

Sensors
Software
Tutorials & Help

TAGS

3D Printing

44780

battery

berryclip

cambridge

camera

DigiMakers

mp4

portable

Model A

sensor
ultrasonic

sd card

soldering

cpu

i2c
Linux

Model B

photos

Pi-Lite

python

power

revision

Book

CamJam

Li-on

photography

Raspberry Pi Bootcamp
ravpower

bluetooth

LED

Minecraft

pcb

avconv

GPIO

games

lcd

Kickstarter
mcp23017

audio

SPI

raspbian
security
temperature

video

RASPBERRY PI RELATED
You can download it directly to your Pi using :
Averageman Vs Pi
wgethttps://bitbucket.org/MattHawkinsUK/rpispy-misc/raw/master/python/stepper.py

MattsBits Pi Resources
Official RaspBerry Pi Site

As with all Python scripts that use the GPIO library it needs to be run using sudo :
1

sudopythonstepper.py

Press Ctrl-C to quit.

http://www.raspberrypispy.co.uk/2012/07/steppermotorcontrolinpython/#comments

Raspberry Pi Pod
RasPi.tv
Raspihub.com

2/10

19.02.2016.

StepperMotorControlInPython
Raspihub.com

Press Ctrl-C to quit.


To specify a different wait time you can pass a number of milliseconds as an argument on the command line using :

RaspTut
Rastrack.co.uk
RPi School

sudo python stepper.py 20


where 20 is the number of milliseconds.

TECH RESOURCES

In this example the default wait time is set to 0.01 seconds (10 milliseconds). To change the speed of rotation you
can change thisvalue. I found I could reduce it to 4ms before the motor stopped working. If the script runs too fast
the motor controller cant keep up. This performance may vary depending on your motor and its controller.

How2Code
Technology Spy

The 4 step sequence is faster but the torque is lower. Its 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

ARCHIVES

speed so I will be using the 8 step sequence.

Archives

SelectMonth

You can now control a stepper motor using a Raspberry Pi and some Python script. Add another motor and youve
got the beginnings of a small robot!

SHARE.

PREVIOUS ARTICLE

Knight Rider & Cylon Lights for the


Raspberry Pi

NEXT ARTICLE

Install RPi.GPIO Library In Raspbian

RELATED POSTS

NOVEMBER 22, 2015

DECEMBER 8, 2014

Raspberry Pi microSD Card


Shoot-out

Pi Wars Saturday 6th


December 2014

MAY 30, 2014

APRIL 3, 2014

SEPTEMBER 19, 2014

57

Top 5 Reasons The Raspberry Pi


Sucks

11

Simple Raspberry Pi Camera


Photo Gallery Using PHP

Celebrating The New


RaspberryPi.org Website Design

MARCH 23, 2013

Bristol Mini Maker Faire 2013

MARCH 12, 2012

Glossary

OCTOBER 16, 2012

Bristol Raspberry Pi Jam


October 2012

MARCH 12, 2012

What is a Raspberry Pi?

45 COMMENTS

DAVID on JULY 17, 2012 4:17 PM

As I understand it, the torque is higher when you select two inputs because you have twice the number
of coils activated. If this is the case, then surely your 8 step sequence actually does High Torque turn
(2 inputs), Low Torque turn (1 input), High Torque turn (2 inputs), Low Torque turn (1 input)
I could be very wrong, but think you may not be using the entire torque available.

http://www.raspberrypispy.co.uk/2012/07/steppermotorcontrolinpython/#comments

REPLY

3/10

Vous aimerez peut-être aussi