Introduction: Driving Bi-Polar Stepper Motor With Keyes L298N

Picture of Driving Bi-Polar Stepper Motor With Keyes L298N

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 Works

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
DegreeYX~Y~X
2250011
3150110
451100
1351001
Counter Clockwise
DegreeYX~Y~X
2250011
1351001
451100
3150110

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)

ColorKeyes L298NArduino Uno
OrangeENAPin 7
YellowIN1Pin ~6
GreenIN2Pin ~5
BlueIN3Pin ~10
PurpleIN4Pin ~9
GreyENBPin 8
BlackGNDGND
White+5VVin

  • 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 my Auto 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 ^_^

Comments

WinnieD1 (author)2016-09-04

do i need to power up my arduino with the 5v output of the L298N? cause mind is not working..

chienline (author)WinnieD12016-09-23

You can have a separate power for Arduino and Keyes module, but make sure you make their Grounds common (put a wire from Arduino GND to Keyes GND).
And also make sure you do a correct wiring on your stepper using a multimeter (step#2).

Gelfling6 (author)WinnieD12016-09-22

You could, but I've heard rumors of possible problems of back-feeding the +5V, raising havoc with the switching between USB & on-board regulated +5V.

I would never, however, feed the +5V pin of the arduino to the LM298N alone, rather, a separate +5V supply should be used. this causes the Arduino's regulator chip to overload, and causes the AVR itself to brown-out with lack of power. Never power anything stronger than 2 RC servos off the +5 pin!

I've run most of my projects off a +12V output from a modified ATX supply, BUT... fed the power through two regulators in series, from +12 to the input of a 7809 (+9V regulator), and the output from the 7809 to the input of a separate 7805 (+5v regulator) , creating a +9V rail (Arduino main supply) and +5V rail (servo, other logic requiring higher amperage.) all common GND.

I keep running into steppers which are in the 1.0 to 5-Ohm range, which leads me to believe they're more of the 3V & 5V coils, and feeding +12V to them would fry them.

You can alter the L298N, by removing the jumper that send VMs (+12V, Motor Supply), and run a jumper from the VMs terminal, and put it in with the +5V terminal, with the wire from the external 7805. This applies +5V to both the logic & Motor Supply sides of the L298N. (also, if your motors are in the 15-36V Range, Remove this jumper!! Critical! anything higher than 15V will Cook the onboard regulator! and simply supply a separate +5 Logic supply.)

chienline (author)WinnieD12016-09-04

And also remember to make sure your power source has enough power to drive the stepper motor you are using. Let's say if you are using a larger stepper motor with a small power battery to power up your L298N board, then the stepper motor will not move because the power is just not enough to move it.

chienline (author)WinnieD12016-09-04

You can power up your arduino from any different power source. If you have a different power sources, try to common the ground, I mean put the ground together. So a jumper wire from your arduino GND to L298N GND is okay. It doesn't matter if you connect the L298N from a battery and your Arduino from a wall wart adaptor.

If it is still not working then you have to re-check the connection to see if you have plugged any wire at the wrong pin.

KennyL15 (author)2016-02-09

Thanks for making this tutorial. However, it appears you haven't added any protection diodes for protecting the L298N. Also, your software program appears sometimes to make pins 1 and 3 equal voltage; and pins 2 and 4 equal voltage, which seems to be removing power from both coils at the same time. This doesn't look right to me.

Kenny

chienline (author)KennyL152016-02-15

Thanks for reading. I think we already have built-in protection diode on Keyes module.

So you are a little confused with the coding. It is not having stepper pin 1 and pin 3 equal high voltage.
At the beginning I set pin 1 and pin 3 of stepper motor into IN1 and IN2 in accordance to the Keyes board pins naming :)
So is pin 2 and pin 4 of stepper motor is set to IN3 and IN4. Then in the sketch I no longer call the stepper pin number, but IN1, IN2 for Motor A; IN3, IN4 for Motor B :)

jjdesign (author)2016-01-08

como eu faco para colocar 3 motores desse usando arduino e o drive l298n


chienline (author)jjdesign2016-01-09

"how do I put 3 motors that using Arduino and the L298N driver?"

You can't drive 3 motors. L298N can only drives 2 DC Motors or 1 Bipolar Stepper Motor. To drive 3 DC Motors you need 2 pieces of L298N driver. To drive 3 Bipolar Stepper Motor you need 3 pieces of L298N driver.

chienline (author)2015-03-20

Okay, I have fix the code. Sorry for the previous mess. Just try my Pro Membership and write it directly in HTML codes :D

I made tables, but somehow they are not rendered well in mobile app. But I have check that the tables are okay in Browsers. Check it out ^_^

chienline (author)2015-03-20

Sorry, what a mess in the code. Will update soon. I was trying to use table in the code as a pro user :D

About This Instructable

24,343views

25favorites

License:

Bio: Just an ordinary person who loves #thinking and #tinkering
More by chienline:Magnetic Hex Extension KeyringDSLR Cowboy HolsterWoven Tag Hiding Place
Add instructable to: