Hi Guys

This article is about something every electronics enthusiast would want to learn and master. Its name might sound really simple but mind you, this thing has the power to solve many complicated problems. Yes, I’m talking about the Stepper Motor Controller – a device which is being used excessively in the world of electronics these days. From DSLR auto-focus systems & 3D printers to plotter rigs and CNCs, you will find SMCs (stepper motor controllers) in almost all types of electro-mechanical equipments & systems.

The main reason why stepper motor controllers are used so often is because they can be programmed to move precise & defined steps either in the clockwise or in the counter-clockwise direction.

In this article, through a basic prototype, I am going to teach you that how you can drive a stepper motor. I will also explain that how you can connect a stepper motor to a microcontroller to programme it.

So here’s the stuff I am going to be using for prototyping:

  1. Arduino UNO
  2. Bipolor stepper motor
  3. Jumper wire
  4. Breadboard
  5. Breadboard jumper
  6. L293D IC

First you can make the same prototype to start learning & tinkering. I have also added the Arduino code below for testing purposes. You can watch the below video for a descriptive step-by-step guide to create the stepper motor controller.

And here is the circuit diagram for further reference. Before you connect your microcontroller and circuit to the power source, just make sure all connections are according to the circuit diagram.

Arduino Stepper Motor controller

Ok, now it’s time to upload the programme into Arduino and test the project. Here is the code.

Arduino code to run stepper motor with L293D

#include <Stepper.h>

int in1Pin = 12;
int in2Pin = 11;
int in3Pin = 10;
int in4Pin = 9;

// change this to the number of steps on your motor
#define STEPS 512

Stepper motor(STEPS, in1Pin, in2Pin, in3Pin, in4Pin);

void setup()
{
pinMode(in1Pin, OUTPUT);
pinMode(in2Pin, OUTPUT);
pinMode(in3Pin, OUTPUT);
pinMode(in4Pin, OUTPUT);

// this line is for Leonardo's, it delays the serial interface
// until the terminal window is opened
while (!Serial);

Serial.begin(9600);
motor.setSpeed(20);
}

void loop()
{
if (Serial.available())
{
int steps = Serial.parseInt();
motor.step(steps);
}
}

Found it useful? Anything you like & dislike? I’ll be happy to reply to your comments!

For more tech wizardry from the CircuitMagic channel, subscribe to our YouTube

19 Comments
  1. bilel 5 years ago

    thx for your tuto but i ask if can i use a stepper little more big with L293D

  2. henrique 5 years ago

    the power input for motor is shorting out in the diagram

  3. sabbir 5 years ago

    can i used arduino mega for this project???

  4. Navjot 5 years ago

    Could we use a transistor to power motors with a higher voltages? say 12v / 24v motors, like NEMA17.
    Thanks.

  5. Viraj 4 years ago

    Which model of stepper motor is used here? Plz reply fast. It’s urgent. I want to do it for my project

  6. jigar-buddhadeo 4 years ago

    Can you help in writing the same program for matlab. I have interface my arduino mega2560 with matlab using matlab support package. Can you write the code to run the same.

  7. Anijith 4 years ago

    This is very helpful..Thanks

  8. Avadhut Kulkarni 4 years ago

    what are the changes in program if I want to rotate a stepper motor first 120 degree anticlockwise then 120 anticlockwise and the 240 degree clockwise?

  9. Akhil Rastogi 4 years ago

    Hi
    Can I have your email I’d

  10. Frantisek 3 years ago

    Power input motor 🙂 short circuit man!

  11. Utsav 3 years ago

    The stepper motor as well as the motor driver is getting quite heated up, what should i do to prevent this? The connections and power supply is fine but the motor and driver IC is still heating up.

  12. Adil Farooq 3 years ago

    I ran a stepper motor of 3.3V with L293D. Worked fine as well. Thanks for sharing

  13. ivan 2 years ago

    thank you for a great tutorial

  14. Armins Senkans 2 years ago

    I was copy-paste Your programm, but it not working, because I got the answer:
    “#include expects “FILENAME” or
    What is this? Which row I can include filename?

  15. HAROLD ANDRES 1 year ago

    power motor is CRASH

  16. madumal 1 year ago

    how can i find #include <Stepper.h> library file
    plz send the answer to my e mail…..

  17. MA Bandula 2 weeks ago

    Dear friend, I must first thank you for trying to help armatures like me to learn a little bit about controlling Stepper motors with Arduino Uno and L293D IC for hobby.
    I have followed the instructions exactly as in the circuit diagram and the video and tried to run your code by copying to Arduino IDE.
    I have tried running both a Nema 17 Stepper and a DVD stepper, but ran into several errors in the process.

    When tried to compile, it stopped on the very first line giving the error.

    #include expects “FILE NAME” or

    So I put at the start and end of it like this #include
    Then it says “Error compiling for Arduino /Genuino Uno.

    This is what I tried to compile.

    #include

    int in1Pin = 12;
    int in2Pin = 11;
    int in3Pin = 10;
    int in4Pin = 9;

    // change this to the number of steps on your motor
    #define STEPS 512

    Stepper motor(STEPS, in1Pin, in2Pin, in3Pin, in4Pin);

    void setup()
    {
    pinMode(in1Pin, OUTPUT);
    pinMode(in2Pin, OUTPUT);
    pinMode(in3Pin, OUTPUT);
    pinMode(in4Pin, OUTPUT);

    // this line is for Leonardo’s, it delays the serial interface
    // until the terminal window is opened
    while (!Serial);

    Serial.begin(9600);
    motor.setSpeed(20);
    }

    void loop()
    {
    if (Serial.available())
    {
    int steps = Serial.parseInt();
    motor.step(steps);
    }
    }

    Can you please look at the code and tell where I am going wrong.
    Many thanks.

  18. MA Bandula 2 weeks ago

    #include (less than sign)&lt;Stepper.h&gt;(greater than sign)

    This is what the first line of code should have read in my first message.

Leave a reply

Your email address will not be published. Required fields are marked *

*

or

Log in with your credentials

or    

Forgot your details?

or

Create Account