Post Top Ad

Wednesday, April 12, 2023

How To Make a Stepper Motor Speed Controller using Arduino and L298 Motor Driver



 Stepper Motor Control with L298N Motor Driver and Arduino

In this user guide, we will learn how to control a stepper motor using the L298N Motor Driver with Arduino. This is a quick guide where we will learn how to interface L298N motor driver with Arduino board and eventually learn how to control bipolar stepper motors in our case NEMA 17 with it. We will show you an Arduino sketch that will control the speed and direction of bipolar stepper motors (NEMA 17) every easily.

Stepper Motors Introduction

Stepper motors are DC brushless and synchronous motors. They rotate in discrete steps of predefined values and are able to rotate both clockwise and anticlockwise. Unlike other DC motors, they provide a precise position control according to the number of steps per revolution for which the motor is designed. That means a complete revolution of a stepper motor is divided into a discrete number of steps. They are commonly used in CNC machines, Robotics, 2D and 3D printers.

NEMA17 Stepper Motor

NEMA17 is a bipolar stepper motor rated at 12V with 200 steps per revolution and 60 rpm speed. In NEMA 17 all pins are connected internally with the coil. To make the movement we need to magnetize the coil. The rotation of the motor requires the magnetic field to make a single step. The time required to make the coil fully magnetic depends on the induction of the coil.


Internally to control the stepper motor we will have to use the green and black pair. The second pair will be of red and blue.

The table below shows the different ways to differentiate the wires for our stepper motor. This is important to know when connecting the stepper motor with the L298N Motor Driver outputs. You need to refer to the datasheet of your stepper motor to be sure as different manufacturers use different configurations.


Its rated voltage is 12V

Phase current is 2.2A

The Holding torque is equal to 40N.cm

One step angle will be of 1.8 Deg.

Total steps for each resolution will be 200.

4-wire and 8-inch lead

Number of phases are 4

Total inductance by each phase will be 2.8 mH

The resistance of the coil is 1.5 Ohm per coil.

L298N Motor Driver Module

The L298N motor driver module is very easy to use with Arduino and relatively inexpensive as well. It is widely used in controlling robots as we can connect up to four motors at once but if we want to control the speed and direction as well then it allows two dc motors or a single stepper motor to be connected. Thus, it is perfect for two-wheeled robots. This module is mainly used in robotics and in controlling dc and stepping motors.


The L298N motor driver module consists of an L298N motor driver IC, 78M05 5V regulator, 5V jumper enable, power LED, heat sink, resistors, and capacitors all combined in an integrated circuit. The diagram below shows all the components consisting inside the module.

The L298N Motor driver IC is powerfully built with a big heat sink. It is a dual-channel H bridge motor driver which can be easily used to drive one single stepper motor.


The module also has a 78M05 5V regulator which is enabled through a jumper. Keeping the jumper intact, means the 5V regulator is enabled. If the motor power supply is less than 12V then we will power the module through the voltage regulator. The 5V pin in this case acts as an output to power the microcontroller. If the power supply is more than 12V, make sure the jumper is not intact and supply 5V power through the pin separately.

Interface L298N DC Motor Driver with Arduino and NEMA 17 Stepper Motor

We will require the following equipment.


Required Equipment

Arduino UNO

L289N Motor driver Module

NEMA 17 stepper motor

External 12 V power supply

Connecting Wires

Assemble the circuit as shown in the connection diagram below.

For which ever stepper motor you are using refer to its datasheet to find the correct coloured wires to differentiate the A+, A-, B+ and B- wires. This will enable us to properly connect the motor with the OUT1, OUT2, OUT3 and OUT4 pins of the driver module. Connect the stepper motor’s coloured wires with correct output pins of the driver module. In our case, black wire is A+ and green wire is A- hence they will connect with OUT1 and OUT2 respectively. Likewise, the red wire is B+ and the blue wire is B- hence they will connect with OUT3 and OUT4 respectively.


Connect positive terminal of 12V power supply with 12V terminal and negative terminal of power supply with common ground.


