Having played with LEDs, switches and buzzers I felt the natural next step was playing with a stepper motor 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.
There is a huge selection of motors to buy but I decided to experiment with a 28BJY-48 with ULN2003 control board.
The reasons I chose this device where :
- It is cheap
- Widely available from both overseas and UK sellers
- Easy to obtain with a controller board
- Small but relatively powerful
- Runs on 5V
- 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
- 5V (P1-02)
- GND (P1-06)
- Inp1 (P1-18)
- Inp2 (P1-22)
- Inp3 (P1-24)
- Inp4 (P1-26)
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 will rotate. The direction can be reversed by reversing the sequence.
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.
#!/usr/bin/env python # Import required libraries import sys 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 # Physical pins 11,15,16,18 # GPIO17,GPIO22,GPIO23,GPIO24 StepPins = [17,22,23,24] # Set all pins as output for pin in StepPins: print "Setup pins" GPIO.setup(pin,GPIO.OUT) GPIO.output(pin, False) # Define advanced sequence # as shown in manufacturers datasheet Seq = [[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], [1,0,0,1]] StepCount = len(Seq)-1 StepDir = 2 # Set to 1 or 2 for clockwise # Set to -1 or -2 for anti-clockwise # Read wait time from command line if len(sys.argv)>1: WaitTime = int(sys.argv)/float(1000) else: WaitTime = 10/float(1000) # Initialise variables StepCounter = 0 # Start main loop while True: for pin in range(0, 4): xpin = StepPins[pin]print StepCounter print pin if Seq[StepCounter][pin]!=0: print " Step %i Enable %i" %(StepCounter,xpin) GPIO.output(xpin, True) else: GPIO.output(xpin, False) StepCounter += StepDir # 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)
You can download it directly to your Pi using :
As with all Python scripts that use the GPIO library it needs to be run using “sudo” :
sudo python stepper.py
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 :
sudo python stepper.py 20
where 20 is the number of milliseconds.
In this example the default wait time is set to 0.01 seconds (10 milliseconds). To change the speed of rotation you can change this value. I found I could reduce it to 4ms 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.
You can now control a stepper motor using a Raspberry Pi and some Python script. Add another motor and you’ve got the beginnings of a small robot!