Nema23 Stepper motor

I am working on a system that uses a stepper motor. It is a 1000mm linear rail. I have a DM542T stepper driver. I want it to move 250mm and then stop for a few seconds, then move another 500mm and stop for a few seconds, and then come back to the starting position. The starting position is in the very corner from where it first started moving. I am running into a few problems. This is my first time using stepper motors.

  1. I use this formula where i provide the distance in mm and it runs the steps according to it.
    "targetPosition_steps = targetPosition_mm / TRAVEL_PER_REVOLUTION * STEPS_PER_REVOLUTION;''
    However, instead of working at 250 mm and 500mm, it works at 960 and 1235 respectively.

  2. It doesn't go back to the starting position but goes back to the 250mm position.

  3. If I shut off the driver or unplug the motor, it starts moving in a completely different way than it is intended to.

I have provided the code below. Right now, It stops at the positions I mentioned but goes further after the second position and comes back to stop at 250mm. I have no idea why because it is supposed to stop at those two positions and come back.

Please let me know how to fix this and what i am doing wrong.

#include <AccelStepper.h>
#include <EEPROM.h> // Include EEPROM library

// Define stepper motor connections and parameters
#define STEP_PIN 9
#define DIR_PIN 8
#define STEPS_PER_REVOLUTION 200 // Assuming a standard 1.8 degree stepper motor
#define TRAVEL_PER_REVOLUTION 1  // Assuming 1mm travel per revolution of the lead screw

// Create stepper motor object
AccelStepper stepper(AccelStepper::DRIVER, STEP_PIN, DIR_PIN);

// Define the original position
const int ORIGINAL_POSITION_ADDRESS = 0; // EEPROM address to store original position
int originalPosition = 0; // Variable to store original position

void setup() {
  // Set maximum speed and acceleration
  stepper.setMaxSpeed(1000);     // Set the maximum speed in steps per second
  stepper.setAcceleration(1000); // Set the acceleration in steps per second^2

  // Read original position from EEPROM
  originalPosition = EEPROM.read(ORIGINAL_POSITION_ADDRESS);
  // Set initial position to original position
  stepper.setCurrentPosition(originalPosition);
}

void loop() {
  // Store the original position
  originalPosition = stepper.currentPosition();

  // Move the motor to 250mm position in forward direction
  moveStepperToPosition(960);
  delay(1000); // Stop for 1 second

  // Move the motor to another 500mm position in forward direction
  moveStepperToPosition(1235);
  delay(1000); // Stop for 1 second

  // Move the motor back to the original position
  moveStepperToPosition(originalPosition);
}

void moveStepperToPosition(float targetPosition_mm) {
  int targetPosition_steps = targetPosition_mm / TRAVEL_PER_REVOLUTION * STEPS_PER_REVOLUTION;
  
  // Determine direction based on target position relative to current position
  if (targetPosition_steps > stepper.currentPosition()) {
    stepper.setPinsInverted(false, false, true); // Set direction to move forward
  } else {
    stepper.setPinsInverted(false, false, false); // Set direction to move backward
  }

  stepper.moveTo(targetPosition_steps);

  // Run the motor until it reaches the target position
  while (stepper.distanceToGo() != 0 || abs(stepper.currentPosition() - targetPosition_steps) > 1) {
    stepper.run();
  }
}




Have you tested and confirmed that this is the case for your motor?

??? How can the motor move if you shut the driver off?

I don't see in your code where you move the motor to a reference point when starting ( homing ).
A stepper gives no fedback where it is positioned. So you must move the stepper to a reference point where it actuates a switch. After that you can position it absolutely by counting the steps ( what does the Accelstepper library ).

Sorry for the confusion, I meant if I unplug it and plug it in again, then it starts moving in a different manner. Can you please tell me how to set a reference point?

The reference point or zero step is found with code in your setup() function. You are the only one who knows what your linear rail is, but it will need a micro switch or other sensor set at one end of the rail or the other. The sensor will switch when the traveler on the rail reaches the switch.
You code a loop to test for the switch changing and if not, move the traveler one step closer to the switch. When the traveler toggles the switch, move the traveler ONE step away from the switch and this position is the home or ZERO position for your stepper motor.

Can I use a limit switch for this purpose?

Certainly. But if you count the steps properly, you never need a "limit switch".

Oh, If your limit switch actually cuts the power to your project, then you need a switch that will only be used to indicate the zero position.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.