Learning how to control a stepper motor using a microcontroller like Arduino will be useful in many projects. Here I have used Arduino UNO to control Nema 17 stepper motor using A4988 stepper motor driver. A simple arduino stepper motor control code is written without library that shows you how to control stepper motor clockwise and anticlockwise with arduino.
Interfacing Arduino with A4988 and Stepper Motor(Nema 17)
For this Nema17 Arduino A4988 tutorial the wiring diagram used is shown below.
How to connect stepper motor Nema17 to A4988 driver
The stepper motor Nema17 is a bipolar stepper motor which has 4 pins. 2 pins of the same coil of the motor should be connected to 2A and 2B labelled on the A4988 stepper motor driver. Similarly the other 2 pins of the Nema 17 stepper motor should be connected to the 1A and 1B labelled pins on the A4988 stepper motor driver. Below picture shows how to connect Nema17 pins to A4988 driver.
If you are confused which of the two pins of the nema 17 stepper motor belongs to same coil you can just use your multimeter and use the continuity tester to check which two wires belongs to same coil. If there is continuity then they are part of the same coil and should be connected to either 2A and 2B or connect them to 1A and 1B.
Following picture shows the A4988 breakout board.
How to connect Power supply to A4988 stepper motor driver
Next we will connect power supply to the A4988 stepper motor driver. In the wiring diagram above, 12V power supply positive terminal is connected to the VMOT pin and negative terminal of power supply to the GND pin just below the VMOT pin. A 100uF capacitor is used between the VMOT and GND to smooth the power signal. If the voltage is not enough, a4988 steppers will not spin. This is shown in the picture below.
How to connect Arduino to A4988 stepper motor driver
The next thing is the arduino a4988 wiring, that is, how to connect the Arduino to A4988 driver.
The !Enable pin is to enable/disable the A4988 driver(! means active low here). It is active low input pin. So if low signal is sent to this pin, the A4988 driver is enabled otherwise disabled. Internally it is pulled low by the internal pull-down resistor so we can leave it unconnected and thereby enable the driver.
The next 3 pins, MS3, MS2 and MS1(MS means Microstep Select) pins of the A4988 driver are used to control the stepping resolution. Different combination of these MS3, MS2 and MS1) pins creates different micro-stepping resolution. From datasheet you can see how you can select one of the step resolution.
Here we will use full step mode which means that the MS3, MS2 and MS1 pins of the A4988 driver are left unconnected because internally they are connected to pull-down resistors which keeps them at low level logic.
The !SLEEP and !RESET pins (! meaning here active low) controls the sleep mode and the reset of the A4988 driver. Both are active low inputs. The SLEEP pin is used to drive the A4988 into sleeping mode in which case the driver will minimize power consumed by the driver. This happens because when sleep mode is activated the internal circuitry(output FETs, charge pumps, current regulator) are disabled and thereby power consumption is lowered. When the reset is made active low then the A4988 driver is set to a predefined Home state until the reset goes high again. In our case we will tie them together.
Arduino Stepper Motor Speed and Direction Control
Next is the STEP and DIRECTION(DIR) pins of the A4988 driver. The Arduino uses these pins for stepper motor speed and direction control.Sending a high pulse from Arduino to STEP pin moves the motor one step. The STEP signal controls the speed of the stepper motor Nema17. A longer step pulse will rotate the motor faster. The direction(DIR) pin is used to control the stepper motor direction. If we send a high pulse to A4988 Direction pin then A4988 will cause the stepper motor to rotate in clockwise direction. If low pulse is sent then the A4988 will rotate the motor in anti-clockwise direction. These two pins needs to be connected to the Arduino digital pins. In this arduino stepper motor tutorial, we will connect STEP pin to digital pin 9 and DIRECTION pin to digital pin 8 of Arduino.
The last other two pins, the VDD(pin 9) and Ground pins(pin 10) shown in the above schematic, need to be connected to the 5V and GND pin of the Arduino respectively.
The following picture shows how to connect Arduino to A4988.
A4988, Arduino Stepper Motor Wiring on Breadboard
The whole A4988 Arduino Stepper Motor circuit realized on breadboard is shown below.
Programming & Coding of Arduino Stepper Motor Control
In this simple Arduino stepper motor tutorial, we will rotate the Nema17 motor one full cycle in clockwise direction and after 1.5 second later rotate the motor one full cycle in anti-clockwise cycle. There are lots of stepper motor control library but in this tutorial, we will use arduino stepper motor code without library. For this we have to understand how the stepper motor step and direction control works using Arduino.
Arduino Stepper Motor Speed Control
For one full cycle, we have to send 360 degree/1.8 degrees step angle = 200 pulses for Nema 17 stepper motor. Thats because Nema 17 has 1.8 degree step angle so that when we send one pulse on step pin of A4988 via digital pin 9 it will rotate 1.8 degree. It follows that by sending 200 pulses the total angle of rotation is 360 degree(one complete cycle).
Arduino Stepper Motor Direction Control
The direction of rotation is controlled by the DIR pin of A4988. By sending a HIGH signal to this pin from arduino(here digital pin 8) the motor will rotate in clockwise direction. By sending LOW signal the motor will rotate in anti-clockwise direction, that is, stepper motor moves in reverse direction. This is how you can control direction of rotation of stepper motor using Arduino.
The following is stepper motor arduino code using A4988.
// defines pins numbers
const int stepPin = 9;
const int dirPin = 8;
void setup() {
// Sets the STEP and DIR pins as Outputs
pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);
}
void loop() {
digitalWrite(dirPin,HIGH); // Send HIGH pulse to rotate the motor in clockwise direction
//For one full cycle rotation, send 200 pulses
for(int i = 0; i < 200; i++) {
digitalWrite(stepPin,HIGH);
delayMicroseconds(500); //500us delay
digitalWrite(stepPin,LOW);
delayMicroseconds(500); //500us delay
}
delay(1500); // wait for 1.5sec
digitalWrite(dirPin,LOW); // Send LOW pulse to rotate the motor in anti-clockwise direction
// Send 200 pulses to rotate the motor one full cycle in anti-clockwise direction
for(int i = 0; i < 200; i++) {
digitalWrite(stepPin,HIGH);
delayMicroseconds(500); //500us delay
digitalWrite(stepPin,LOW);
delayMicroseconds(500); //500us delay
}
delay(1500); // wait for 1.5sec
}
Watch the following video demonstration.
If you are interested in DC motor control see our tutorials:
-Speed control of DC motor with PWM using Arduino
- DC motor Speed control with Potentiometer and PWM using Arduino