WickedPenguin
Posts: 33
Joined: Wed Sep 04, 2013 7:11 pm

TUTORIAL - How to control a bipoloar 4 wire stepper motor

Sat Sep 14, 2013 4:00 am

It was a bit of a mission getting this working, so I thought others might benefit from a tutorial.

Aim:

To control a 4 wire bipolar stepper motor using a Raspberry pi, external power source and a L298 H-Bridge Board.

Parts:

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
Raspeberry Pi

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.

cobbler.jpg
cobbler.jpg (34.6 KiB) Viewed 70960 times
enaandb.jpg
enaandb.jpg (50.61 KiB) Viewed 70960 times


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.
cobbler02.jpg
cobbler02.jpg (49.62 KiB) Viewed 70960 times
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.

Image

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.

Image

Image

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)
Hopefully this tutorial has been beneficial to those starting out with the raspberry pi and interesting in the GPIO motor control side of things.
Last edited by WickedPenguin on Tue Nov 05, 2013 7:23 am, edited 2 times in total.

WickedPenguin
Posts: 33
Joined: Wed Sep 04, 2013 7:11 pm

Re: TUTORIAL - How to control a bipoloar 4 wire stepper moto

Sat Sep 14, 2013 4:03 am

blank
Last edited by WickedPenguin on Sat Sep 14, 2013 6:43 pm, edited 1 time in total.

User avatar
joan
Posts: 13655
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: TUTORIAL - How to control a bipoloar 4 wire stepper moto

Sat Sep 14, 2013 8:50 am

WickedPenguin wrote:Please Note: i have lots more images, but the forum wont allow me to add more than 3. Any way around this?
Host the images on one one of the many free image hosting sites and post links.

VenCain
Posts: 1
Joined: Tue Nov 05, 2013 1:55 am

Re: TUTORIAL - How to control a bipoloar 4 wire stepper moto

Tue Nov 05, 2013 1:59 am

I'm a 99.99% software and .01% hardware guy so I'll defer to those in the know but I'm betting you want to couple your grounds.

-VenCain

promet
Posts: 3
Joined: Thu Oct 31, 2013 12:53 am

Re: TUTORIAL - How to control a bipoloar 4 wire stepper moto

Sun Dec 08, 2013 11:05 pm

VenCain wrote:I'm a 99.99% software and .01% hardware guy so I'll defer to those in the know but I'm betting you want to couple your grounds.

-VenCain
Hi VenCain,

I've been fidgeting with my Raspi and an H-Bridge driver all afternoon, with no small amount of confusion. Your simple remark above has been the missing link in getting my DC Motor running; couple the grounds, you maroon. There is always so much to puzzle out, I often overlook the simplest things...

Also I, insomuch as it is within my power, am granting you the honorary rank of, ".07% Hardware Guy", lol! :D

Thanks!

sesam
Posts: 16
Joined: Sun Apr 28, 2013 7:45 pm

Re: TUTORIAL - How to control a bipoloar 4 wire stepper moto

Mon Dec 30, 2013 7:48 pm

Great tutorial.

Just out of curiosity...
has anyone tried that with Piface ?

Ravenous
Posts: 1956
Joined: Fri Feb 24, 2012 1:01 pm
Location: UK

Re: TUTORIAL - How to control a bipoloar 4 wire stepper moto

Tue Dec 31, 2013 9:20 am

sesam wrote:has anyone tried that with Piface ?
As far as I can tell the piface doesn't have H-bridge chips on it so wouldn't directly drive a bipolar stepper. (Someone please correct me if I'm wrong as I am having trouble finding any technical details.)

However the open collector outputs would probably drive a small "unipolar" stepper motor, which usually has five or six wires. In that case four outputs would be needed to drive one motor.

sesam
Posts: 16
Joined: Sun Apr 28, 2013 7:45 pm

Re: TUTORIAL - How to control a bipoloar 4 wire stepper moto

Tue Dec 31, 2013 9:34 am

From here:
The outputs are driven via a standard ULN2803A darlington driver chip
You seem to be right,
a little bit of googling around and I am under the impression that ULN2803A cannot act as an H-bridge.
:(

raspbee
Posts: 40
Joined: Tue Sep 25, 2012 1:25 pm

PL15S-020

Fri Feb 07, 2014 5:03 pm

I was trying this tutorial with the PL15S-020 from an old DVD drive.
However the motor is just making some noise and vibrating/jiggling.

Usually this is when wired wrong however I took the settings from the specs file of the motor.
http://robocup.idi.ntnu.no/wiki/images/ ... 15S020.pdf

The sequence of the motor is:
Step1: A1,[A2],[B1],B3
Step2: A1,[A2],B1,[B3]
Step3: [A1],A2,B1,[B3]
Step4: [A1],A2,[B1],B3


I am directly connecting a L298N H-bridge to the raspberry. Power 5V comes from the raspberry 2A power supply.

Code: Select all

import RPi.GPIO as GPIO
import time

delay=0.02
steps=500

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)

# 4,17,23,24
coil_A_1_pin = 23
coil_A_2_pin = 24
coil_B_1_pin = 17
coil_B_2_pin = 4

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)

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)

