To control a 4 wire bipolar stepper motor using a Raspberry pi, external power source and a L298 H-Bridge Board.
External Power source for stepper - I used a 8 x AA battery pack from jcar - $1.20
L298N H-bridge Board - $3 from ebay
12V DC Bipolar Stepper - From an old epson printer
T-Cobbler board - Not required, but useful
Bread Board - Also not required, but useful
4 x F to F Jumper cables
6 x M to F Jumper cables
Firstly you need to connect two GPIO pins to the L298N board. These two pins will be used to enable the stepper motor by setting the ENA and ENB pins to high (Some boards have a jumper to do this and you wont need to connect to pi)
For this I chose pins 18 and 22. Pin 18 connects to ENA and 22 to ENB. Please view image below for reference.
Next you need to connect the pins which with control the movement or step sequence of the motor. For this I have chosen pins 23,24,4,17.
As you can see from the below image, 23 connects to IN1, 24 to IN2, 4 to IN3 and 17 to IN4. You can use whatever pins you like, but you have to make sure you update the code for this to work.
Now, the raspberry pi doesn't have enough power to run the a stepper motor of this size by itself, so this is where the external power source comes into play. The power soruce V and AMPS will vary depending on the kind of stepper you have. For my stepper I have used a 8 x AA battery pack.
Connect the red and black wires of your power pack to the VMS and GND on your L298N board as shown below.
Next step is to connect your stepper motor to the L298N board. Each stepper is different, but if it has 4 wires then it is probably bipolar. Stepper motors are very interesting little motors, in order to get them moving you have to power each of the internal windings in the right sequence. Sometimes you have be lucky and get it right, however the best output comes from the stepper motor datasheet, which tells you all about warm up , warm down, stepper sequence and loads more.
If you want to learn more have a look at this wiki page. http://en.wikipedia.org/wiki/Stepper_motor
To work out how to connect your motor you use a OHM meter to check the resistance between to wires, if there is a small resistance then those wires are a pair, if you get none then they aren't.
For my stepper, each second wire was part of a pair so I connect the stepper below with one of each pair both MOTA and MOTB output on the L298N board.
Once you have it all connected correctly, its time to for the code. I will be using a python script to give a very basic examples of moving the stepper motor forward then reversing.
As you can see from the code below, you can set the variables delay and steps to whatever you want, the higher the delay in ms, the slow the steps and generally the higher the torque, the less delay, the lower the torque the faster the steps.
Code: Select all
import RPi.GPIO as GPIO import time # Variables delay = 0.0055 steps = 500 GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) # Enable GPIO pins for ENA and ENB for stepper enable_a = 18 enable_b = 22 # Enable pins for IN1-4 to control step sequence coil_A_1_pin = 23 coil_A_2_pin = 24 coil_B_1_pin = 4 coil_B_2_pin = 17 # Set pin states GPIO.setup(enable_a, GPIO.OUT) GPIO.setup(enable_b, GPIO.OUT) GPIO.setup(coil_A_1_pin, GPIO.OUT) GPIO.setup(coil_A_2_pin, GPIO.OUT) GPIO.setup(coil_B_1_pin, GPIO.OUT) GPIO.setup(coil_B_2_pin, GPIO.OUT) # Set ENA and ENB to high to enable stepper GPIO.output(enable_a, True) GPIO.output(enable_b, True) # Function for step sequence def setStep(w1, w2, w3, w4): GPIO.output(coil_A_1_pin, w1) GPIO.output(coil_A_2_pin, w2) GPIO.output(coil_B_1_pin, w3) GPIO.output(coil_B_2_pin, w4) # loop through step sequence based on number of steps for i in range(0, steps): setStep(1,0,1,0) time.sleep(delay) setStep(0,1,1,0) time.sleep(delay) setStep(0,1,0,1) time.sleep(delay) setStep(1,0,0,1) time.sleep(delay) # Reverse previous step sequence to reverse motor direction for i in range(0, steps): setStep(1,0,0,1) time.sleep(delay) setStep(0,1,0,1) time.sleep(delay) setStep(0,1,1,0) time.sleep(delay) setStep(1,0,1,0) time.sleep(delay)