The Cheapest way to control a Stepper Motor with an Arduino. But is it any good?

//The Cheapest way to control a Stepper Motor with an Arduino. But is it any good?
TUTORIAL

L298n_Arduino_stepper


Using the L298N Motor Driver

with a Stepper Motor

HELP OUT

All my content is free.

If you feel that my videos are helping and you would like to contribute, you can toss some coins in the Tip Jar via PayPal.

Select amount then click the “Donate” button.

Brainy-Bits Tip Jar Amount
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.

WHAT’S SO GREAT ABOUT THE EASYDRIVER ANYWAY?

Arduino Easy Driver

Don’t get me wrong the EasyDriver motor driver is pretty great, but it does have some things to consider:

The first thing you’ll notice is that when you buy one, it will come with the pins unsoldered…  So that’s something you’ll need to do on your own right from the start.

Also, the EasyDriver is really made to be used on a breadboard or a custom designed PCB.  I mean, you could solder the pins UP and connect the Stepper Motor and Arduino pins that way, but those connections would not be very secured.

The maximum current it can supply is 750mA per coil.  Since most of the time we’re using Stepper Motors with 2 coils, then this maximum go up to 1.5Amps.

The EasyDriver is pretty power efficient, but it’s still a good idea to install a heatsink if you’re gonna come close to hitting the current limit.

There is a version of the EasyDriver that can provide up to 2A per coil… the ‘BIG EasyDriver’, and I’ll have a look at that one in a future tutorial.

Arduino Big EasyDriver

So if your stepper motor requires more , then you can’t really use the EasyDriver…  Well… you can, but the Stepper Motor will not have the same amount of torque.

Ok that’s the cons, but they are pros as well:

You can operate the EasyDriver with as little as 2 Digital pins from an Arduino, which is great if your project is running out of them.

Of course you’ll need more if you want to control the type of steps, and enable/disable, but it’s still an option.

The EasyDriver does have a small potentiometer onboard that enables you to limit the amount of current fed to the stepper motor…  So if you have very small motors that have a maximum rating of let’s say 500mA you can adjust this pot accordingly and not have your stepper overheat as much.

Also it’s able to adjust the current while running the stepper or when the stepper is stopped, this reduces the amount of heat generated.

Another big benefit is that you can adjust the type of steps you want to achieve.

Most stepper motors are 200 steps per revolution using Full Steps.

The EasyDriver can do Full Steps, but also: half step , 1/4 step, and 1/8 step.

For example if you want more precision or smoother motion, you can set it up to do 1/8 steps, which will give you 1600 steps per revolution.

And this can be adjusted inside the Arduino code directly, no jumpers needed.

To power the EasyDriver you can use any power supply (depending on your stepper motor specs), from 6V to 30V, it has a 5V regulator onboard that power the onboard electronics and can provides clean 5V (up to 500mA) that you can use to power your Arduino.

In closing the EasyDriver is a better driver in many ways,  but is of course a little more expensive.

WHAT ABOUT THE L298N?

Arduino 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.

Arduino Analog Slider

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
  }
}
TUTORIAL VIDEO
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!

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

By | 2019-12-06T19:34:30-05:00 December 4th, 2019|Tutorials|

Leave A Comment