When you plan to assemble your new robot, at some point you will want to learn how to control stepper motors. The easiest and cheapest way to control stepper motors is to use the L298N motor driver. It can control both the speed and direction of rotation of any small to medium sized bipolar stepper motor such as the NEMA 17.
If you want to control multiple stepper motors, it is recommended to use a standalone dedicated stepper driver like the A4988. If you want to learn more about it, check out this tutorial.
Controlling a stepper motor with an H-bridge
The L298N module has two H-bridges. Each H-bridge drives one of the electromagnetic coils of a stepper motor.
By energizing these electromagnetic coils in a specific order, the stepper shaft can be precisely moved forward or backward in small increments.
However, the speed of the motor is determined by how often these coils are energized.
The animation below shows how H-bridges drive a stepper motor.

L298N motor driver chip
At the center of the module is a large black chip with a chunky heatsink - the L298N from ST Semiconductor.

Inside the L298N chip you'll find two standard H-bridges that can drive a pair of DC motors or a single stepper motor.
The L298N motor driver has a supply range of 5V to 35V and can supply 2A continuous current per coil, so it works very well with most of our stepper motors.
Technical specifications
Here are the specifications:
Motor output voltage | 5V – 35V |
Motor output voltage (recommended) | 7V – 12V |
logic input voltage | 5V – 7V |
continuous current per channel | 2a |
Maximum power dissipation | 25W |
Please refer to the datasheet below for more details.
Pinout of the L298N motor driver module
The L298N module has a total of 11 pins that connect it to the outside world. The pens are as follows:

Let's get acquainted with all the pins one by one.
Power-Pins
The L298N motor driver module is powered via a 3-pin 3.5mm screw terminal block.

The L298N motor driver actually has two input power pins - VS and VSS.
VSPin supplies power to the IC's internal H-bridges to drive the motor. You can connect any input voltage between 5 and 12 V to this pin.
VSSis used to drive the logic circuitry inside the L298N IC, which can be 5 to 7V.
Masseis the common ground pin.
output pins
The output channels of the L298N motor driverOUT1,OUT2,AUS3andAUS4are broken out with two screw terminals in a 3.5 mm grid up to the edge of the module. You can connect any 12-24V stepper motor to these terminals.

Each channel of the module can supply up to 2A to the stepper motor. However, the amount of current supplied to the motor depends on the power supply of the system.
control pins
Using the four control pinsIN 1,IN 2,IN3andIN4you can control both the speed and the direction of rotation of the stepper motor. These pins actually control the H-bridge circuit switches inside the L298N chip.

The way you pulse these pins will affect the behavior of the motor.
- The pulse sequence determines the direction of rotation of the motor.
- The frequency of the pulses determines the speed of the motor.
- The number of pulses determines how far the motor turns.
Enable Pens
Die Enable-PinsThisandENBare used to enable or disable the motor independently of the input signals.

Pulling these pins HIGH enables the motor, while pulling them LOW disables the motor.
The module usually ships with jumpers on these pins. If the jumpers are set, the motor is enabled. If you want to programmatically control the motor, you'll need to remove the jumpers and connect these pins to the digital pins on the Arduino.
Integrated 5V regulator and jumper
The module has an integrated 5V regulator - 78M05. It can be enabled or disabled via a jumper.

When this jumper is present, the 5V regulator is enabled, which derives the logic power supply (VSS) from the motor power supply (VS). In this case, the 5V input terminal (VSS) acts as an output pin and supplies 5V 0.5A. You can use it to power your Arduino or other circuits that need a 5V power supply.
If the jumper is removed, the 5V regulator is disabled and we need to supply 5V separately through the VSS pin.
Warning:
You can leave the jumper in place if the motor power supply is less than 12V. If it is higher than 12V, you must remove the jumper to avoid damaging the onboard 5V regulator.
Also, DO NOT supply power to the VSS and VS pins while the jumper is in place.
How to identify the phases of a bipolar stepper motor?
Before you start connecting the motor to the module, you need to identify the phases of the motor you want to use. The best way to do this is to check the motor's datasheet.
If you can't find the datasheet, use the following trick.
- Put your multimeter in resistance mode and simply measure wire pairs for resistance.
- If the resistance is only a few ohms (<100Ω) then you have a pair.
- The other two wires should form the second pair.

Wiring a bipolar stepper motor to the L298N module and Arduino
Let's start by connecting the power supply to the module. In our experiment we use a NEMA 17 bipolar stepper with a nominal voltage of 12V. Therefore we connect the external 12V power supply to the VS pin.
Next we need to supply 5V to the logic circuit of the L298N. We're going to use the onboard 5V regulator to derive 5V from the motor power supply, so leave the 5V EN jumper in place.
You also need to leave the ENA and ENB jumpers in place so the motor is always enabled.
Now connect the input pins (IN1, IN2, IN3, and IN4) of the L298N module to the four Arduino digital output pins (8, 9, 10, and 11).
Finally, connect one phase of the motor to terminal A (OUT1 and OUT2) and the other phase to terminal B (OUT3 and OUT4). Polarity doesn't matter.
The following image shows how everything is wired.

Arduino Code - Controlling the NEMA 17 Stepper Motor
Here is the simple sketch that makes the stepper motor spin clockwise at 60rpm and then counterclockwise.
This sketch will give you a complete understanding of how to control a bipolar stepper motor like NEMA 17 with L298N motor driver and can serve as a basis for more practical experiments and projects.
// Add the Arduino Stepper library#contain<Stepper.h>// Number of steps per output revolutionart intsteps per revolution =200;// Create an instance of the stepper librarySteppermy stepper(steps per revolution,8,9,10,11);Empty to install(){// set the speed to 60 rpm:my stepper.set speed(60);// initialize serial port:Serial.Start(9600);}Empty Ribbon() {// step one turn in one direction:Serial.println("clockwise");my stepper.Step(steps per revolution);delay(500);// step one turn in the other direction:Serial.println("counterclockwise");my stepper.Step(-stepsPerRevolution);delay(500);}
Code Explanation:
The sketch begins by including theArduino stepper library. The stepper library comes with the Arduino IDE and takes care of sequencing the pulses that are sent to the motor.
// Add the Arduino Stepper library#contain<Stepper.h>
After including the library, we define a variable calledsteps per revolution
. As the name suggests, this is the number of steps per revolution your motor is rated for. In our case it is 200.
// Number of steps per output revolutionart intsteps per revolution =200;
Next, let's create a stepper library object. The builder ofStepper
The class takes the steps per revolution of the motor and Arduino pin connections as arguments.
// Create an instance of the stepper librarySteppermy stepper(steps per revolution,8,9,10,11);
In the setup section of the code, we set the speed of the stepper motor by calling theset speed()
Function and initialize the serial communication.
Empty to install(){// set the speed to 60 rpm:my stepper.set speed(60);// initialize serial port:Serial.Start(9600);}
Finally, in the loop section of the code, we simply call theStep()
Function that makes the motor rotate a certain number of steps at a speed set by theset speed()
Function. Passing a negative number to this function reverses the direction of rotation of the motor.
Empty Ribbon() {// step one turn in one direction:Serial.println("clockwise");my stepper.Step(steps per revolution);delay(500);// step one turn in the other direction:Serial.println("counterclockwise");my stepper.Step(-stepsPerRevolution);delay(500);}
Please note thatStep()
is a blocking function. This means waiting for the motor to finish moving to pass control to the next line in your sketch. For example if you set the speed to say 1rpm and callstep (100)
For a 100 step motor, this function takes a full minute.