We are keeping the 5V Enable jumper in its place as it will power up the L298N motor driver. Then, we have connected the input pins IN1, IN2, IN3 and IN4 with digital pins of the Arduino UNO. We have used GPIO9, GPIO8, GPIO7 and GPIO6 respectively to connect with each of the input pins of the motor driver.


The enable pins ENA and ENB jumpers are not removed. This will ensure that the stepper motor stays enabled.

Arduino Sketch: Controlling Stepper Motor using L298N Motor Driver

Open your Arduino IDE and go to File > New. A new file will open. Copy the code given below in that file and save it.

How the Code Works?

Firstly, we will include the Stepper.h library. This library provides useful functions that make it easy to control the stepper motor.

The next step is to define the steps per revolution. This is the number of steps our motor requires to move one complete revolution. If using NEMA 17 set the value to 200. If using 28BYJ-48 instead set the value to 48.


Next, we will define the input pins of the motor connections with the Arduino board. As you can see we have used the digital pins 9,8,7 and 6 to connect with IN1, IN2, IN3 and IN4 respectively. However, you can use any other suitable Arduino digital pins as well.


Now we will create an instance of the Stepper library called motor() and pass the steps per revolution and the individual motor input pins as arguments. Make sure you specify the input pins in their correct sequence. It is IN1, IN3, IN2, and IN4 for the stepper motor.


Inside the setup() function, Serial.begin() is used to establish the serial connection between the development board at a baud rate of 115200. We will use the setSpeed() method on the motor instance and pass the speed of the motor in rpm as an argument inside it. In our case we are setting the stepper motor speed to 60 revolutions per minute.


Inside the loop() function, we will first rotate the motor clockwise by using the step() method and passing the steps per revolution as the argument inside it. Hence the motor will rotate at steps of 200 per revolution. Likewise, to rotate the motor anti-clockwise we will pass the steps per revolution with a negative sign inside the step() method. Between the two types of rotations we will have a delay of 0.5 second. Additionally, we will print the type of rotation in the serial monitor as well.


This basic sketch will show us how to control a stepper motor’s speed and direction of rotation using the L298N motor driver.



 Stepper Motor Control with L298N Motor Driver and Arduino

In this user guide, we will learn how to control a stepper motor using the L298N Motor Driver with Arduino. This is a quick guide where we will learn how to interface L298N motor driver with Arduino board and eventually learn how to control bipolar stepper motors in our case NEMA 17 with it. We will show you an Arduino sketch that will control the speed and direction of bipolar stepper motors (NEMA 17) every easily.

Stepper Motors Introduction

Stepper motors are DC brushless and synchronous motors. They rotate in discrete steps of predefined values and are able to rotate both clockwise and anticlockwise. Unlike other DC motors, they provide a precise position control according to the number of steps per revolution for which the motor is designed. That means a complete revolution of a stepper motor is divided into a discrete number of steps. They are commonly used in CNC machines, Robotics, 2D and 3D printers.

NEMA17 Stepper Motor

NEMA17 is a bipolar stepper motor rated at 12V with 200 steps per revolution and 60 rpm speed. In NEMA 17 all pins are connected internally with the coil. To make the movement we need to magnetize the coil. The rotation of the motor requires the magnetic field to make a single step. The time required to make the coil fully magnetic depends on the induction of the coil.


Internally to control the stepper motor we will have to use the green and black pair. The second pair will be of red and blue.

The table below shows the different ways to differentiate the wires for our stepper motor. This is important to know when connecting the stepper motor with the L298N Motor Driver outputs. You need to refer to the datasheet of your stepper motor to be sure as different manufacturers use different configurations.


Its rated voltage is 12V

Phase current is 2.2A

The Holding torque is equal to 40N.cm

One step angle will be of 1.8 Deg.

Total steps for each resolution will be 200.

4-wire and 8-inch lead

Number of phases are 4

Total inductance by each phase will be 2.8 mH

The resistance of the coil is 1.5 Ohm per coil.

L298N Motor Driver Module

