Control Stepper Motor with L298N Motor Driver & Arduino

One of the easiest and inexpensive way to control stepper motors is to interface L298N Motor Driver with Arduino. It can control both speed and spinning direction of of any Bipolar stepper motor like NEMA 17.

If you are planning on building your own 3D printer or a CNC machine, you will need to control a bunch of stepper motors. And having one Arduino control all of them is not a good option. Instead, it is recommended to use a dedicated stepper motor driver like A4988.

Tutorial For Interfacing A4988 Stepper Motor Driver With Arduino
Interface A4988 Stepper Motor Driver Module with Arduino
If you are planning on building your own 3D printer or a CNC machine, you will need to control a bunch of stepper motors. And...

Controlling a Stepper Motor With an H-Bridge

As L298N module has two H-Bridges, each H-Bridge will drive one of the electromagnetic coils of a stepper motor.

By energizing these electromagnetic coils in a specific sequence, the shaft of a stepper can be moved forward or backward precisely in small steps.

However, the speed of a motor is determined by the how frequently these coils are energized.

Below image illustrates driving stepper with H-Bridge.

Controlling a Stepper Motor With an H-Bridge

Driving Bipolar Stepper Motor (NEMA 17)

In our experiment, we are using NEMA 17 bipolar stepper rated at 12V. It offers 200 steps per revolution, and can operate at at 60 RPM. If you don’t already have these specifications, find out now as you will need them for the sketch.

Before we start hooking the motor up with the module, you will need to determine the A+, A-, B+ and B- wires on the motor you plan to use. The best way to do this is to check the datasheet of the motor. For our motor these are red, green, blue and yellow.

NEMA 17 Bipolar Stepper Motor Coil Pinout Color Code

The connections are fairly simple. Start by connecting external 12V power supply to the VCC terminal. And keep the 5V-EN jumper in place.

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

Now, connect the input pins(IN1, IN2, IN3 and IN4) of the L298N module to four Arduino digital output pins(8, 9, 10 and 11).

Finally, connect the A+, A-, B+ and B- wires from the stepper motor to the module as shown in the illustration below.

Wiring NEMA 17 Stepper Motor with L298N & Arduino
Wiring NEMA 17 Stepper Motor with L298N & Arduino

Arduino Code – Controlling NEMA 17 Stepper Motor

The following sketch will give you complete understanding on how to control a bipolar stepper motor like NEMA 17 with L298N motor driver and can serve as the basis for more practical experiments and projects.

// Include the Arduino Stepper Library
#include <Stepper.h>

// Number of steps per output rotation
const int stepsPerRevolution = 200;

// Create Instance of Stepper library
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);


void setup()
{
	// set the speed at 60 rpm:
	myStepper.setSpeed(60);
	// initialize the serial port:
	Serial.begin(9600);
}

void loop() 
{
	// step one revolution in one direction:
	Serial.println("clockwise");
	myStepper.step(stepsPerRevolution);
	delay(500);

	// step one revolution in the other direction:
	Serial.println("counterclockwise");
	myStepper.step(-stepsPerRevolution);
	delay(500);
}

Code Explanation:

The sketch starts with including Arduino Stepper Library. The stepper library comes packaged with the Arduino IDE and takes care of sequencing the pulses we will be sending to our stepper motor.

// Include the Arduino Stepper Library
#include <Stepper.h>

After including the library we define a variable named stepsPerRevolution. As the name suggests it’s the number of steps per revolution that our motor is rated at. In this case it’s 200 i.e. 1.8 degrees per step.

// Number of steps per output rotation
const int stepsPerRevolution = 200;

Next, we create an instance of the stepper library. It takes the steps per revolution of motor & Arduino pin connections as parameter.

// Create Instance of Stepper library
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);

In setup section of code, we set the speed of stepper motor by calling setSpeed() function and initialize the serial communication.

void setup()
{
	// set the speed at 60 rpm:
	myStepper.setSpeed(60);
	// initialize the serial port:
	Serial.begin(9600);
}

In loop section of code, we simply call step() function which turns the motor a specific number of steps at a speed determined by setSpeed() function. Passing a negative number to this function reverses the spinning direction of motor.

void loop() 
{
	// step one revolution in one direction:
	Serial.println("clockwise");
	myStepper.step(stepsPerRevolution);
	delay(500);

	// step one revolution in the other direction:
	Serial.println("counterclockwise");
	myStepper.step(-stepsPerRevolution);
	delay(500);
}