Controlling a 4-Wire Stepper Motor with an L298N Module

Video thumbnail for E181 Control Position and  Speed of Stepper motor with L298N module using Arduino RJT181

Controlling a 4-Wire Stepper Motor with an L298N Module

This detailed tutorial shows how to control a stepper motor using the L298N dual H-Bridge driver and an Arduino board. You’ll learn how to rotate the motor clockwise and counterclockwise, adjust speed, and set position using a potentiometer. Several code examples are provided for practical applications like single-step motion and knob-based control.

All wiring diagrams and Arduino code examples used in the video are available for download below this article.

🧾 Introduction

This tutorial uses a 28BYJ-48 or similar stepper motor controlled through an L298N motor driver module. The L298N supports two DC motors or one stepper motor and is ideal for robotics and motion-based projects where bidirectional and speed control are required.

🔧 Hardware Components Explained

The video explains the following key components:

  • L298N driver module: Converts Arduino signals into motor-driving voltage/current

  • Stepper motor: Operates in precise steps for controlled motion

  • Potentiometer: Used for controlling speed or position

  • Arduino Uno: Controls everything with custom logic via uploaded sketches

The explanation covers pin functions, motor connections, and power input options.


🔌 Wiring the Circuit

The motor wires are connected to the OUT1 to OUT4 pins on the L298N. The IN1 to IN4 pins on the module connect to the Arduino’s digital pins. A potentiometer is connected to analog input (A0 or similar) for controlling speed or position.


💻 Code Example – Stepper Motor Control Using L298N and Arduino

The following Arduino sketch demonstrates how to rotate a stepper motor clockwise and counterclockwise using the L298N motor driver and the built-in Stepper library.

cppCopyEdit#include <Stepper.h>

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

// Define motor control pins connected to L298N IN1–IN4
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);

  • stepsPerRevolution should match the specifications of your stepper motor (commonly 200 for NEMA 17 motors).

  • Pins 8–11 on the Arduino are connected to the L298N input pins (IN1–IN4).

cppCopyEditvoid setup() {
  myStepper.setSpeed(300);     // Set motor speed in RPM
  Serial.begin(9600);          // Start serial communication

  // Enable L298N motor channels
  pinMode(2, OUTPUT);          
  digitalWrite(2, HIGH);       // EN1 enabled
  pinMode(3, OUTPUT);
  digitalWrite(3, HIGH);       // EN2 enabled
}

  • setSpeed(300) sets the motor speed to 300 RPM (you can adjust this).

  • EN1 and EN2 pins (connected to Arduino pins 2 and 3) are enabled to activate both motor channels on the L298N.

cppCopyEditvoid loop() {
  Serial.println("clockwise");
  myStepper.step(stepsPerRevolution);   // Rotate one full revolution clockwise
  delay(500);

  Serial.println("counterclockwise");
  myStepper.step(-stepsPerRevolution);  // Rotate one full revolution counterclockwise
  delay(500);
}

  • The motor performs one full revolution in the clockwise direction.

  • Then, it reverses and performs a full revolution counterclockwise.

  • A 500 ms delay is placed between the two motions for observation.

This code demonstrates how to alternate the direction of the stepper motor continuously, making it ideal for beginners learning bidirectional motion control.


🎬 Video Chapters

  • 00:00 – Start

  • 00:52 – Introduction

  • 01:51 – Hardware Explained

  • 10:06 – Wiring Explained

  • 13:27 – Code Explained (1)

  • 19:06 – Code Example (One step at a time)

  • 19:47 – Code Example (Motor knob: position)

  • 26:15 – Code Example (Motor knob: speed)

📥 Download Section


You’ll find all the code samples, wiring diagrams, and component links below this article. This tutorial is ideal for students, engineers, and hobbyists exploring stepper motor applications in embedded systems.



Code Snippets

Comments will be displayed here.