for i in range(0, steps):

     setStep(0,1,1,0)
     time.sleep(delay)
     setStep(0,1,0,1)
     time.sleep(delay)
     setStep(1,0,0,1)
     time.sleep(delay)
     setStep(1,0,1,0)
     time.sleep(delay)



Does anyone know whats wrong here?

Ravenous
Posts: 1956
Joined: Fri Feb 24, 2012 1:01 pm
Location: UK

Re: TUTORIAL - How to control a bipoloar 4 wire stepper moto

Fri Feb 07, 2014 5:15 pm

Try running it at a much slower speed so you can see each step, and put something on the shaft (like a piece of blu tak or strip of tape) as a "pointer" so you can see the angle moved by each turn.

Your sequence does look sensible at first glance so maybe it's a classic speed & torque problem... just guessing though.

raspbee
Posts: 40
Joined: Tue Sep 25, 2012 1:25 pm

Re: TUTORIAL - How to control a bipoloar 4 wire stepper moto

Fri Feb 07, 2014 6:00 pm

I tried that already (it is still on the rail of the dvd drive) and it looks like the rail is moving forward and backward. I made an output after the steps and it looks like the first two move forward and the last two move backward. I just don´t know what to make out of it. I tried some other type of bipolar motor and that worked fine.

MorePiPlease
Posts: 21
Joined: Mon Nov 05, 2012 1:59 pm

Re: TUTORIAL - How to control a bipoloar 4 wire stepper moto

Sat Nov 22, 2014 5:59 pm

I am about to begin my quest to run a stepper motor with an h-bridge and I am using the info from this thread to do so...thank you for sharing and creating this information. I read the previous thread and know that you started without knowing how to do this and researched it in order to create this thread. I really appreciate it and everyone who has put such helpful information on here.

I have all the parts and a fresh cup of coffee.

Here is the last project I built with some friends and a Raspberry Pi :-) www.rowboboat.com

On that project I used a 12v DC motor with an encoder wheel (heady days). This stepper and a belt are going to be the linear actuation on the next version of that boat control system.

MorePiPlease
Posts: 21
Joined: Mon Nov 05, 2012 1:59 pm

Re: TUTORIAL - How to control a bipoloar 4 wire stepper moto

Sat Nov 22, 2014 7:54 pm

I am using the raspberry pi b+ and these two components along with a bench-top 12v power supply.

stepper motor part no: 42hs4013a4
L298N SainSmart
http://www.sainsmart.com/sainsmart-l298 ... robot.html

I have taken the jumpers off the ENA and ENB pins (they were both jumped to 5v)...time for a research break. :D

MorePiPlease
Posts: 21
Joined: Mon Nov 05, 2012 1:59 pm

Re: TUTORIAL - How to control a bipoloar 4 wire stepper moto

Sun Nov 23, 2014 3:53 am

It is working! I had to find the following wiring layout on the internet and perhaps this will help someone else. :lol: 8-) :D ;)
SainSmart_H_Bridge_Stepper_Driver_Wiring.png
SainSmart H Bridge Stepper Motor Driver wiring
SainSmart_H_Bridge_Stepper_Driver_Wiring.png (22.51 KiB) Viewed 55831 times

User avatar
joan
Posts: 13655
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: TUTORIAL - How to control a bipoloar 4 wire stepper moto

Sun Nov 23, 2014 9:13 am

MorePiPlease wrote: ...
I have taken the jumpers off the ENA and ENB pins (they were both jumped to 5v)...time for a research break. :D
Any particular reason for removing the jumpers? If you remove the jumper for ENx you can use ENx for PWM but that isn't appropriate for a stepper motor.

TENOJI
Posts: 12
Joined: Thu Jun 26, 2014 7:54 pm

Re: TUTORIAL - How to control a bipoloar 4 wire stepper moto