The L298N motor driver module is very easy to use with Arduino and relatively inexpensive as well. It is widely used in controlling robots as we can connect up to four motors at once but if we want to control the speed and direction as well then it allows two dc motors or a single stepper motor to be connected. Thus, it is perfect for two-wheeled robots. This module is mainly used in robotics and in controlling dc and stepping motors.


The L298N motor driver module consists of an L298N motor driver IC, 78M05 5V regulator, 5V jumper enable, power LED, heat sink, resistors, and capacitors all combined in an integrated circuit. The diagram below shows all the components consisting inside the module.

The L298N Motor driver IC is powerfully built with a big heat sink. It is a dual-channel H bridge motor driver which can be easily used to drive one single stepper motor.


The module also has a 78M05 5V regulator which is enabled through a jumper. Keeping the jumper intact, means the 5V regulator is enabled. If the motor power supply is less than 12V then we will power the module through the voltage regulator. The 5V pin in this case acts as an output to power the microcontroller. If the power supply is more than 12V, make sure the jumper is not intact and supply 5V power through the pin separately.

Interface L298N DC Motor Driver with Arduino and NEMA 17 Stepper Motor

We will require the following equipment.


Required Equipment

Arduino UNO

L289N Motor driver Module

NEMA 17 stepper motor

External 12 V power supply

Connecting Wires

Assemble the circuit as shown in the connection diagram below.

For which ever stepper motor you are using refer to its datasheet to find the correct coloured wires to differentiate the A+, A-, B+ and B- wires. This will enable us to properly connect the motor with the OUT1, OUT2, OUT3 and OUT4 pins of the driver module. Connect the stepper motor’s coloured wires with correct output pins of the driver module. In our case, black wire is A+ and green wire is A- hence they will connect with OUT1 and OUT2 respectively. Likewise, the red wire is B+ and the blue wire is B- hence they will connect with OUT3 and OUT4 respectively.


Connect positive terminal of 12V power supply with 12V terminal and negative terminal of power supply with common ground.


We are keeping the 5V Enable jumper in its place as it will power up the L298N motor driver. Then, we have connected the input pins IN1, IN2, IN3 and IN4 with digital pins of the Arduino UNO. We have used GPIO9, GPIO8, GPIO7 and GPIO6 respectively to connect with each of the input pins of the motor driver.


The enable pins ENA and ENB jumpers are not removed. This will ensure that the stepper motor stays enabled.

Arduino Sketch: Controlling Stepper Motor using L298N Motor Driver

Open your Arduino IDE and go to File > New. A new file will open. Copy the code given below in that file and save it.

How the Code Works?

Firstly, we will include the Stepper.h library. This library provides useful functions that make it easy to control the stepper motor.

The next step is to define the steps per revolution. This is the number of steps our motor requires to move one complete revolution. If using NEMA 17 set the value to 200. If using 28BYJ-48 instead set the value to 48.


Next, we will define the input pins of the motor connections with the Arduino board. As you can see we have used the digital pins 9,8,7 and 6 to connect with IN1, IN2, IN3 and IN4 respectively. However, you can use any other suitable Arduino digital pins as well.


Now we will create an instance of the Stepper library called motor() and pass the steps per revolution and the individual motor input pins as arguments. Make sure you specify the input pins in their correct sequence. It is IN1, IN3, IN2, and IN4 for the stepper motor.


Inside the setup() function, Serial.begin() is used to establish the serial connection between the development board at a baud rate of 115200. We will use the setSpeed() method on the motor instance and pass the speed of the motor in rpm as an argument inside it. In our case we are setting the stepper motor speed to 60 revolutions per minute.


Inside the loop() function, we will first rotate the motor clockwise by using the step() method and passing the steps per revolution as the argument inside it. Hence the motor will rotate at steps of 200 per revolution. Likewise, to rotate the motor anti-clockwise we will pass the steps per revolution with a negative sign inside the step() method. Between the two types of rotations we will have a delay of 0.5 second. Additionally, we will print the type of rotation in the serial monitor as well.


This basic sketch will show us how to control a stepper motor’s speed and direction of rotation using the L298N motor driver.

No comments:

Post a Comment

Post Top Ad

Pages