Arduino Step Motor

Dear all,

I was hoping you can help me troubleshoot something.

I am using Arduino Mega with NEMA 17 Mercury Motor SM-42BYG011-25 and A4988 Step Driver.

When i try to increase the speed, the motor starts making scratching noise or missing steps.

Can you review my code and tell me if there is a problem in the code or that's just the limit speed of the motor.

I'd like at least 1500-1700 RPM speed from the NEMA 17 motor and i can barely get 600 RPM.

Thank you a lot.

#include <AccelStepper.h>

// Define the connections to the A4988 driver
#define stepPin 2
#define dirPin 3
#define enPin 4 // Define the enable pin
#define runButtonPin 7 // Define the run button pin
#define holdButtonPin 8 // Define the hold button pin

// Create an instance of AccelStepper
AccelStepper stepper(AccelStepper::DRIVER, stepPin, dirPin);

// State variable to keep track of motor running state
bool isMotorRunning = false;
bool lastRunButtonState = HIGH;
bool lastHoldButtonState = HIGH;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;

void setup() {
  // Set the pins as outputs or inputs as required
  pinMode(enPin, OUTPUT);
  pinMode(runButtonPin, INPUT_PULLUP); // Use internal pull-up resistor for run button
  pinMode(holdButtonPin, INPUT_PULLUP); // Use internal pull-up resistor for hold button

  // Set initial direction and speed
  digitalWrite(dirPin, HIGH); // Set the direction
  stepper.setMaxSpeed(700);  // Set maximum speed
  stepper.setAcceleration(400); // Set acceleration
  stepper.setSpeed(650);      // Set initial speed (lowered for testing)

  // Enable the driver initially
  digitalWrite(enPin, LOW); // Enable the driver
}

void loop() {
  // Check if the run button is pressed
  if (debounceButton(runButtonPin, &lastRunButtonState)) {
    isMotorRunning = true;
    digitalWrite(enPin, LOW); // Enable the driver
  }

  // Check if the hold button is pressed
  if (debounceButton(holdButtonPin, &lastHoldButtonState)) {
    isMotorRunning = false;
    digitalWrite(enPin, HIGH); // Disable the driver
  }

  if (isMotorRunning) {
    // Run the motor
    stepper.runSpeed();
  } else {
    // Stop the motor by not calling runSpeed()
    // The stepper will be disabled, allowing it to move freely
    digitalWrite(stepPin, LOW); // Ensure step pin is low
  }
}

// Simple debounce function
bool debounceButton(int buttonPin, bool *lastButtonState) {
  bool reading = digitalRead(buttonPin);
  if (reading != *lastButtonState) {
    lastDebounceTime = millis();
  }
  if ((millis() - lastDebounceTime) > debounceDelay) {
    if (reading == LOW) {
      *lastButtonState = reading;
      return true;
    }
  }
  *lastButtonState = reading;
  return false;
}

I moved your topic to an appropriate forum category @mws12345.

In the future, please take some time to pick the forum category that best suits the subject of your topic. There is an "About the _____ category" topic at the top of each category that explains its purpose.

This is an important part of responsible forum usage, as explained in the "How to get the best out of this forum" guide. The guide contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

1 Like

...or power supply...
What voltage are you supplying?

I'm using a 12V power supply with 1.5 AMP Output capacity

This motor is a high impedance stepper ( 12V/0,33A ). Not suited for high speed applications, and not a suitable choice for this driver. In the code you don't use acceleration. In general all steppers cannot go from standstill to max speed without acceleration.

I'll try a new code with acceleration, in the meanwhile if this does not work, should i look for a NEMA 17 with 1A or more for bigger speeds?

With a chopper driver, supply voltage for the driver should at least be twice the voltage of the motor.

If you want to keep your motor, then increase the supply voltage to at least 24volt.
If you want to keep your driver, then get a motor with a lower voltage.

A motor with a lower voltage (and higher current) can usually be used at higher speeds.
Make sure you set your driver to the correct motor current, before you connect the motor.
Leo..