Arduino - Controls the stepper motor with the L298N driver | Arduino tutorial (2023)

Ads from ArduinoGetStarted.com

Arduino - Controls the stepper motor with the L298N driver | Arduino tutorial (1)

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.

Arduino - Controls the stepper motor with the L298N driver | Arduino tutorial (2)

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 NOour 1we 2we are 3
1A+AA
2A- AC
3B+BB
4B- BD

Arduino - Controls the stepper motor with the L298N driver | Arduino tutorial (3)

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 methodsteps per revolutionReal degrees per step
full stepSTEP_PER_REVOLUTION = 360 / DEG_PER_STEPYOU_PER_STEP
half stepSTEP_PER_REVOLUTION = (360 / DEG_PER_STEP) * 2DEG_PER_STEP / 2
Microstepping (1/n)STEP_PER_REVOLUTION = (360 / DEG_PER_STEP) * nYOU_PER_STEP / n

Example: If the data sheet of the motor states 1.8 deg/step:

control methodsteps per revolutionReal degrees per step
full step200 steps/revolution1,8°
half step400 steps/revolution0,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.

Arduino - Controls the stepper motor with the L298N driver | Arduino tutorial (4)

Pinout of the L298N driver

Arduino - Controls the stepper motor with the L298N driver | Arduino tutorial (5)

(Video) Control Position and Speed of Stepper motor with L298N module using Arduino

The L298N driver has 11 pins and three jumpers:

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

Arduino - Controls the stepper motor with the L298N driver | Arduino tutorial (6)

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-StifteL298N pens
7IN 1
6IN 2
5IN3
4IN4

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 pensstepper pinsoror
OUT1A+AA
OUT2A- AC
AUS3B+BB
AUS4B- BD

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:

Arduino - Controls the stepper motor with the L298N driver | Arduino tutorial (7)

Based on this mapping, the wiring table becomes:

L298N pensStepper motor pinsDrahtfarbe
OUT1Ablack cable
OUT2Cgreen wire
AUS3Bred cable
AUS4Dblue 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

Arduino - Controls the stepper motor with the L298N driver | Arduino tutorial (8)

  • Seek„AccelStepper“, then find Mike McCauley's AccelStepper library

  • clickTo installButton to install the AccelStepper library.

Arduino - Controls the stepper motor with the L298N driver | Arduino tutorial (9)

  • 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

(Video) How to control Nema17 stepper motor with L298n motor driver.

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

(Video) L298n Dual H-Bridge Motor Driver : DC Motors : PWM : Stepper Motors : Eye-On-Stuff

E-Mail: ArduinoGetStarted@gmail.com

Videos

1. NEMA 17 Stepper Motor Control using L298N Arduino tutorial
(Maker Tutor)
2. Controlling DC Motors with the L298N H Bridge and Arduino
(DroneBot Workshop)
3. How to control DC motor with L298N driver and Arduino
(Circuit Magic)
4. Arduino stepper motor control with L298N
(tronixstuff)
5. L298N | how to control dc motor with Arduino | Motor speed and direction control
(Project Weekend)
6. cd rom stepper motor Arduino L298n + Joystick controlled speed and direction Control
(Electronic Clinic)

References

Top Articles
Latest Posts
Article information

Author: Msgr. Benton Quitzon

Last Updated: 15/07/2023

Views: 6157

Rating: 4.2 / 5 (63 voted)

Reviews: 94% of readers found this page helpful

Author information

Name: Msgr. Benton Quitzon

Birthday: 2001-08-13

Address: 96487 Kris Cliff, Teresiafurt, WI 95201

Phone: +9418513585781

Job: Senior Designer

Hobby: Calligraphy, Rowing, Vacation, Geocaching, Web surfing, Electronics, Electronics

Introduction: My name is Msgr. Benton Quitzon, I am a comfortable, charming, thankful, happy, adventurous, handsome, precious person who loves writing and wants to share my knowledge and understanding with you.