How to use the Nema 17 Stepper motor with Arduino and A4988 Stepper driver

How to use the Nema 17 Stepper motor with Arduino and A4988 Stepper driver

Hello and welcome back. In this tutorial, we will learn how to use the Nema 17 stepper motor with Arduino. For this project, I used an A4988 stepper motor driver to control the motor and an Arduino Nano board as the main controller. The joystick module is used to rotate the stepper motor left and right. If you want to know more about stepper motors and the A4988 Stepper Driver, how they work, please check this link. Stepper motors are widely used in precision control applications, so this knowledge will help you when making 3D printers, CNC machines, camera sliders, or any other stepper motor-based projects. This is a very simple and beginner-friendly project.

Ok, let’s do this project step by step. The required components are given below.

Disclosure: These Amazon links are Affiliate links. As an Amazon Associate, I earn from qualifying purchases.

Step 1

Firstly, identify these components.

Step 2

Secondly, place the A4988 stepper motor driver on the breadboard. After that, connect a 100µF capacitor across the external power supply pins to help stabilize the voltage.

Step 3

Thirdly, connect the stepper motor driver to the Arduino UNO board using the circuit diagram below.

How to use the Nema 17 Stepper motor with Arduino and A4988 Stepper driver

Step 4

Next, connect the joystick module to the Arduino UNO board.

Step 5

Afterward, connect the Arduino board to the computer using a USB cable. Next, open the Arduino IDE and copy–paste the code below.

#define stepPin 2
#define dirPin 5
#define enPin 8       // A4988 Enable pin
#define joyX A0       // Joystick X-axis

void setup() {
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
  pinMode(enPin, OUTPUT);
  pinMode(joyX, INPUT);

  digitalWrite(enPin, LOW); // Enable motor outputs at startup
}

void loop() {
  int xValue = analogRead(joyX);

  if (xValue < 400) {
    // Move LEFT
    digitalWrite(enPin, LOW);        // Enable motor
    digitalWrite(dirPin, LOW);       // Set direction
    stepMotor(map(512 - xValue, 0, 512, 1, 10));  // More deflection = more steps

  } else if (xValue > 624) {
    // Move RIGHT
    digitalWrite(enPin, LOW);        // Enable motor
    digitalWrite(dirPin, HIGH);      // Set direction
    stepMotor(map(xValue - 512, 0, 511, 1, 10));

  } else {
    // Joystick centered — fully stop and disable motor
    digitalWrite(enPin, HIGH);       // Disable motor outputs
    delay(10);                       
  }
}

void stepMotor(int steps) {
  for (int i = 0; i < steps; i++) {
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(800);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(800);
  }
}
  • Now, select the correct board and port in the Arduino IDE. After that, click the Upload button to upload the program.

Step 6

Next, provide a 12V DC power supply to the stepper motor driver. Now you can control the stepper motor easily using the joystick module.

Ok, enjoy this project! The full video guide is provided below. We hope to see you in the next project. Have a great day!

How to use the Nema 17 Stepper motor with Arduino and A4988 Stepper driver

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *