How to control Stepper motor using L298N motor driver and Arduino.

L298N Stepper motor driver with Arduino

The L298N motor driver is dual full-bridge driver mainly used to drive two DC motors. However, this driver can also control speed and direction of bipolar stepper motors like NEMA 17 as I’ll be demonstrating in the later parts of this tutorial.  

In a previous tutorial I looked at the detailed description of the L298N motor driver and how to use this driver to run DC motors. You can check out that tutorial from the link below for further reference.

Consideration before Using L298N motor driver to control stepper motors.

A major challenge with using the L298N motor driver is that it does not have a feature for setting current limit unlike other stepper motor drivers like theTB6560, TB6600, A4988 and other stepper motor drivers. This means you have to be very careful when selecting the stepper motor and power supply to use with this motor driver.

The operating voltage of L298N motor driver is 4.8 to 46V although 35V is the maximum voltage for the driver mounted on a breakout board. Also, the driver can supply a maximum of 2A current per channel therefore you need to find a motor that can be used in this voltage range and does not exceed the maximum current rating of the driver.

Due to the above limitations, not all stepper motors work with the L298N motor driver and you have to check your motor’s datasheet for the voltage and current draw otherwise if the motor draws too much current, it can damage the driver.

You can also estimate the motor’s current draw by measuring the resistance of one of the motor’s coils and use the formula below:

Current draw (A) = Supply voltage (V) ÷ Coil resistance (Ω)

If the motor you want to use doesn’t work with the L298N motor driver, you can use other motor drivers that work better with stepper motors like the ones below that I have written about in my other tutorials.

Connecting Stepper motor to L298N motor driver and Arduino.

Wiring of the stepper motor to Arduino and the L298N motor driver breakout board is done according to the schematic shown below

Schematic for connection of L298N motor driver to Stepper motor and Arduino.

On the power supply terminal block, connect the +12V terminal to 5 ~ 35V DC power supply and the GND is connected to both the power supply and Arduino ground.

The 5V terminal is left unconnected. It is connected to Arduino 5V if the 12 V jumper is removed.

IN1, IN2,1N3 and IN4 are connected to digital pins of Arduino for example in my case I have used digital Pins 8, 9, 10 and 11 respectively.

The stepper motor coil wires are connected to the OUTPUT terminals of the breakout board where OUT1 and OUT2 are for attaching wires from the first motor coil with OUT3 and OUT4 are for the second motor coil. Polarity of the coils doesn’t matter.

How to determine which of the stepper motor wires belong to the same coil?

For a 4-wire bipolar stepper motor, disconnect the motor from the circuit and then pick a random pair of wires from the motor and connect the bare ends together. Then try to spin the shaft of the stepper motor and if you feel a lot of resistance while turning the shaft, you have found a pair of wires from the same coil. If you can still spin the shaft freely, try another pair of wires.

Note: If you are using a power supply higher than 12V you must remove the 12V jumper. This is because when this jumper is connected, the on-board voltage regulator is enabled and it will create the 5V logic voltage. However, when you remove the jumper, you need to provide the board with 5 V from the Arduino.

You also need to keep both the ENA and ENB jumpers in place so that the motor is always enabled.

Do not use a motor that draws more than 2 A at the voltage that you want to use.

Code for controlling a stepper motor using L298N motor driver and Arduino.

The code sketch am going to use is based on the Stepper.h library which is a library for controlling stepper motors that comes already packaged with the Arduino IDE.

#include <Stepper.h> 
const int stepsPerRevolution = 200;
Stepper myStepper = Stepper(stepsPerRevolution, 5, 6, 9, 11);

void setup() {
  myStepper.setSpeed(60);
}

void loop() {
  myStepper.step(stepsPerRevolution);
  delay(2000);
  myStepper.step(-stepsPerRevolution);
  delay(2000);
}

How the above code works.

First include the Stepper.h library which contains the necessary functions for speed and direction control of stepper motors.

Then define the number of steps it takes for the motor to make one revolution. In this case am using the motor in full-step mode and the stepper motor type moves 200 steps to rotate 360 degrees. This value changes depending on the type of stepper motor you are using for example if it is 28BYJ-48 stepper motor then the value will be 48.

Next you need to create a new instance of the Stepper class which takes the steps per revolution of motor and Arduino pin connections as parameters.

In the setup()  section, we set the speed of rotation of the motor in rpm using the setSpeed() function.

In the loop() section, first rotate the motor clockwise by using the step() method and parsing the steps per revolution as the argument inside it. Hence the motor will rotate at steps of 200 per revolution. To rotate the motor anti-clockwise we will parse the steps per revolution with a negative sign inside the step() method. Between the two types of rotations, we will have a delay.

Using the AccelStepper library.

The AccelStepper.h library is a very common Arduino library for controlling the speed, direction and even acceleration and deceleration of the stepper motors.

You can download and install this library within the Arduino Library Manager.

  • Go to Tools > Manage Libraries…  to open the Library Manager with an updated list of installed libraries in your Arduino IDE.
  • Search for ‘accelstepper‘ and look for the library by Mike McCauley.
  • Select the latest version and then click Install.

The example code below shows how you can control the speed, direction, and the number of steps and revolutions of a stepper motor using AccelStepper library.

#include <AccelStepper.h>

#define MotorInterfaceType 4

AccelStepper stepper = AccelStepper(MotorInterfaceType, 8, 9, 10, 11);

void setup() {
stepper.setMaxSpeed(1000);
}

void loop() {
stepper.setCurrentPosition(0);
while (stepper.currentPosition() != 400) {
stepper.setSpeed(200);
stepper.runSpeed();
}
delay(1000);

stepper.setCurrentPosition(0);
while (stepper.currentPosition() != -200) {
stepper.setSpeed(-600);
stepper.runSpeed();
}
delay(1000);

Code description

First include the AccelStepper.h library and then define the motor interface type. The motor interface type is set to 4 since we are using a 4-wire stepper motor in full-step mode.

Next, create a new instance of the AccelStepper class with the appropriate motor interface type and connections to Arduino.

In the setup() section, define the maximum speed in steps per second.

In the loop section, first set the current position of the stepper motor to zero using the setCurrentPosition(0) method.

 A while loop runs continuously until the condition inside the loop becomes false. Therefore, in this case we check if the current position of the stepper motor is not equal to 400 steps. While this is not the case, we run the stepper motor at a constant speed of 200 steps per second as set by the setSpeed() method until the motor reaches 400 steps which is 2 revolutions. The runSpeed() method moves the motor at the speed set by the setSpeed() method.

After the 2 revolutions, the motor is given a delay of 1 second and then set the current position of the motor to zero and using another while loop run the motor backwards at 600 steps per second until the motor reaches -200 steps or 1 revolution.