Control Stepper Motor with A4988 Driver Module & Arduino

5,761 views

Introduction

Motors are more widely used in electronics; whether it’s a DC motor, a servo motor, or a stepper motor, industries cannot function without them. These motors are employed in consumer and robotics applications, in addition to industrial machinery. So we’ve opted to talk about a stepper motor, which is one of those motors. So, in this tutorial, we are going to interface ” Stepper Motor with Arduino UNO”.

The name “stepper motor” comes from the fact that each electrical pulse causes the motor to advance one step. A pulsed electrical current is turned into an accurate one-step motion in the stepper motor, which is controlled by a stepper motor driver. Thus, stepper motors are controlled by a driver, which sends pulses into the motor, causing it to rotate. And, for this article, we are using the A4988 Driver Module.

A4988 Driver Module

The A4988 driver module has a built-in translator, and, because of this translator, just two wires are needed to connect it to the controller board. The maximum output capacity of the driver is 35 V and 2 A. It has full-, half-, quarter-, eighth-, and sixteenth-step modes for bipolar stepper motors.

A4988 Driver Module Pinout

Pin NameDescription
VDD & GNDConnected to 5V and GND of Controller
VMOT & GNDUsed to power the motor
1A, 1B, 2A, 2BConnected to the 4 coils of the motor
DIRECTIONMotor Direction Control pin
STEPSteps Control Pin
MS1, MS2, MS3Microstep Selection Pins
SLEEP Pins For Controlling Power States
RESET
ENABLE

Hardware Required

S.noComponentValueQty
1.Arduino UNO1
2.USB Cable Type A to B1
3.Jumper Wires
4.Driver ModuleA49881
5.Stepper Motor1
6.Adapter12v1
7.Capacitor100uF1

Circuit Diagram

Connection Table

ArduinoRelay Module
VCCVCC
GNDGND
D2DIR
D3STEP

Arduino Code

// Define pin connections & motor's steps per revolution
const int dirPin = 2;
const int stepPin = 3;
const int stepsPerRevolution = 200;

void setup()
{
	// Declare pins as Outputs
	pinMode(stepPin, OUTPUT);
	pinMode(dirPin, OUTPUT);
}
void loop()
{
	// Set motor direction clockwise
	digitalWrite(dirPin, HIGH);

	// Spin motor slowly
	for(int x = 0; x < stepsPerRevolution; x++)
	{
		digitalWrite(stepPin, HIGH);
		delayMicroseconds(2000);
		digitalWrite(stepPin, LOW);
		delayMicroseconds(2000);
	}
	delay(1000); // Wait a second
	
	// Set motor direction counterclockwise
	digitalWrite(dirPin, LOW);

	// Spin motor quickly
	for(int x = 0; x < stepsPerRevolution; x++)
	{
		digitalWrite(stepPin, HIGH);
		delayMicroseconds(1000);
		digitalWrite(stepPin, LOW);
		delayMicroseconds(1000);
	}
	delay(1000); // Wait a second
}

Working Explanation

Connect the circuit according to the circuit diagram or the connection table to control the stepper motor with Arduino UNO. Then, using the Arduino IDE, write the above-mentioned code and upload it to the Arduino UNO. Now you will watch the motors rotate in accordance with your code. Since the code is for a clockwise direction, therefore, you will observe the clockwise rotation.

Code Explanation

  • We first defined the Arduino pins that are connected to the A4988’s pins. Then, we defined the content stepsPerRevolution which holds the integer number of steps that the motor would take to complete per revolution.
  • In void setup, we declared the control pins of the motor as output.
  • In a void loop, we set the DIR pin high to rotate the motor in a clockwise direction (to rotate the motor anti-clockwise you can set DIR low). Since the frequency of the pulses we send to the STEP pin determines the speed of a motor, therefore we set that pin to High, and then Low. The motor operates faster when the pulses are higher.

Applications and Uses

Stepper motors have greater use in different industries, and therefore this circuit can be utilized at so many trades. For example:

  • In robotics, to convert electrical pulse into angular movements, or steps.
  • Pulling filament into the extruder in a 3D printer.
  • In CNC machines, to transform nonlinear input into linear movement.