IOT

How to Control Stepper Motor with A4988 Driver & Arduino?

In this tutorial, we’ll use the A4988 Driver Module and Arduino to control a NEMA17 stepper motor. The A4988 is a micro-stepping driver with a built-in translator for easy usage that can control bipolar stepper motors. With just two pins from our controller, we can control the stepper motor. The STEP pin will control the steps while the DIR pin will control the rotation direction.

Components Required:

  • Arduino UNO R3/ Nano or Any other Arduino Board
  • A4988 Stepper Motor Driver
  • NEMA17 Stepper Motor
  • Electrolytic Capacitors-100uF
  • Power Supply 1-12V, 2A DC Adapter
  • Power Supply 2-5V DC Adapter
  • Connecting Wire
  • Breadboard

A4988 Stepper Motor Driver Module

A4988 Stepper Driver Module

The A4988 is a full Microstepping Motor Driver with an easy-to-use built-in translator. Allegro’s breakout board has configurable current limiting, over-current and over-temperature protection, as well as five microstep resolutions. It operates between 8 and 35 volts and can deliver up to 1 amp per phase without the use of a heat sink or forced airflow. It is rated for 2 A per coil and comes with adequate cooling.

Features

  • Max. Operating Voltage: 35V
  • Min. Operating Voltage: 8V
  • Max. Current Per Phase: 2A
  • Microstep resolution: Full step, ½ step, ¼ step, 1/8 and 1/16 step
  • Reverse voltage protection: No
  • Dimensions: 15.5 × 20.5 mm (0.6″ × 0.8″)
  • Short-to-ground and shorted-load protection
  • Low RDS(ON) outputs
  • Thermal shutdown circuitry

A4988 Motor Driver Pinout

The A4988 driver has a total of 16 pins which are as follows:

A4988 Pins

1. Power Supply Pins:  VDD, VMOT, and a pair of GND pins make up the power supply pins. VDD is used to power the inner logic circuitry, which can range from 3 to 5 volts, whereas VMOT is used to power the motor, which can range from 8 to 35 volts..

2. Microstep Selection Pins: The A4988 driver includes three microstep resolution selector inputs, MS1, MS2, and MS3. We’ll set the motors to at least one of the five-step resolutions by setting appropriate logic levels to those pins.

3. Control Input Pins: The two control input pins are STEP and DIR. The STEP input controls the motor’s micro-steps, while the DIR input controls the motor’s spinning direction.

4. Power States Control Pin: EN, RST, and SLP are the three inputs used to control the A4988’s power states. The EN pin is an active low input that enables the A4988 driver when pulled LOW. SLP Pin is a low-input active pin. Pulling this pin LOW puts the driver into sleep mode, reducing the amount of energy used by the facility. The RST is a lively low input that ignores all STEP inputs when pulled LOW. It also resets the driver by resetting the internal translator to the initial stage of the motor.

.

5. Output Pins: The output pins are labeled 2B, 2A, 1B, and 1A. Any bipolar stepper motor with a voltage between 8 and 35 volts can be connected to those pins.

Heat Sink Requirement

If the current rating is up to 1A, the A4988 Driver can be used without a heat sink. A heat sink or other cooling device is necessary to get more than 1A per coil.

A4988 Heat Sink

The increased power dissipation of the A4988 driver causes a temperature rise that can exceed the IC’s capacity, potentially destroying it.

Setting Up Current Limit

Before connecting the motor, make sure the driver’s current limiting is set to keep the current within the motor’s limits. We can accomplish so by altering the reference voltage on the board with the potentiometer and utilizing the calculation below.

Current Limit = VRef x 2.5

If the Stepper Motor is rated for 350mA, for example, the reference voltage must be set to 0.14V. Adjust the current limit with a potentiometer with a little screwdriver until you achieve the rated current.

A4988 Current Limit Setting

NEMA17 Stepper Motor

NEMA 17 is a 1.8° step-angle hybrid stepping motor with 200 steps per revolution. At 4 V, each phase draws 1.2 A, resulting in a holding torque of 3.2 kg-cm. Printers, CNC equipment, and Laser Cutters all require NEMA 17 stepper motors.

NEMA17 Stepper Motor

Six wires are connected to two split windings in this motor. The first winding includes black, yellow, and green wires, whereas the second winding includes red, white, and blue wires.

Interfacing NEMA17 Stepper Motor with Arduino using A4988 Driver

Let’s connect the A4988 Stepper Motor Driver to the Arduino and use it to operate the NEMA17 Stepper Motor. To control the motor direction and stride, I used the D2 and D3 pins. The connection is shown below.

A4988 Arduino Nema17 Stepper Motor

The VDD pin is powered by a 5V supply, whereas the VMOT pin is powered by a 12V supply. Remember to connect the motor power supply pins to the board with a hefty 100F decoupling electrolytic capacitor.

Project PCB Gerber File & PCB Ordering Online

If you don’t want to put the circuit together on a breadboard and instead want a PCB for your project, here is the PCB for you. EasyEDAs online Circuit Schematics & PCB Design tool was used to create the PCB Board for the Nema17 Stepper Motor Control with A4988 & Arduino. The PCB’s front and back sides are shown below.

