Control Stepper Motor with L298N Motor Driver & Arduino

4,550 views

Introduction

Stepper motors are utilized in a wide range of applications. Textile machines, printing presses, medical imaging equipment, CNC machines, and a variety of other devices use stepper motors. Stepper motor control, on the other hand, necessitates a complete understanding because each machine must be designed and managed independently. As a result, a variety of driver motors are now available that can better and more precisely control DC motors using microcontrollers such as Arduino. As a result. In this tutorial, we are going to interface “Stepper Motor with L298N Motor Driver with Arduino UNO”.

An L298N Motor Driver IC and a 78M05 Voltage Regulator make up the L298N Motor Driver module. Only when the jumper is inserted will the 78M05 Voltage Regulator be activated. The internal circuitry will be powered by the voltage regulator when the power source is less than or equal to 12V, and the 5V pin can be utilized as an output pin to power the microcontroller. When the power supply is greater than 12V, the jumper should be removed and a separate 5V should be provided through the 5V connector to power the internal circuitry.

Hardware Required

S.noComponentValueQty
1.Arduino UNO1
2.USB Cable Type A to B1
3.Jumper Wires
4.Motor DriverL298N1
5.Adapter12v1
6.Stepper Motor1

Circuit Diagram

Connection Table

ArduinoL298N Motor Driver
VCCVCC
GNDGND
D8IN1
D9IN2
D10IN3
D11IN4

Arduino Code

// 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);
}

Working Explanation

To Control Stepper Motor with L298N & Arduino. Then, using your Arduino IDE, create the provided code and upload it to your Arduino. When you turn on the power to the circuit, the motor begins to rotate in accordance with the commands in the code. For example, it first takes the 200 number steps per revolution clockwise, then it takes 200 number steps per revolution in the anti-clockwise direction.

Code Explanation

  • First, we have included the stepper motor library. After that we assigned the variable stepsPerRevolution to declare the number of steps required per revolution that our motor is rated at. Then we created the  stepper library which uses the motor’s steps per rotation as a parameter, as well as the Arduino pin connections.
  • In the void setup, we call the setSpeed() function to set the speed. Then we initialize the serial port by Serial.begin.
  • In the void loop, we have called the step() function which controls the motor at a certain speed and at prescribed number of steps.

Application and Uses

  • It is used in automated manufacturing equipment in industry.
  • Textile machines, printing presses, medical imaging machines, and CNC machines all employ them.
  • They’re included in automated digital cameras in consumer electronics.