How to Program Two Stepper Motor with Arduino | Stepper Motor Solutions

How to Program Two Stepper Motors with Arduino: A DIY 3D Printer Success Story

Introduction: Step-by-Step Guide to Programming Two Stepper Motors with Arduino

Stepper motors are an essential component for precision movement in CNC machines, robots, and 3D printers. If you’re an electronics enthusiast looking to integrate stepper motors into your Arduino project, you’ve come to the right place. In this blog, we’ll walk you through how to program two stepper motors using Arduino to control a DIY 3D printer. From understanding the components to wiring the motors and writing the Arduino code, this guide covers everything you need to know.

The Challenge: DIY 3D Printer from Brazil

Meet Miguel, a passionate DIYer from Brazil who set out to build his own 3D printer from scratch. After assembling the mechanical frame, Miguel needed to control the X and Y axes using two stepper motors. Unfortunately, when using an Arduino Uno, the motors would jitter and occasionally lose steps, which resulted in inaccurate prints.

Miguel reached out for help, and we provided a solution that ensured smooth and precise movement for his 3D printer.

Our Solution: Precision Control with A4988 Stepper Motor Drivers

After analyzing Miguel’s setup, we identified the issue: the low-power stepper drivers he was using weren’t providing enough current to power the motors. To fix this, we recommended upgrading to the A4988 Stepper Motor Driver. This driver simplifies the control of stepper motors and provides the necessary current for strong, precise movement.

With the A4988 drivers, Miguel could control both motors simultaneously with more accuracy. Here’s a breakdown of the components and wiring we used.

Required Components for Arduino Stepper Motor Control:

  • Arduino Uno: The brain of the operation, controlling the stepper motors.
  • Two NEMA 17 Stepper Motors: These motors control the X and Y-axis movement of the 3D printer.
  • Two A4988 Stepper Driver Modules: These drivers provide high current to the motors and ensure smooth operation.
  • 12V-24V DC Power Supply: Powers the stepper motors and drivers with sufficient torque.
  • Breadboard and Jumper Wires: For making the necessary connections between the Arduino, A4988 drivers, and stepper motors.

Wiring Guide: Connecting Two Stepper Motors to Arduino

The A4988 Stepper Motor Driver acts as an interface between the low-power Arduino and the high-power stepper motors. Here’s how you connect the components:

  • STEP Pins (A4988): Connect the STEP pin of each A4988 driver to Arduino digital pins. For Motor 1, use pin 2, and for Motor 2, use pin 3.
  • DIR Pins (A4988): The DIR (Direction) pin controls the motor’s direction. Connect Motor 1’s DIR pin to Arduino pin 4, and Motor 2’s DIR pin to pin 5.
  • Motor Wires (A4988): Connect the NEMA 17 motor wires to the A4988 driver outputs (1A, 1B, 2A, 2B).
  • Power Supply: Use a 12V-24V power supply to power the A4988 drivers, and connect VMOT to the positive terminal and GND to the negative terminal of the power supply.
  • Common Ground: Connect the GND pin of the A4988 drivers to the GND pin on the Arduino to ensure proper operation.

How to Program the Movement: Arduino Code Example

Once the wiring is complete, you’ll need the Arduino code to control the stepper motors. Here’s a simple code that moves both motors in sync. We use digitalWrite() to generate pulses for each motor’s STEP pin, while the DIR pins control the direction.


<!-- Pins for Stepper Motor 1 (X-Axis) -->
#define STEP1 2
#define DIR1 4

<!-- Pins for Stepper Motor 2 (Y-Axis) -->
#define STEP2 3
#define DIR2 5

void setup() {
  // Set all pins as Outputs
  pinMode(STEP1, OUTPUT);
  pinMode(DIR1, OUTPUT);
  pinMode(STEP2, OUTPUT);
  pinMode(DIR2, OUTPUT);
}

void loop() {
  // Set directions
  digitalWrite(DIR1, HIGH); // Motor 1 moves forward
  digitalWrite(DIR2, LOW);  // Motor 2 moves reverse

  // Create 200 steps (one revolution for a 200-step motor)
  for (int i = 0; i < 200; i++) {
    digitalWrite(STEP1, HIGH);
    digitalWrite(STEP2, HIGH);
    delayMicroseconds(500); // Controls speed
    digitalWrite(STEP1, LOW);
    digitalWrite(STEP2, LOW);
    delayMicroseconds(500);
  }
  delay(1000); // Wait a second
}

Code Explanation:

  • DIR Pin: Controls the rotation direction of the motor (HIGH or LOW).
  • STEP Pin: Each HIGH to LOW pulse tells the motor to move one step.
  • delayMicroseconds(500): Controls the speed of the motor. A shorter delay results in faster movement.

This simple code allows you to move both motors simultaneously in sync, one forward and the other in reverse.

Key Takeaways for Your Stepper Motor Project

  • Use a Proper Driver: Never drive stepper motors directly from the Arduino. Always use a dedicated stepper motor driver like the A4988 or DRV8825.
  • Power is Key: Stepper motors need a dedicated power supply, as the Arduino’s 5V is insufficient for driving motors directly.
  • Common Ground: Always connect the grounds of the Arduino, driver, and power supply to ensure proper operation.
  • Start Simple: Start with full-step mode to avoid complexity. You can implement microstepping later for smoother motion.

Need Help with Your Stepper Motor Project?

Are you working on a DIY 3D printer or other stepper motor project? Our team can help! We offer high-quality NEMA 17 stepper motors, A4988 drivers, and complete motor kits for your needs. We also provide expert technical support to ensure your project is a success.

Contact us today for program two stepper motor with arduino and custom solutions and let us help you bring your ideas to life with the right stepper motor technology.

© 2025 Shenzhen Sunshine Sea Technology Co.,Ltd

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Leave a Reply

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