Controlling the stepper motor with L298N motor driver and Arduino (2023)

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.

Controlling the stepper motor with L298N motor driver and Arduino (2)

L298N motor driver chip

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

Controlling the stepper motor with L298N motor driver and Arduino (3)

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:

(Video) Control Position and Speed of Stepper motor with L298N module using Arduino | testing stepper motor

Motor output voltage5V – 35V
Motor output voltage (recommended)7V – 12V
logic input voltage5V – 7V
continuous current per channel2a
Maximum power dissipation25W

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:

Controlling the stepper motor with L298N motor driver and Arduino (4)

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.

Controlling the stepper motor with L298N motor driver and Arduino (5)

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.

(Video) Control Position and Speed of Stepper motor with L298N module using Arduino

Controlling the stepper motor with L298N motor driver and Arduino (6)

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.

Controlling the stepper motor with L298N motor driver and Arduino (7)

The way you pulse these pins will affect the behavior of the motor.

  1. The pulse sequence determines the direction of rotation of the motor.
  2. The frequency of the pulses determines the speed of the motor.
  3. 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.

Controlling the stepper motor with L298N motor driver and Arduino (8)

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.

Controlling the stepper motor with L298N motor driver and Arduino (9)

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.

(Video) How to control Nema17 stepper motor with L298n motor driver.

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.
Controlling the stepper motor with L298N motor driver and Arduino (10)

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.

Controlling the stepper motor with L298N motor driver and Arduino (11)

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.

(Video) L298n Dual H-Bridge Motor Driver : DC Motors : PWM : Stepper Motors : Eye-On-Stuff

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

Videos

1. How to Run Stepper Motor with Arduino using L298N Driver Module
(Circuit Magic)
2. DIY 🚕Stepper Motors control with arduino | L298n Motor Driver +MPU6050 gyroscope with code+diagram
(Dhadam Robotics)
3. How to control DC motor with L298N driver and Arduino
(Circuit Magic)
4. L298N | how to control dc motor with Arduino | Motor speed and direction control
(Project Weekend)
5. Arduino Beginners Getting Started With NEMA 17 Stepper Motor and The L298N Motor Driver Module
(BMonster Laboratory)
6. How to control stepper motor with node mcu using L298n Motor driver
(Passion Tech KLM)

References

Top Articles
Latest Posts
Article information

Author: Lilliana Bartoletti

Last Updated: 20/09/2023

Views: 5940

Rating: 4.2 / 5 (53 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Lilliana Bartoletti

Birthday: 1999-11-18

Address: 58866 Tricia Spurs, North Melvinberg, HI 91346-3774

Phone: +50616620367928

Job: Real-Estate Liaison

Hobby: Graffiti, Astronomy, Handball, Magic, Origami, Fashion, Foreign language learning

Introduction: My name is Lilliana Bartoletti, I am a adventurous, pleasant, shiny, beautiful, handsome, zealous, tasty person who loves writing and wants to share my knowledge and understanding with you.