Stepper driver steps not accurate

Hi All,

I have a project that is up and running for some time now but the stepper drivers require more steps than what the DIP switches are set too.

I'm using an ST-M5045 stepper driver that is set for 400 steps but in order to get 1 revolution I have to put 1600 steps into my code to get it to make that 1 revolution and would love some idea where I should look for my error.
Here's a data sheet for the driver

I have the DIP switches set for 400 steps(sw 4 on & sw5 - 8 all set to on) with current set to 2A(sw1 on, sw2 off, sw3 on)

I have the driver wired as follows to my UNO R3,

PUL+ pin 5
Pul - GND
DIR+ pin 6
DIR- GND

Like I said above it is working but would like to understand where I went wrong as far as the stepping goes and I finally have time to find out, I would post the code but it is a rather lengthy program and I don't think my problem lies with the code.
Thanks

If your code is too lengthy to post, trim it down. Write the bare minimum sketch that shows your unexpected behavior. Post that.

Going to the mechanic and saying "my car wont start but don't check the battery because I am pretty sure it is good" will get you a funny look from the mechanic and then he will check you battery as the first step.

I have the DIP switches set for 400 steps(sw 4 on & sw5 - 8 all set to on) with current set to 2A(sw1 on, sw2 off, sw3 on)

Try some other settings.

You have the drive set for microsteps times 2, 2 steps from controller for 1 step on motor, do you have a 400 step per revolution motor? Post a link to the motor.

I have a 1.8 deg. 200 step unipolar motor set up in Bi-polar mode(using 4 wires) 200 oz-in with a 36V 4A power supply. I trimmed the code down to moving the motor at the press of a button.

#include <Stepper.h>

Stepper tableSlider = Stepper(200, 5, 6);

const int button = 2;            // pin button is on
int val = 0;                     // current button state
int old_val = 0;
int state = 0;
unsigned long previousMillis = 0;


void setup() {

  pinMode(button, INPUT_PULLUP);
  tableSlider.setSpeed(200);  //  table slide

}

void loop() {

  val = digitalRead(button);                    // read input value and store it
  unsigned long currentMillis = millis();       //check for current time


  //give time for debouncing
  if (currentMillis - previousMillis >= 100) {

    if ((val == LOW) && (old_val == HIGH)) {
      state = 1 - state;                    // change the state
    }
    previousMillis = currentMillis;         // save the last time the button was ptressed

    old_val = val;                       // val is now old, let's store it
  }
  if (state == 1) {
    tableSlider.step(-1600);           //slide table in

    state = 0;
  }

}

The standard stepper library is not intended for stepper drivers that take step and direction signals. Try the AccelStepper library and use its DRIVER option.

Or just write your own code.

...R
Stepper Motor Basics
Simple Stepper Code

Stepper doesn't ramp up step rate upon starting, this will cause most steppers to miss-step (only
very small motors tolerate this sort of instant-speedup).

The AccelStepper library does what's needed.

I guess that's why you guys get the big bucks, it was code related but I still have a problem getting it to act the same as my old code. I can run the stepper only once with the press of the button and then have to hit reset on the controller to make it move again with the button press using the Accelstepper.h library. On the stepper.h library I just changed the state at the end of the program and then was able to press the button to start everything again, but if I write state = 0 at the end of the program I can feel the motor shutter ever so slightly but not run to a position. I'm not sure why it would be any different but hopefully someone can point me in the right direction.
Thanks

#include <AccelStepper.h>

AccelStepper tableSlider(AccelStepper::DRIVER, 5, 6);

const int button = 2;            // pin button is on
int val = 0;                     // current button state
int old_val = 0;
int state = 0;
unsigned long previousMillis = 0;


void setup() {

  pinMode(button, INPUT_PULLUP);
  tableSlider.setMaxSpeed(1000);
  tableSlider.setAcceleration(400);
}

void loop() {
  val = digitalRead(button);                    // read input value and store it
  unsigned long currentMillis = millis();       //check for current time


  //give time for debouncing
  if (currentMillis - previousMillis >= 100) {

    if ((val == LOW) && (old_val == HIGH)) {
      state = 1 - state;                    // change the state
    }
    previousMillis = currentMillis;         // save the last time the button was ptressed

    old_val = val;                       // val is now old, let's store it
  }
  if (state == 1) {
    tableSlider.moveTo(-400);
    tableSlider.run();
    //state = 0;
  }

}

You should not have the line

tableSlider.run();

inside any IF statement. It should just be inside loop() so it is called as often as possible. The library will stop sending pulses when the motor reaches its destination

I'm not sure if you should be using moveTo() or move()

...R

Robin2:
You should not have the line

tableSlider.run();

inside any IF statement. It should just be inside loop() so it is called as often as possible. The library will stop sending pulses when the motor reaches its destination

I'm not sure if you should be using moveTo() or move()

...R

I tried moving tableSlider.run(); out of the IF statement and it acted the same, the stepper moved the assigned amount of steps and stopped but a button press wouldn't start it again.

I tried move() instead of moveTo() and the stepper didn't move at all so I went back to moveTo().

The only problem I have is making the stepper move every time I press the button. On the old code I would just change the STATE of the button to low and the stepper would move on the next press of the button but the Accelstepper doesn't seem to allow that.

Perhaps I need another IF statement to change the button STATE ?

Try this version

#include <AccelStepper.h>

AccelStepper tableSlider(AccelStepper::DRIVER, 5, 6);

const int button = 2;            // pin button is on
int val = 0;                     // current button state
int old_val = 0;
int state = 0;
unsigned long previousMillis = 0;


void setup() {

    pinMode(button, INPUT_PULLUP);
    tableSlider.setMaxSpeed(1000);
    tableSlider.setAcceleration(400);
}

void loop() {

    unsigned long currentMillis = millis();
    if (currentMillis - previousMillis >= 100) {
        val = digitalRead(button);
        if ((val == LOW) && (old_val == HIGH)) {
            state = 1;
        }
        previousMillis = currentMillis;
        old_val = val;
    }
    if (state == 1) {
        tableSlider.move(-400);
        state = 0;
    }
    
    tableSlider.run();
}

...R

Robin2:
Try this version

#include <AccelStepper.h>

if (state == 1) {
        tableSlider.move(-400);
        state = 0;
    }
   
    tableSlider.run();
}




...R

I tried that this morning, it only runs the code once and then I have to reset the controller to make the push button start it again.

ribbonman:
I tried that this morning, it only runs the code once and then I have to reset the controller to make the push button start it again.

I just tried it now and it works fine. Every time I ground the wire in pin2 it moves my motor 400 steps.

...R

Robin2:
I just tried it now and it works fine. Every time I ground the wire in pin2 it moves my motor 400 steps.

...R

Weird, I tried it again and it didn't work but I copy and pasted what you had and it did, I still can't see what I had wrong and I have them side by side on the screen.

Thanks for the help, I thought it should have worked like the old code but just couldn't figure out where I was wrong.

I seem to have a gremlin, the code from post #10(completed code) and the previous code that used delay instead of millis will now operate randomly. I turn the arduino on and leave it sit and the stepper will operate without doing anything(no button press).

I have disconnected the button from the arduino and it will still operate randomly, is this some kind of random spike going to the arduino?

Any thoughts of where to look?

Post the actual program that you have uploaded to YOUR Arduino.

Make a pencil drawing showing all your connections and post a photo of the drawing.

...R

I retraced my wiring and connected to the NC terminal on the switch instead of the NO and it doesn't operate randomly any more, not sure why that matters but I will search it and try to learn why.
Thanks for the help Robin2.

Here's the program just in case.

#include <AccelStepper.h>

AccelStepper stapler(AccelStepper::DRIVER, 5, 6);

const int button = 2;            // pin button is on
int val = 0;                     // current button state
int old_val = 0;
int buttonstate = 0;
unsigned long previousMillis = 0;

void setup() {

  pinMode(button, INPUT_PULLUP);
  stapler.setMaxSpeed(1000);
  stapler.setAcceleration(6000);

}

void loop() {

  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= 100) {
    val = digitalRead(button);
    if ((val == LOW) && (old_val == HIGH)) {
      buttonstate = 1;
    }
    previousMillis = currentMillis;
    old_val = val;
  }
  if (buttonstate == 1) {
    stapler.move(400);
    buttonstate = 0;
  }

  stapler.run();
}