Sat Dec 27, 2014 10:17 pm

I have tried using this tutorial to control my stepper motor. However, it hasn't hummed, vibrated, or done anything. The lights on the driver board are all lit up except the IN4 light. Should this be lit? I am using the stepper motor in the link:
http://www.amazon.com/Vexta-PK266-02A-S ... AV42HVS31S
I am also using the same driver board as the one in the tutorial. The power supply is 3 9-volt batteries connected in parallel. Please help.

Axolotl42
Posts: 4
Joined: Fri Dec 20, 2013 11:57 am

Re: TUTORIAL - How to control a bipoloar 4 wire stepper moto

Sat Jan 24, 2015 3:02 pm

I don't have a lot of experience with de Pi, so if I am making a fool of myself, I am sorry.
You wrote about the ENA and ENB pins. In the code it looked like those pins are used to power the motors, but in the text you said you used batterys. What is the purpose of those pins? I understand everything else in the code, but not that. It would be nice if you could help me, because I am working on a school mproject where I need this...

User avatar
joan
Posts: 13655
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: TUTORIAL - How to control a bipoloar 4 wire stepper moto

Sat Jan 24, 2015 6:10 pm

Axolotl42 wrote:I don't have a lot of experience with de Pi, so if I am making a fool of myself, I am sorry.
You wrote about the ENA and ENB pins. In the code it looked like those pins are used to power the motors, but in the text you said you used batterys. What is the purpose of those pins? I understand everything else in the code, but not that. It would be nice if you could help me, because I am working on a school mproject where I need this...
The enable pins are used to switch the motor driver output circuitry on or off.

For a stepper motor this circuitry should always be enabled.

Some boards have a 5V pin behind the enable pin. Putting a jumper over the enable and 5V pin will permanently enable the output (which is what you want for a stepper motor).

MorePiPlease
Posts: 21
Joined: Mon Nov 05, 2012 1:59 pm

Re: TUTORIAL - How to control a bipoloar 4 wire stepper moto

Sat Jan 24, 2015 10:45 pm

I'll have to check but I think on my h-bridge I needed to remove ENA and ENB jumpers so that I was individually powering the coils in the stepper. Without removing those jumpers those switches would always be on...let me check.

Kisa_ua
Posts: 2
Joined: Wed Mar 11, 2015 6:43 pm

Re: TUTORIAL - How to control a bipoloar 4 wire stepper moto

Mon Apr 06, 2015 9:57 am

for me works great.

Is there the difference how to run steeps ?

1,0,1,0
0,1,1,0
0,1,0,1
1,0,0,1

or in the different way like
0,1,1,0
0,1,0,1
1,0,0,1
1,0,1,0

is there is the difference how to connect "out 1" and "out 2" to one of the coils?

strongcoffee
Posts: 1
Joined: Tue Jun 09, 2015 7:09 pm

Re: TUTORIAL - How to control a bipoloar 4 wire stepper moto

Tue Jun 09, 2015 7:12 pm

Superb tutorial, just what I was looking for. Worked 'out of the box'!


dixit.hrishikesh26
Posts: 2
Joined: Wed Oct 14, 2015 7:27 am

Re: TUTORIAL - How to control a bipoloar 4 wire stepper moto

Wed Oct 14, 2015 7:31 am

can i connect two l298 to Rpi b+? i wanted to control 4 motors with Rpi

NewtownGuy
Posts: 10
Joined: Mon Feb 18, 2013 6:25 pm

Re: TUTORIAL - How to control a bipoloar 4 wire stepper moto

Thu Dec 03, 2015 11:17 pm

How, exactly, are the GPIO outputs updated by the OS ? With the code as shown, isn't there a risk that some other task will interrupt the updating of the bits that control the stepper motors, and cause improper operation of the motors ? If so, wouldn't it be better to strobe the enable lines on and off for each step so the four data bits are always driven correctly ? It's a lot more GPIO operations, and the durations could still vary, but the data bits would be correct.

vinderluvu
Posts: 6
Joined: Mon Feb 15, 2016 4:43 am
Contact: Website

Re: TUTORIAL - How to control a bipoloar 4 wire stepper moto

Mon Feb 15, 2016 5:20 am

A stepper motor is a motor controlled by a series of electromagnetic coils. bikram majithiaThe center shaft has a series of magnets mounted on it, and the coils surrounding the shaft are alternately given current or not, creating magnetic fields which repulse or attract the magnets on the shaft, causing the motor to rotate.

Return to “Teaching and learning resources”