In this tutorial we will learn:
How to use L298N driver to control bipolar stepper motor
How to control the position of the stepper motor
How to control the speed of the stepper motor
How to control the direction of the stepper motor
The tutorial can be applied to any type of bipolar stepper motor (4 wires). The tutorial takes a NEMA 17 stepper motor as an example.
Required Hardware
1 | × | Arduino UNO or Genuino UNO | |
1 | × | USB 2.0-Kabel Typ A/B | |
1 | × | Stepper motor #17 | |
1 | × | L298N motor driver module | |
1 | × | 12V power supply | |
1 | × | DC jack | |
1 | × | jumper wires | |
1 | × | (Optional) 9V power supply for Arduino |
Please note: these are affiliate links. If you purchase the components through these links, we may earn a commission at no additional cost to you. We appreciate it.
About stepper motor
There are two popular types of stepper motors:
bipolar: This motor type has 4 wires
unipolar: This motor type has 5 wires or 6 wires.
For a 6-wire unipolar stepper motor, we can use four wires out of six and control it as a bipolar stepper motor.
For unipolar 5-wire stepper motors seeArduino - Controlling the 28BYJ-48 stepper motor with the ULN2003 driver
This tutorial only focuses on the bipolar stepper motor.
Pinout of the bipolar stepper motor
The pinout of the bipolar stepper motor has 4 pins. Depending on the manufacturer, the pins of the motor have several names. The following table shows some commonly used pin names:
PIN NO | our 1 | we 2 | we are 3 |
---|---|---|---|
1 | A+ | A | A |
2 | A- | A | C |
3 | B+ | B | B |
4 | B- | B | D |
Pin order, wire naming, and wire color may vary by manufacturer. You must read the datasheet or manual to see the association between wire color and pin name. The above picture also shows the specification of two different motors with different wire designation and wire color.
steps per revolution
The specification of the motor gives degrees per step (let's call it DEG_PER_STEP). Depending on the control method, the steps per revolution (let's call STEP_PER_REVOLUTION) are calculated as in the table below:
control method | steps per revolution | Real degrees per step |
---|---|---|
full step | STEP_PER_REVOLUTION = 360 / DEG_PER_STEP | YOU_PER_STEP |
half step | STEP_PER_REVOLUTION = (360 / DEG_PER_STEP) * 2 | DEG_PER_STEP / 2 |
Microstepping (1/n) | STEP_PER_REVOLUTION = (360 / DEG_PER_STEP) * n | YOU_PER_STEP / n |
Example: If the data sheet of the motor states 1.8 deg/step:
control method | steps per revolution | Real degrees per step |
---|---|---|
full step | 200 steps/revolution | 1,8° |
half step | 400 steps/revolution | 0,9° |
Microstepping (1/n) | (200 * n) steps/revolution | (1,8 / n)° |
How to control a stepper motor with Arduino
Arduino can generate signals to control the stepper motor. However, the signals from Arduino do not have enough voltage and/or current that the stepper motor needs. Therefore we need a hardware driver between Arduino and the stepper motor. The driver does two jobs:
Amplify Arduino control signals (current and voltage)
Protect Arduino from high currents and voltages used to power the stepper motor.
There are many types of hardware drivers that can be used to control stepper motors. The L298N driver is one of the most commonly used hardware drivers to control stepper motors.
About the L298N driver
A single L298N driver can be used to control two DC motors or one stepper motor. In this tutorial we will learn how to use it to control the stepper motor.
Pinout of the L298N driver
The L298N driver has 11 pins and three jumpers:
VCC-Pin:supplies power to the motor. It can be anywhere from 5 to 35V.
GND-Pin:is a common ground pin that must be connected toMasse(0V).
5V pin:supplies power for the L298N module. It can be powered with 5V from Arduino.
Pins IN1, IN2, IN3, IN4:are connected to the pins of Arduino to receive the control signal to control the stepper motor.
OUT1, OUT2, OUT3, OUT4 Pins:are connected to the stepper motor.
ENA-, ENB-Jumper:are used to activate the stepper motor. You must leave both the ENA and ENB jumpers in place.
5V-EN-Jumper:
Leaving the 5V-EN jumper in place will keep the power supply for the L298N moduleVCC, we don't need to connect anything to the 5V pin. If we remove the 5V-EN jumper, we need to power the L298N module from a 5V pin
As described above, the L298N driver has two input powers:
One for stepper motor (VCCand GND pins): from 5 to 35 V.
One for the internal operation of the L298N module (5V and GND pins): from 5 to 7V. With the 5V-EN jumper left in place, we don't need to connect this pin to anything.
circuit diagram
This image is created withFritzing. Click image to enlarge
※ NOTE THAT:
Please leave all three jumpers on the L298N module in place (in case motor power supply is ≤12V).
The order of the pins on stepper motors can vary depending on the manufacturer. Please check the table below for correct wiring.
Wiring table between Arduino and L298N driver
Arduino-Stifte | L298N pens |
---|---|
7 | IN 1 |
6 | IN 2 |
5 | IN3 |
4 | IN4 |
Wiring table between L298N driver and stepper motor
Important!:Please don't pay attention to the wiring order of the stepper motor on the above schematic picture. It's just an example. The order of the pins on stepper motors can vary depending on the manufacturer. Make sure your wiring conforms to the table below.
L298N pens | stepper pins | or | or |
---|---|---|---|
OUT1 | A+ | A | A |
OUT2 | A- | A | C |
AUS3 | B+ | B | B |
AUS4 | B- | B | D |
Before purchasing a stepper motor, we suggest you check the stepper motor datasheet, specification or manual. Make sure they provide the mapping between the color and the name of the pin. For example thisstepper motorprovides the mapping as shown below:
Based on this mapping, the wiring table becomes:
L298N pens | Stepper motor pins | Drahtfarbe |
---|---|---|
OUT1 | A | black cable |
OUT2 | C | green wire |
AUS3 | B | red cable |
AUS4 | D | blue wire |
※ NOTE THAT:
In all the above wiring charts between the stepper motor and the L298N driver, we can swap OUT1 with OUT2, OUT3 with OUT4. Therefore, there are several ways to do the wiring. However, if we swap them around, the direction of rotation of the motors can be changed (clockwise to counterclockwise and vice versa).
How to control a stepper motor with an L298N driver.
Controlling a stepper motor is not an easy task, especially if we don't want to control it in a stalling manner. Fortunately thanksAccelStepperLibrary makes controlling the stepper motor child's play.
Arduino IDE also has a built inStepperLibrary. However, we do not recommend you to use this library because:
The library provides the locking function. This means that Arduino is prevented from doing other work while it is controlling the stepper motor.
It doesn't have enough features.
(Video) Control Position and Speed of Stepper motor with L298N module using Arduino | testing stepper motor
Instead, we recommend you to use the AccelStepper library. This library supports:
acceleration
Delay.
Full step and half step driving.
Multiple simultaneous steppers with independent simultaneous stepper on each stepper.
Disadvantage:DO NOT support microstepping
How to control stepper motor position via L298N driver
We can move the stepper motor to the desired position using:
Stepper.move to(Desired position);// Move motor one revolution
※ NOTE THAT:
ThatStepper.move to()Function is non-blocking. This is a great point of the library. However, when using this feature, we must keep the following in mind:
Call stepper.run() as many times as possible. It should be called in thevoid loop()Function.
don't useDelay()Function when the engine is moving.
Should NOT be usedSerial.print()andSerial.println()Function when the engine is moving. These features make the stepper motor slower.
How to control stepper motor speed via L298N driver
We can control not only speed but also acceleration and deceleration using some simple functions.
Stepper.setAcceleration(50,0);// set acceleration/decelerationStepper.set speed(200);// set desired speed
How to control stepper motor direction via L298N driver
If you do the wiring as above, the motor will spin in:
Clockwise:when we control the motor from one position to a higher position (position increment)
counterclockwise:when we control the motor from one position to a lower position (position decrease)
For example:
If the current position is 100 and we control the motor to 200, the motor will spin clockwise
If the current position is -200 and we control the motor to -100, the motor will rotate clockwise
If the current position is 200 and we control the motor to 100, the motor will rotate counterclockwise
If the current position is -100 and we control the motor to -200, the motor will rotate counterclockwise
※ NOTE THAT:
As mentioned earlier, if you swap OUT1 with OUT2 or OUT3 with OUT4, the increment of position can be counter-clockwise and the decrement of position can be clockwise.
How to stop the stepper motor
The stepper motor stops automatically after reaching the desired position.
The stepper motor can be forced to an immediate stop at any timeStepper.halt()Function.
(Video) How to Run Stepper Motor with Arduino using L298N Driver Module
Arduino Code - Stepper Motor Code
The following code does:
Rotate the motor one revolution clockwise
Stop the engine for 5 seconds
Turn the motor back one turn counterclockwise
Stop the engine for 5 seconds
This process is repeated again and again
/** Created by ArduinoGetStarted.com** This sample code is in the public domain** Tutorial page: https://arduinogetstarted.com/tutorials/arduino-controls-stepper-motor-using-l298n-driver*/#contain<AccelStepper.h>#defineYOU_PER_STEP 1.8#defineSTEP_PER_REVOLUTION (360 / DEG_PER_STEP)AccelStepperStepper (AccelStepper::FULL4WIRE, 7, 6, 5, 4);langmoveToPosition = STEP_PER_REVOLUTION;Empty to install() { Serial.Start(9600);Stepper.setAcceleration(200,0);// set accelerationStepper.set speed(200);// Set initial speedStepper.SetCurrentPosition(0);// Set position to 0Stepper.move to(STEP_PER_REVOLUTION);// Move motor one revolution clockwise Serial.println("motor runs clockwise");}Empty Ribbon() { if(Stepper.DistanceToGo() == 0) { Serial.println("Engine is stopped"); delay(5000);// Stop 5 secondsStepper.SetCurrentPosition(0);// Reset position to 0moveToPosition = -1 * moveToPosition;// reverse directionStepper.move to(moveToPosition);// Move motor one revolution if(Stepper.DistanceToGo() > 0) Serial.println("motor runs clockwise"); anders if(Stepper.DistanceToGo() < 0) Serial.println("Motor rotates counter-clockwise");} // Serial.print (F ("Position:")); // Serial.println (stepper.currentPosition());Stepper.run();// MUST be called as often as possible}
Quick steps
In the Arduino IDE, go toToolmanage libraries
Seek„AccelStepper“, then find Mike McCauley's AccelStepper library
clickTo installButton to install the AccelStepper library.
Copy the code above and open it with Arduino IDE
clickuploadButton on Arduino IDE to upload code to Arduino
You'll see:
The stepper motor rotates one revolution clockwise
Stepper motor stops 5 seconds
The stepper motor rotates back one revolution counterclockwise
Stepper motor stops 5 seconds
The above process is repeatedly executed.
Check out the result on Serial Monitor
COM6
Send
Motor runs clockwise Motor stops Motor runs anti-clockwise Motor stops Motor runs clockwise Motor stops Motor runs anti-clockwise Motor stops Motor runs clockwise Motor stops Motor runs anti-clockwise Motor stops
Auto ScrollShow timestamp
clear output
9600 Baud
New line
code explanation
Read the line-by-line explanation in the comment lines of the source code!
video guide
We are considering doing the video tutorials. If you think the video tutorials are essential, please subscribe to oursYoutube Kanalto motivate us to make the videos.
The best Arduino starter kit
Check out the best Arduino kit for beginners
See also
Arduino - stepper motor
Arduino - Controls the 28BYJ-48 stepper motor with the ULN2003 driver
Arduino - stepper motor and limit switch
※ OUR NEWS
We are AVAILABLE. SeeThis is how you entrust us with the creation of your project
If this tutorial is useful for you,Please give us the motivation to create more tutorials.
You can share the link of this tutorial anywhere. However, please do not copy the content to share on other websites. We took a lot of time and effort to create the content of this tutorial, please respect our work!
follow us
Share with your friends!
PREVIOUSLY
NEXT
DISCLOSURE
ArduinoGetStarted.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program that allows websites to earn advertising fees by advertising and linking to Amazon.com, Amazon.it, Amazon.fr, Amazon.co. uk, Amazon.ca, Amazon.de, Amazon.es and Amazon.co.jp
Copyright © 2018-2022 ArduinoGetStarted.com. All rights reserved.
Terms and Conditions|Privacy Policy
E-Mail: ArduinoGetStarted@gmail.com