OVERVIEW
I’ve done quite a few tutorial about controlling stepper motors with an Arduino.
And looking back, I used the EasyDriver motor driver in almost ‘All’ of them…
So today I’m gonna show you how you can control a Stepper Motor with an Arduino using something else.
I’ve used the L298N motor driver before for DC motors, but you can hookup a Stepper Motor to it as well, so let’s see if using the L298N is a good option.
PARTS USED
Arduino UNO
L298N Motor Driver
Analog Slider
These are Amazon affiliate links...
They don't cost you anything and it helps me keep the lights on
if you buy something on Amazon. Thank you!
WHAT ABOUT THE L298N?
For simple projects or just to test Stepper Motors, the L298N might be a good choice.
At first glance, when looking at the L298N driver, you see it comes pre-installed with a pretty big heatsink.
You might be thinking “well, that’s better than the EasyDriver”
Well yes and no, you see the L298N is not very efficient when it comes to power, and that’s why it needs that heatsink…
The EasyDriver is better so it can get away without one, within limits of course.
Keep in mind that the L298N doesn’t have all the bells and whistles that more advanced driver have.
For example it only supports Full Steps on it’s own, but will support Half Steps when using the AccelStepper Arduino Library.
The L298N is easy to find, very cheap, but the big plus for me is that it’s much easier to connect.
You see the L298N comes with Screw Terminals already soldered on for connection the Stepper Motor wires and power supply lines very easily.
Also the pins to connect to an Arduino are already there and ready to go.
The L298N can take from 5V to 35V… It also has a 5V regulator onboard, but unlike the EasyDriver, this one will only work up to 12V input, if you supply over 12V to the L298N you need to disable the onboard regulator with a jumper, and provide your own 5V supply for the onboard electronics and the Arduino.
Now on paper the L298N can provide up to 2A of power per coil, but in reality it will go into thermal shutdown before getting even close to that because of the bad efficiency.
600 mA per phase/coil is about the max you can achieve using the standard heatsink.
In this tutorial I’m using a 0.375A per coil stepper motor and it got very very hot just using it for a minute or two. So in reality it’s not much better than the EasyDriver.
That’s another problem of the L298N, it can’t limit the amount of current that gets fed to the stepper motor, it will always supply the maximum current that the stepper motor is rated for, so if using it, you need to turn off the output when not moving to avoid overheating.
With all this said, the L298N still works to drive small stepper motors with an Arduino.
So although it’s not the most recent and efficient driver on the market, it might be fun to give it a try.
CONNECTION DIAGRAM
Here’s the connection that are used in this tutorial:
I’m using a 12V 2A power supply for the L298N and powering the Arduino using the 5V output pin.
“Make sure to not go over 12 volts, since the L298N will not provide 5V when you supply more than 12V”
Arduino pins 8,9,10,11 are connected to the L298N pins IN1-2-3-4
The 5V output and GND pins of the L298N are connected to the VIN and GND pins of the Arduino to power it up
The 5V and GND pins of the Arduino are connected to the V and GND pins of the Analog Slider
The output pin of the Analog slider is connected to the A0 pin of the Arduino
We are using a 12V 1.5A power supply to power the L298N
THE CODE
In this tutorial I’m using an Analog Slider module to move and control the speed of the Stepper Motor.
Moving the slider slowly to the left will start moving the stepper to the left at a slow speed and the speed will increase the more I moved the Analog Slider.
Moving the slider to the right will do the same but in the opposite direction.
Sliding near the middle will stop the Stepper Motor.
I also disable the outputs of the L298N when not moving to reduce overheating.
As always check out the tutorial video for more information.
/* Control Stepper Motor with L298N and Analog Slider
Created by Yvan / https://Brainy-Bits.com
This code is in the public domain...
You can: copy it, use it, modify it, share it or just plain ignore it!
Thx!
*/
#include <AccelStepper.h> // AccelStepper library by Mike McCauley : https://www.arduinolibraries.info/libraries/accel-stepper
#define StepperDriverType 4 // 4 Wires Stepper Motor interface
AccelStepper stepper = AccelStepper(StepperDriverType, 8, 9, 10, 11); // Arduino Pins 8,9,10,11 connected to OUT 1,2,3,4 on L298N
#define Slider_Pin A0 // Arduino A0 Pin connected to the analog out of the Slider
int Slider_Value; // Used to save the current Analog Slider Value
int Stepper_Speed; // Used to set the travel speed of the stepper motor
int Stepper_Direction; // Used to choose the direction of travel
void setup() {
Stepper_Direction=stepper.currentPosition(); // Set the starting position of the stepper which is equal to zero at startup
}
void loop() {
Slider_Value=analogRead(Slider_Pin); // Read the value of the analog slider
if (Slider_Value > 575) { // If slider is moved to the Right
Stepper_Speed=map(Slider_Value,575,1023,1,200); // Map the Right value of the slider to a Speed value for the stepper
Stepper_Direction--; // Decrease the position to reach by one which will be used to move the stepper later
} else if (Slider_Value < 350) { // If slider is moved to the Left
Stepper_Speed=map(Slider_Value,350,0,1,200); // // Map the Left value of the slider to a Speed value for the stepper
Stepper_Direction++; // Increase the position to reach by one which will be used to move the stepper later
} else {
Stepper_Speed=0; // If slider is around the middle then don't move the stepper by setting the speed to zero
stepper.disableOutputs(); // Turn OFF L298N
}
// Move the stepper to new position
stepper.moveTo(Stepper_Direction);
stepper.setSpeed(Stepper_Speed);
stepper.setMaxSpeed(200);
// Do this until the stepper as reached the new destination
while (stepper.distanceToGo() != 0) { // if stepper hasn't reached new position
stepper.enableOutputs(); // Turn ON L298N
stepper.runSpeedToPosition(); // move the stepper until new position reached
}
}
CONCLUSION
This was my first time using the L298N to control a Stepper Motor, and I can see myself using it again for the right projects.
It does tend to overheat pretty fast when not shutting down the stepper current when idle, but when moving it seems fine.
I would only use it for very small stepper motors if I needed to have them running or idle for a long time though.
If all you need to simple control of stepper motors, and want to be able to connect everything quick and easy, the L298N is great for that.
I will be looking a other types of stepper drivers in the future and see what they have to offer, so stay tuned.
Thanks for stopping by and hope to see you again soon! Cheers!
TUTORIAL VIDEO
DOWNLOAD
Copy and Paste the above code/sketch in your Arduino IDE software.
Link to the libraries used in this tutorial:
AccelStepper Library by Mike Mcauley: https://www.airspayce.com/mikem/arduino/AccelStepper/index