Driving Bi-Polar Stepper Motor With Keyes L298N

31,804

31

17

About: Just an ordinary person who loves #thinking and #tinkering

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.

Supplies:

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

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 ^_^

Share

    Recommendations

    • Classroom Science Contest

      Classroom Science Contest
    • IoT Challenge

      IoT Challenge
    • Colors of the Rainbow Contest

      Colors of the Rainbow Contest

    17 Discussions

    0
    None
    henry couper

    Question 4 months ago on Step 5

    Hi chienline,

    Your program does the job, no problems. I now wish to make the stepper turn using 2 button switches, one for forwards and one for reverse. Is it possible to nest a program within your program to achieve this. To explain further, the switches will need to be operated remotely. I am building a 3m long model submarine which will be controlled by cable from a aircraft carrier mother ship.

    thanks
    Harry couper
    magpie.power@bigpond,com

    1 answer
    0
    None
    chienlinehenry couper

    Answer 4 months ago

    Awesome project it must be. This was few years back and I need to refresh :D Well, as far as I can imagine your submarine need stepper motor to open/close torpedo launcher, or open/close a door to intake water to sink or float. If let us say you want it for the propellers to move forward or backward, I suggest you to use DC motor instead of stepper motor.

    Then you said about two button to be operated remotely while at the end it is controlled by cable from mothership.

    If it is operated by cable, then it will be simpler. Two "buttons" is placed on the mothership. Check out the Arduino button tutorial, and don't forget the 10K pull-down resistors. When the button is pressed (put digitalRead() in void loop() to keep checking if the buttons are pressed) just called the forward() or reverse() function with the step and speed parameters set.

    If it is operated wirelessly, then you need to learn how to send "button pressed signal" through bluetooth or wifi. The setup is more or less the same, just adding bluetooth modules on both your submarine and mothership. You can check about bluetooth module on my panzer project on step 3.

    Sorry, I can't provide you the exact working code due to my limited time for now I have working full time :)
    But I can direct you at my best to help. All you need is keep searching for we now have the powerful Mr.Google at our fingertips ;)

    hello, I have a keyes L298N v2 and I haven't found the circuit to arduino :( ...

    can you help me? Thanks! :)

    3 replies

    Keyes L298N v2 should not be any difference from v1 and you can see the wiring on Step #3.
    If they are different can you post the link to your product? Maybe I can help you find the wiring ;)

    It's identical to this link :http://www.dx.com/p/keyes-l298n-v2-dc-stepper-gear-motor-drive-module-red-5v-334707
    P. S. I have been resolved this problem, thanks for reply :)

    0
    None
    WinnieD1

    2 years ago

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

    4 replies
    0
    None
    chienlineWinnieD1

    Reply 2 years ago

    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).

    0
    None
    Gelfling6WinnieD1

    Reply 2 years ago

    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.)

    0
    None
    chienlineWinnieD1

    Reply 2 years ago

    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.

    0
    None
    chienlineWinnieD1

    Reply 2 years ago

    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.

    0
    None
    KennyL15

    3 years ago

    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

    1 reply
    0
    None
    chienlineKennyL15

    Reply 3 years ago

    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 :)

    0
    None
    jjdesign

    3 years ago

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


    1 reply
    0
    None
    chienlinejjdesign

    Reply 3 years ago

    "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.

    0
    None
    chienline

    4 years ago on Introduction

    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 ^_^

    0
    None
    chienline

    4 years ago

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