Driving Bi-Polar Stepper Motor with Keyes L298N

Written by: roboguru

Picture of Driving Bi-Polar Stepper Motor with Keyes L298NDual_H-Bridge_Motor_Driver_Keyes.jpg

Commonly people use Darlington Array to control stepper motors such as ULN2003 or ULN2004. Here I want to show you how to use L298N Dual Motor Driver to drive a 4-wire bi-polar stepper motor. Well, it may be more expensive than a Darlington Array Chip so consider this as an "option".

The reason I am working with this test is :

  • There was a comment on my Auto Fish Feeder questioning why I did not use a driver module or at least transistors to drive the 5V bi-polar stepper motor. It was because I couldn't find a Darlington Array Chip in town.
  • I only had one L298N motor driver which I saved for my Panzer project. Thank God now I have 3 Keyes L298N motor drivers for testing (^_^ )
  • I also read some people out there asking how to do this, they had the same problem with me, didn't own a Darlington Array chip but had a L298N driver in hand.

So I search on the net to learn more about a stepper motor, particularly a 4-wire bi-polar stepper motor which I used in my Auto Fish Feeder project. It is a mini 5V stepper motor which can be found in a floppy disk drive.

Step 1: Bill of Materials

  1. A 5V Bi-Polar Stepper Motor. You can find one in your old unused floppy disk drive.
  2. A Keyes L298N Motor Driver. Or any other compatible or similar L298N module driver.
  3. An Arduino Uno R3 or compatible. Here I use Sparkfun RedBoard.
  4. Some jumper wires.
  5. A Multimeter.

Step 2: Stepper : How It Works

Picture of Stepper : How It Worksmovement.jpg

Internal diagram of a 4-wire stepper motor can be drawn like picture number one above. While picture number two shows us the internal working of a stepper motor to make it moves clockwise or counter clockwise. All we have to do is energized the coils in correct orders.

From the Movement Diagram above : 
(best viewed in browsers)

Clockwise
Degree Y X ~Y ~X
225 0 0 1 1
315 0 1 1 0
45 1 1 0 0
135 1 0 0 1
 
Counter Clockwise
Degree Y X ~Y ~X
225 0 0 1 1
135 1 0 0 1
45 1 1 0 0
315 0 1 1 0

How do we figure out which wires in pair (X and ~X; Y and ~Y)? Because we want to SOURCE or SINK the coils to produce desired magnetic field to move the motor. Use a multimeter and set it to Ohm Meter, we are measuring the resistance between cables.

When I measure pin 1 and pin 3 it reads 12.6 ohm. So is it when I measure pin 2 and pin 4. There is a resistance when the pins are connected. We can also set the multimeter to "continuity check".

When I measure pin 1 and pin 2 it shows "1" on the left side which means "out-of-range". The same thing happens when I measure pin 2 and pin 3. That means they are not connected.

You can try visit this page for more detail information about Operation Principle of Stepper Motor. It has some GIF animations and a complete table of stepper degree and state of pins to make you understand better.

Step 3: Wiring

Wires from Keyes L298N Module to Arduino Uno : 
(best viewed in browsers)

Color Keyes L298N Arduino Uno
Orange ENA Pin 7
Yellow IN1 Pin ~6
Green IN2 Pin ~5
Blue IN3 Pin ~10
Purple IN4 Pin ~9
Grey ENB Pin 8
Black GND GND
White +5V Vin

  • Stepper pin 1 and pin 3 to Keyes L298N Motor A.
  • Stepper pin 2 and pin 4 to Keyes L298N Motor B.
  • Battery+ to Keyes L298N VMS.
  • Battery- to Keyes L298N GND.

Step 4: Arduino Sketch

 

/*
* Driving a 5V stepper motor using Keyes L298N Dual Motor Driver; * Chienline @2015; */

const int ENA = 7; const int IN1 = 6; const int IN2 = 5; const int ENB = 8; const int IN4 = 9; const int IN3 = 10; const int ledPin = 13;

void setup() { pinMode(ENA,OUTPUT); pinMode(IN1,OUTPUT); pinMode(IN2,OUTPUT); pinMode(ENB,OUTPUT); pinMode(IN3,OUTPUT); pinMode(IN4,OUTPUT); pinMode(ledPin,OUTPUT); digitalWrite(ledPin, LOW);

//delay is used to control the speed, the lower the faster. //reverse(step,delay); reverse(80,20); //forward(step,delay); forward(80,20); }

void loop() { }

void reverse(int i, int j) {

// set both motors ON digitalWrite(ENA, HIGH); digitalWrite(ENB, HIGH);

while (1) { digitalWrite(IN1, 0); digitalWrite(IN2, 1); digitalWrite(IN3, 0); digitalWrite(IN4, 1); delay(j); i--; if (i < 1) break;

digitalWrite(IN1, 0); digitalWrite(IN2, 1); digitalWrite(IN3, 1); digitalWrite(IN4, 0); delay(j); i--; if (i < 1) break;

digitalWrite(IN1, 1); digitalWrite(IN2, 0); digitalWrite(IN3, 1); digitalWrite(IN4, 0); delay(j); i--; if (i < 1) break;

digitalWrite(IN1, 1); digitalWrite(IN2, 0); digitalWrite(IN3, 0); digitalWrite(IN4, 1); delay(j); i--; if (i < 1) break; }

// set both motors OFF digitalWrite(ENA, LOW); digitalWrite(ENB, LOW); } // end reverse()

void forward(int i, int j) {

// Set both motors ON digitalWrite(ENA, HIGH); digitalWrite(ENB, HIGH);

while (1) { digitalWrite(IN1, 0); digitalWrite(IN2, 1); digitalWrite(IN3, 0); digitalWrite(IN4, 1); delay(j); i--; if (i < 1) break;

digitalWrite(IN1, 1); digitalWrite(IN2, 0); digitalWrite(IN3, 0); digitalWrite(IN4, 1); delay(j); i--; if (i < 1) break;

digitalWrite(IN1, 1); digitalWrite(IN2, 0); digitalWrite(IN3, 1); digitalWrite(IN4, 0); delay(j); i--; if (i < 1) break;

digitalWrite(IN1, 0); digitalWrite(IN2, 1); digitalWrite(IN3, 1); digitalWrite(IN4, 0); delay(j); i--; if (i < 1) break; }

// set both motors OFF digitalWrite(ENA, LOW); digitalWrite(ENB, LOW);

} // end forward()

 

Step 5: See How It Works

As you can see in the video, it moves backward then forward. I used it on myAuto Fish Feeder for a month before the tank was given to my friend because we need the space for a new larger refrigerator T-T

I kept my fish feeder and torn it apart to make this instructable. Hope this helps and hope I get a new fish tank one day ^_^

Leave a Reply