The Gerber File for the PCB is given below. You can simply download the Gerber File and order the PCB from https://www.nextpcb.com/

Download Gerber File: A4988 + Arduino + NEMA17 PCB

Now you can visit the NextPCB official website by clicking here: https://www.nextpcb.com/. So you will be directed to NextPCB website.

You can now upload the Gerber File and place an order on the website. The PCBs are in excellent condition. That is why the majority of people entrust NextPCB with their PCB and PCBA needs.

Basic Stepper Motor Control Code

It’s time to connect the Arduino to the computer and upload some code now that you’ve connected up the driver and set the current limit. This sketch only moves the motor in one direction.

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
}

Controlling Stepper Motor Spinning Direction

This code can be used to control the stepper motor’s direction. A clockwise or anticlockwise rotation of the motor is possible. This sketch controls the stepper motor’s speed, amount of rotations, and spinning direction.

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
}

Controlling Stepper Motor with AccelStepper Library

The Arduino AccelStepper library can be used to operate a stepper motor. It provides an object-oriented interface for stepper motors and motor drivers with 2, 3, or 4 pins.

AccelStepper outperforms the conventional Arduino Stepper library in various respects, including acceleration and deceleration capabilities. It also enables many steppers to run at the same time, with each stepper doing independent concurrent stepping. Even the slowest speeds are supported.

The following code shows all the above-mentioned features.

#include <AccelStepper.h>

// Define stepper motor connections and motor interface type. Motor interface type must be set to 1 when using a driver:
#define dirPin 2
#define stepPin 3
#define motorInterfaceType 1

// Create a new instance of the AccelStepper class:
AccelStepper stepper = AccelStepper(motorInterfaceType, stepPin, dirPin);

void setup() {
// Set the maximum speed in steps per second:
stepper.setMaxSpeed(1000);
}

void loop()
{
// Set the current position to 0:
stepper.setCurrentPosition(0);

// Run the motor forward at 200 steps/second until the motor reaches 400 steps (2 revolutions):
while(stepper.currentPosition() != 400)
{
stepper.setSpeed(200);
stepper.runSpeed();
}

delay(1000);

// Reset the position to 0:
stepper.setCurrentPosition(0);

// Run the motor backwards at 600 steps/second until the motor reaches -200 steps (1 revolution):
while(stepper.currentPosition() != -200)
{
stepper.setSpeed(-600);
stepper.runSpeed();
}

delay(1000);

// Reset the position to 0:
stepper.setCurrentPosition(0);

// Run the motor forward at 400 steps/second until the motor reaches 600 steps (3 revolutions):
while(stepper.currentPosition() != 600)
{
stepper.setSpeed(400);
stepper.runSpeed();
}

delay(3000);
}

Stepper Motor Acceleration & deceleration Code

The following sketch adds acceleration and deceleration to the stepper motor’s motions. The motor will move back and forth at a speed of 200 steps per second and a 30 step per second acceleration.

#include <AccelStepper.h>

#define dirPin 2
#define stepPin 3
#define motorInterfaceType 1

// Create a new instance of the AccelStepper class:
AccelStepper stepper = AccelStepper(motorInterfaceType, stepPin, dirPin);

void setup()
{
// Set the maximum speed and acceleration:
stepper.setMaxSpeed(200);
stepper.setAcceleration(30);
}

void loop() {
// Set the target position:
stepper.moveTo(600);
// Run to target position with set speed and acceleration/deceleration:
stepper.runToPosition();

delay(1000);

// Move back to zero:
stepper.moveTo(0);
stepper.runToPosition();

delay(1000);
}

Control NEMA17 Stepper Motor with A4988 & Potentiometer

The potentiometer can also be used to control the stepper motor. I used a 10K potentiometer and linked it to the Arduino Nano’s A0 analog pin. The voltage sent to Arduino’s Analog pin can be utilized as a reference voltage to control the Stepper Motor’s speed. The following is a schematic of the connections.

Stepper Motor A4988 Potentiometer Arduino

Copy the code from below and upload it to the Arduino Nano Board.

/ Defines pins numbers
const int stepPin = 3;
const int dirPin = 4;
int customDelay,customDelayMapped; // Defines variables
void setup() {
// Sets the two pins as Outputs
pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);
digitalWrite(dirPin,HIGH); //Enables the motor to move in a particular direction
}
void loop() {

customDelayMapped = speedUp(); // Gets custom delay values from the custom speedUp function
// Makes pules with custom delay, depending on the Potentiometer, from which the speed of the motor depends
digitalWrite(stepPin, HIGH);
delayMicroseconds(customDelayMapped);
digitalWrite(stepPin, LOW);
delayMicroseconds(customDelayMapped);
}
// Function for reading the Potentiometer
int speedUp() {
int customDelay = analogRead(A0); // Reads the potentiometer
int newCustom = map(customDelay, 0, 1023, 300,4000); // Convrests the read values of the potentiometer from 0 to 1023 into desireded delay values (300 to 4000)
return newCustom;
}

Conclusion

I hope all of you understand how to Control Stepper Motor with A4988 Driver & Arduino. We MATHA ELECTRONICS will be back soon with more informative blogs.

Leave a Reply

Your email address will not be published.