One of the easiest and inexpensive way to control stepper motors is to interface L293D Motor Driver IC with Arduino. It can control both speed and spinning direction of any Unipolar stepper motor like 28BYJ-48 or Bipolar stepper motor like NEMA 17.
If you want to learn the basics of L293D IC, below tutorial is invaluable. Consider reading (at least skimming) through this tutorial first.
Controlling a Stepper Motor With an H-Bridge
As L293D IC has two H-Bridges, each H-Bridge will drive one of the electromagnetic coils of a stepper motor.
By energizing these electromagnetic coils in a specific sequence, the shaft of a stepper can be moved forward or backward precisely in small steps.
However, the speed of a motor is determined by the how frequently these coils are energized.
Below image illustrates driving stepper with H-Bridge.
Driving Unipolar Stepper Motor (28BYJ-48)
In our first experiment, we are using 28BYJ-48 unipolar stepper rated at 5V. It offers 48 steps per revolution.
Before we start hooking the motor up with the chip, you will need to determine the A+, A-, B+ and B- wires on the motor you plan to use. The best way to do this is to check the datasheet of the motor. For our motor these are orange, pink, blue and yellow.
Note that we will not be using the common center connection(Red) in this experiment.
The center connection is merely used to energize either the left or right side of the coil, and get the effect of reversing the current flow without having to use a circuit that can reverse the current.
The connections are fairly simple. Start by connecting 5V output on Arduino to the Vcc2 & Vcc1 pins. Connect ground to ground.
You also need to connect both the ENA & ENB pins to 5V output so the the motor is always enabled.
Now, connect the input pins(IN1, IN2, IN3 and IN4) of the L293D IC to four digital output pins(12, 11, 10 and 9) on Arduino.
Finally, connect the stepper motor’s wires A+ (Orange), A- (Pink), B- (Yellow) and B+ (Blue) to the L293D’s output pins (Out4, Out3, Out2 & Out1) as shown in the illustration below.
Driving Bipolar Stepper Motor (NEMA 17)
In our next experiment, we are using NEMA 17 bipolar stepper rated at 12V. It offers 200 steps per revolution, and can operate at 60 RPM.
Before we start hooking the motor up with the chip, you will need to determine the A+, A-, B+ and B- wires on the motor you plan to use. The best way to do this is to check the datasheet of the motor. For our motor these are red, green, blue and yellow.
The connections are fairly simple. Start by connecting external 12V power supply to the Vcc2 pin and 5V output on Arduino to the Vcc1 pin. Make sure you common all the grounds in the circuit.
You also need to connect both the ENA & ENB pins to 5V output so the the motor is always enabled.
Now, connect the input pins(IN1, IN2, IN3 and IN4) of the L293D IC to four digital output pins(12, 11, 10 and 9) on Arduino.
Finally, connect the A+ (Red), A- (Green), B+ (Blue) and B- (Yellow) wires from the stepper motor to the L293D’s output pins (Out4, Out3, Out2 & Out1) as shown in the illustration below.
Arduino Code – Controlling Stepper Motor
The following sketch will give you complete understanding on how to control a unipolar or bipolar stepper motor with L293D chip and is same for both the motors except stepsPerRevolution
parameter.
Change this parameter as per your motor’s specification before trying the sketch out. For example, for NEMA 17 set it to 200 and for 28BYJ-48 set it to 48.
// Include the Arduino Stepper Library#include <Stepper.h>// Number of steps per output rotationconst int stepsPerRevolution = 200;// Create Instance of Stepper libraryStepper myStepper(stepsPerRevolution, 12, 11, 10, 9);void setup(){// set the speed at 20 rpm:myStepper.setSpeed(20);// initialize the serial port:Serial.begin(9600);}void loop() {// step one revolution in one direction:Serial.println("clockwise");myStepper.step(stepsPerRevolution);delay(500);// step one revolution in the other direction:Serial.println("counterclockwise");myStepper.step(-stepsPerRevolution);delay(500);}
The sketch starts with including Arduino Stepper Library. The stepper library comes packaged with the Arduino IDE and takes care of sequencing the pulses we will be sending to our stepper motor.
// Include the Arduino Stepper Library#include <Stepper.h>
After including the library we define a variable named stepsPerRevolution
. As the name suggests it’s the number of steps per revolution that our motor is rated at. Change this parameter as per your motor’s specification. For example, for NEMA 17 set it to 200 and for 28BYJ-48 set it to 48.
// Number of steps per output rotationconst int stepsPerRevolution = 200;
Next, we create an instance of the stepper library. It takes the steps per revolution of motor & Arduino pin connections as parameter.
// Create Instance of Stepper libraryStepper myStepper(stepsPerRevolution, 12, 11, 10, 9);
In setup section of code, we set the speed of stepper motor by calling setSpeed()
function and initialize the serial communication.
void setup(){// set the speed at 20 rpm:myStepper.setSpeed(20);// initialize the serial port:Serial.begin(9600);}
In loop section of code, we simply call step()
function which turns the motor a specific number of steps at a speed determined by setSpeed()
function. Passing a negative number to this function reverses the spinning direction of motor.
void loop() {// step one revolution in one direction:Serial.println("clockwise");myStepper.step(stepsPerRevolution);delay(500);// step one revolution in the other direction:Serial.println("counterclockwise");myStepper.step(-stepsPerRevolution);delay(500);}