In this tutorial it is shown how to control a stepper motor using Arduino l293d motor shield. You will learn how to connect a stepper motor to the Arduino motor shield and how to use the AF motor shield library to control the direction and how to use different stepping modes. We will use Nema 17 stepper motor and Arduino Uno in this tutorial.
Nema 17 Stepper Motor
The Nema 17 stepper motor is a popular bipolar stepper motor widely used in robotics, 3D printers, film cutting machines, engraving machines and other applications. The stepper motor is called Nema 17 because of the fact that its face size is 1.7 inches by 1.7 inches. It comes in single and double shaft. Here we are using single shaft type whose step angle is 1.8 degree. Hence it takes 200 steps per revolution for complete 360 rotation(200*1.8 degree). The recommended voltage is 12V. It has a holding torque of 4.8 kg·cm. It maximum speed depends on the stepping size and driver but 600 rpm to 4688 rpm have reported.
Arduino Motor Shield
The Arduino motor shield is popular among hobbyist. The motor shield can be attached on top of Arduino UNO and hence called a "Shield". The motor shield can drive up-to 4 DC motors independently and each DC motors can be driven in both direction and speed can be controlled using PWM technique. In our last tutorial DC motor control using L293D Motor Shield and Arduino we showed how we can control a DC motor speed and direction. Here we will be driving NEMA17 bipolar stepper motor. We will be using the Stepper motor port 2(M3 & M4).
Stepper Motor, Motor Shield & Arduino Interfacing
The wiring diagram of stepper motor Nema17, Motor Shield and Arduino is as shown below.
Here we have connected the 4 pins of the Nema17 motor to the Stepper Motor Connection Port 2(M3 and M4). The shield is powered by 12V DC power supply connected to the External Power Supply terminal.
Programming Stepper motor control using Motor Shield & Arduino
To write program for stepper motor control using the motor shield we will make use of the AF motor shield library for Arduino. For using the library the following are the steps that is required.
1. Include the AF motor library header file
On the top of the program sketch, write the following header file.
#include <AFMotor.h>
2. Create an Object with class name AF_Stepper
AF_Stepper stepmotor(StepsPerRevolution, Stepper Motor Port Number);
For example,
AF_Stepper stepmotor(200, 2);
In the example above, we have created an object called stepmotor of class AF_Stepper. Also we passed two argument: 200 as our StepsPerRevolution and 2 as our stepper motor port number. There are two ports 1 and 2 available for connecting stepper motor in the Motor Shield. See the Motor Shield picture above where Stepper 1 and Stepper 2 are labelled.
3. Use Methods for rotating the Stepper motor
After having created stepper motor object stepmotor, we can use different methods using dot notation. The following are different methods you can use with the library:
a. setSpeed(RPMspeed)
This method sets the RPM speed of the motor.
Example Usage:
stepmotor.setSpeed(10);
b. onestep(direction, stepstyle)
This method will rotate the stepper motor in one step. The direction parameter can be either FORWARD or BACKWARD. The stepstyle can be any of the followings:
- SINGLE : One coil is energized at a time
- DOUBLE : Two coils are energized at a time for more torque.
- INTERLEAVE - Alternate between single and double to create a half-step in between. This can result in smoother operation, but because of the extra half-step, the speed is reduced by half too.
- MICROSTEP - Adjacent coils are ramped up and down to create a number of 'micro-steps' between each full step. This results in finer resolution and smoother rotation, but with a loss in torque.
Example Usage:
stepmotor.onestep(FORWARD, DOUBLE);
c. step(steps, direction, style)
This method is used to rotate the stepper motor in multiple steps specified by the steps arguments in forward or backward direction with different stepping styles as explained above.
Example Usage:
stepmotor.step(100, FORWARD, MICROSTEP);
d. release()
This method is used to release the holding torque of the stepper motor which will reduce the current consumption and hence heat generated. This method will not actively stop the motor rotation.
Example Usage:
stepmotor.release();
Program Code:
In the following Arduino motor shield code, we will send various commands to rotate the stepper motor at speed 20RPM, in forward and in backward direction with different stepping style.
#include <AFMotor.h>
// Specify Steps Per Revolution, depends on Stepmotor specification
const int spp = 200;
// connect stepmotor to stepper motor port #2 (M3 and M4)
AF_Stepper stepmotor(spp, 2);
void setup() {
Serial.begin(9600); //configure serial interface
stepmotor.setSpeed(20); // 20 rpm
}
void loop() {
unsigned char cmd;
if(Serial.available()){
cmd = Serial.read();
switch(cmd){
case 's':
Serial.println("Single coil steps");
stepmotor.step(100, FORWARD, SINGLE);
stepmotor.step(100, BACKWARD, SINGLE);
break;
case 'd':
Serial.println("Double coil steps");
stepmotor.step(100, FORWARD, DOUBLE);
stepmotor.step(100, BACKWARD, DOUBLE);
break;
case 'i':
Serial.println("Interleave coil steps");
stepmotor.step(100, FORWARD, INTERLEAVE);
stepmotor.step(100, BACKWARD, INTERLEAVE);
break;
case'm':
Serial.println("Micrsostep steps");
stepmotor.step(100, FORWARD, MICROSTEP);
stepmotor.step(100, BACKWARD, MICROSTEP);
break;
case 'r':
Serial.println("Release Torque");
stepmotor.release();
break;
default:
break;
}
}
}
In the above program code, we have used serial port of the Arduino to send various commands to Arduino to move the Stepper motor in forward and backward direction with different stepping styles at speed of 20 RPM. For example if we send command s then the stepper motor will rotate at a speed of 20 RPM, 100 steps, that is half of revolution, in forward and backward directions with single coil steps. If we send command d then the stepper motor will rotate at speed 20 RPM, 100 steps, in forward and backward directions with double coil steps. Similarly for command i and m the stepper motor will rotate in interleaved step mode and microstep step mode with speed of 20 RPM, forward and backward direction with 100 steps which is half the full rotation angle.
Video Demonstration