Using an L298N Motor Driver to Control a 4-Wire Stepper Motor

Using an L298N Motor Driver to Control a 4-Wire Stepper Motor

In this tutorial, we will explore how to control a 4-wire stepper motor using the L298N motor driver in conjunction with an Arduino. The L298N is a versatile dual H-bridge driver that allows us to control the direction and speed of stepper motors effectively. By the end of this guide, you will have a functional setup that can rotate the stepper motor in both directions and control its speed using a potentiometer.

L298N motor driver (yellow connector)

To help you understand the process better, I recommend watching the associated video, which provides a visual demonstration of the wiring and coding (in video at 01:30).

Hardware Explained

The L298N motor driver is designed to control motors by allowing you to drive them in both forward and reverse directions. It uses an H-bridge configuration, which means it can switch the polarity of the voltage applied to the motor, thus reversing its direction. This driver can handle a supply voltage of up to 46V and a continuous current of 2A per channel, making it suitable for many applications.

In our setup, we will use four control pins to manage the stepper motor. The key pins are ENA and ENB, which must be set to HIGH (5V) to enable the driver. The pins IN1, IN2, IN3, and IN4 will control the motor's winding states, allowing it to step in the desired direction.

Datasheet Details

ManufacturerSTMicroelectronics
Part numberL298N
Logic/IO voltage5 V
Supply voltage5–46 V (VS)
Output current (per channel)2 A max/channel (continuous)
Peak current (per channel)3 A max
PWM frequency guidance1 kHz to 20 kHz
Input logic thresholds0.8 V (low), 2.0 V (high)
Voltage drop / RDS(on) / saturation1.5 V max
Thermal limits150 °C max
PackageDIP-15
Notes / variantsAlso available in flat versions (L298P)

  • Ensure ENA and ENB are connected to 5V for proper operation.
  • Use adequate heat sinking for high-current applications to prevent overheating.
  • Maintain common ground between the Arduino and the motor driver.
  • Be cautious of wiring polarity to avoid damaging the motor or driver.
  • Implement decoupling capacitors near the power pins to filter noise.
  • Adjust PWM frequency based on motor specifications for optimal performance.

Wiring Instructions

L298N motor driver (yellow connector)
L298N motor driver (yellow connector)

To wire the L298N motor driver to the Arduino and stepper motor, start by connecting the power supply. Connect the VIN pin on the L298N to your 12V power source. Next, connect the GND pin on the L298N to the ground of your power supply and also to the Arduino ground.

Now, for the control pins, connect ENA to pin 2 on the Arduino and ENB to pin 3. Next, connect IN1 to pin 8, IN2 to pin 9, IN3 to pin 10, and IN4 to pin 11 on the Arduino. Finally, connect the stepper motor's wires to the output terminals of the L298N. Ensure that the two pairs of wires from the motor are connected correctly to the outputs.

Code Examples & Walkthrough

In the code, we first include the Stepper.h library to simplify control of the stepper motor. We define the total number of steps per revolution, which is crucial for accurate movement. The key identifiers in the code include stepsPerRevolution, which sets the number of steps needed for a full rotation, and myStepper, which initializes the stepper object with the specified pins.

const int stepsPerRevolution = 200;  // change this to fit the number of steps per revolution
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11); // initialize the stepper library

This excerpt initializes the stepper motor with the appropriate pins. In the setup() function, we set the speed of the motor and enable the driver by setting pins 2 and 3 to HIGH.

void setup() {
  myStepper.setSpeed(300); // set the speed at 60 rpm
  pinMode(2,OUTPUT); // for EN1
  digitalWrite(2,HIGH); // enable EN1
  pinMode(3,OUTPUT); // for EN2
  digitalWrite(3,HIGH); // enable EN2
}

In the loop() function, the motor steps one revolution in both clockwise and counterclockwise directions with a delay in between. This allows you to observe the motor's operation.

void loop() {
  myStepper.step(stepsPerRevolution); // step one revolution in one direction
  delay(500); // wait half a second
  myStepper.step(-stepsPerRevolution); // step one revolution in the other direction
  delay(500); // wait half a second
}

This excerpt demonstrates how to control the motor to rotate in both directions. The full code loads below the article for your reference.

Demonstration / What to Expect

Once everything is wired correctly and the code is uploaded, you should see the stepper motor rotating one full revolution in one direction, followed by a full revolution in the opposite direction. Additionally, if you incorporate the potentiometer for speed control, adjusting it will change the rotation speed of the motor. Be mindful of the motor's current; as you increase speed, the motor may draw more current, which can lead to overheating if not managed properly (in video at 09:45).

If the motor does not rotate as expected, check the wiring to ensure that all connections are secure and correctly oriented. Also, verify that the power supply voltage is appropriate for your stepper motor.

Video Timestamps

  • 00:00 - Introduction
  • 01:30 - Wiring Explanation
  • 05:00 - Code Overview
  • 09:45 - Demonstration of Motor Control

Images

L298N motor driver (yellow connector)
L298N motor driver (yellow connector)
L298N motor driver (yellow connector)
L298N motor driver (yellow connector)
L298N motor driver (yellow connector)
L298N motor driver (yellow connector)
Nema17_stpper_motor-1
Nema17_stpper_motor-1
Nema17_stpper_motor-2
Nema17_stpper_motor-2
94-Code for One Revolution
Language: C++
95-Code for rotating a stepper motor one step at a time
Language: C++
Copied!

Resources & references

No resources yet.

Files📁

No files available.