Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

Arduino Mega 2560 Dual Stepper Motor Controller with A4988 Drivers and Battery Power

View and Edit Project In Cirkit DesignerImage of Arduino Mega 2560 Dual Stepper Motor Controller with A4988 Drivers and Battery Power

Circuit Documentation

Summary

This circuit controls two bipolar stepper motors using A4988 stepper motor drivers and an Arduino Mega 2560. The motors rotate 3000 steps forward and then 3000 steps backward with a 1-second delay between direction changes. The DIR and STEP pins for each motor are connected to the Arduino Mega 2560.

Component List

  1. Stepper Motor (Bipolar)

    • Pins: D, B, C, A
    • Description: A bipolar stepper motor used for precise control of rotational position.
  2. Arduino Mega 2560

    • Pins: IOREF, RESET, 3V3, 5V, GND, VIN, A0-A15, D21/SCL, D20/SDA, D19/RX1, D18/TX1, D17 PWM/RX2, D16 PWM/TX2, D15/RX3, D14/TX3, D0 RX0, D1 TX0, D2 PWM, D3 PWM, D4 PWM, D5 PWM, D6 PWM, D7 PWM, D8 PWM, D9 PWM, D10 PWM, D11 PWM, D12 PWM, D13 PWM, AREF, SDA, SCL, D52, D50, D48, D46, D44, D42, D40, D38, D36, D34, D32, D30, D28, D26, D24, D22, D53, D51, D49, D47, D45, D43, D41, D39, D37, D35, D33, D31, D29, D27, D25, D23
    • Description: A microcontroller board based on the ATmega2560, used to control the stepper motors.
  3. A4988 Stepper Motor Driver (Red)

    • Pins: ENABLE, MS1, MS2, MS3, RESET, SLEEP, STEP, DIR, VMOT, GND, 2B, 2A, 1A, 1B, VDD
    • Description: A driver module for controlling bipolar stepper motors.
  4. 5V Battery

    • Pins: positive, negative
    • Description: A power source providing 5V for the circuit.

Wiring Details

Stepper Motor (Bipolar) 1

Stepper Motor (Bipolar) 2

Arduino Mega 2560

A4988 Stepper Motor Driver (Red) 1

A4988 Stepper Motor Driver (Red) 2

5V Battery

Code Documentation

/*
 * This Arduino Sketch controls two bipolar stepper motors using A4988 drivers.
 * The motors rotate 3000 steps forward and then 3000 steps backward with a
 * 1-second delay between direction changes. The DIR and STEP pins for each
 * motor are connected to the Arduino Mega 2560.
 */

#define DIR_PIN1 2   // Direction pin for Motor 1
#define STEP_PIN1 3  // Step pin for Motor 1
#define DIR_PIN2 4   // Direction pin for Motor 2
#define STEP_PIN2 5  // Step pin for Motor 2
#define DELAY_BETWEEN_STEPS 300 // Microseconds between steps
#define DELAY_BETWEEN_DIRECTIONS 1000 // Milliseconds between direction changes

void setup() {
  pinMode(DIR_PIN1, OUTPUT);
  pinMode(STEP_PIN1, OUTPUT);
  pinMode(DIR_PIN2, OUTPUT);
  pinMode(STEP_PIN2, OUTPUT);
}

void loop() {
  // Move both motors forward
  digitalWrite(DIR_PIN1, HIGH); // Set direction for Motor 1
  digitalWrite(DIR_PIN2, HIGH); // Set direction for Motor 2
  moveMotor(STEP_PIN1, 3000);   // Move Motor 1
  moveMotor(STEP_PIN2, 3000);   // Move Motor 2

  delay(DELAY_BETWEEN_DIRECTIONS); // 1-second delay

  // Move both motors backward
  digitalWrite(DIR_PIN1, LOW);  // Change direction for Motor 1
  digitalWrite(DIR_PIN2, LOW);  // Change direction for Motor 2
  moveMotor(STEP_PIN1, 3000);   // Move Motor 1
  moveMotor(STEP_PIN2, 3000);   // Move Motor 2

  delay(DELAY_BETWEEN_DIRECTIONS); // 1-second delay
}

void moveMotor(int stepPin, int steps) {
  for (int i = 0; i < steps; i++) {
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(DELAY_BETWEEN_STEPS);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(DELAY_BETWEEN_STEPS);
  }
}

This code sets up the Arduino Mega 2560 to control two stepper motors via the A4988 drivers. The motors are moved forward and backward with a specified delay between steps and direction changes.