Running NEMA34 with Arduino UNO and On/Off Switch

Hi there
I am new in the arduino world. I tried a project running a stepper motor using arduino uno and an simple on off switch.

The aim is that, if I switch on the switch the motor runs the max power and runs untill I switch it off. Basically a simple thing

I got help regarding the code, did upload libraries and the code to the device, wired everything up, but: no move.
Only fuse (the motor is not moving but )

Would be nice if someone could have a look at my setup. Probably you see what is wrong.

Thanks in advance

#include <AccelStepper.h>
// Define the stepper motor pins
#define STEP_PIN 3     // Pin for sending step pulses to the HBS86H
#define DIR_PIN 4      // Pin for direction control (clockwise/counterclockwise)
#define ENABLE_PIN 5   // Pin to enable/disable the motor (LOW enables the motor)
// Define the on/off switch pin
const int switchPin = 2;   // Pin for the on/off switch
bool switchState = false;   // To track the state of the switch

// Create an instance of the AccelStepper library
AccelStepper stepper(AccelStepper::DRIVER, STEP_PIN, DIR_PIN);

void setup() {
  // Initialize the pins
  pinMode(switchPin, INPUT);  // Set the switch pin as input
  pinMode(ENABLE_PIN, OUTPUT); // Set the enable pin as output
  digitalWrite(ENABLE_PIN, LOW);  // Enable the stepper driver (LOW to enable)

  // Set the maximum speed and acceleration for the motor
  stepper.setMaxSpeed(1500);  // Maximum speed in steps per second (adjust as needed)
  stepper.setAcceleration(500);  // Acceleration in steps per second^2 (adjust as needed)
}
void loop() {
  // Read the state of the on/off switch
  switchState = digitalRead(switchPin);

  if (switchState == HIGH) {  // When the switch is ON
    // Start the motor with maximum speed and constant torque
    if (!stepper.isRunning()) {
      stepper.setSpeed(1500);  // Set motor speed to maximum (adjust as needed)
    }
    stepper.runSpeed();  // Continuously run the motor at the set speed
  } 
  else {  // When the switch is OFF
    // Gradually decelerate the motor and stop it
    stepper.setSpeed(0);  // Set speed to 0 to stop the motor
    stepper.runSpeed();  // Gradual deceleration to stop the motor
    }
   // Ensure smooth operation by calling run() regularly
  stepper.run();
}

Would be nice if you followed the forum instructions for posting sketches.

hi. if you mean regarding the code I tried. But I had no idea how to format the code. sorry.
Did you mean this?

got the code formated now :slight_smile:

I am fairly sure that it should be HIGH to enable the driver (according to the datasheet for HBS86H).

1 Like

and your switch state is LOW when you press the button / close the switch.
Also, how is your stepper powered?

Then you also need a power supply with the highest voltage your controller board can support and set the current limit on the controller board to match the maximum current your stepper motor can handle. You have not supplied any data on either device, so no further help can be given.

Also, better to make that :

pinMode(switchPin, INPUT_PULLUP); // Set the switch pin as input

1 Like

48V, 12.5A

You mean this?

hi. thanks for the hint. will try it

You didn't tell anything about your driver / motor (except it's nema34).

Sorry. NEMA 34 12.5Nm, 6A, 1.8dec

I changed the code as you guys sugested. Motor is still not running. However, Now I can measure 5V for all pins but not for DIR.

What else can I try?

Provide information about your driver and settings that you have on that.
Also wiring including the driver, motor and the power supply, which are missing on your original post...
Also, did you consider post #6 for HIGH/LOW trigger?

I changed to if (switchState == HIGH) {...

Let me please do some pics...

back asap

But it should be LOW according to your comments.
Doesn't matter, on one or another position it should start the motor anyway...

Here the fotos:


Here I changed to 1600 (on/off/on/on)and also in the scrip to 1600 (instead of 1500). Probably better? Don't know

here a better view of the wiring:

Quite chinese... I can't even tell which polarity one should "pick" for DC supply...