StepperDriver library problem - Motor energized but not moving

Hi, I'm using the StepperDriver library (by Laurentiu Badea - laurb9), as it seems to come up every time I google for a library to use with the DRV8825, to drive a geared stepper motor that rotates a disc, using the NonBlocking method from its examples, the problem is that the motor energizes but doesn't move. I know my wiring and chip is fine by testing it with the example by commenting/uncommenting the correct options and changed the pins, and it work fine there but not with my code.

Here is the setup void where it should drive the motor, the command is also used in the loop with similar code, it is called in the same way except the stepper.begin() which is called only on the setup.

void setup() {

  pinMode(HOME_SENS, INPUT_PULLUP);
  pinMode(FILTER_SENS, INPUT_PULLUP);

  Serial.begin(57600);
  delay(5000);
  stepper.begin(RPM, MICROSTEPS);

  if (digitalRead(HOME_SENS) == HIGH){
    Serial.println("NotHomed#");
    stepper.enable();
    stepper.startMove(15000);
    while (digitalRead(HOME_SENS) == HIGH){
      digitalRead(HOME_SENS);
      digitalWrite(13, HIGH);
    }
  }
  if (digitalRead(HOME_SENS) == LOW){
    stepper.stop();
    stepper.disable();
    current_pos = 1;
    Serial.println("Homed#");
    digitalWrite(13, LOW);
  }
}

If HOME_SENS is left HIGH the motor is energized and the LED turns on and when put LOW it de-energizes and the LED turns off, it goes to loop and when it is called to rotate until a variable changes it does the same thing, energizes but doesn't turn.

Can someone point my obvious mistake, I have looked at the code so many times and every time it makes sense in my mind, I know the while loop is blocking but shouldn't the motor rotate since the startMove is supposed to be nonblocking?

To check if you have everything connected properly try the first example from this Simple Stepper Code. It does not need any library.

I am not familiar with the library you mention. I believe the AccelStepper library is more common.

...R Stepper Motor Basics

As I said, it does work with at least a couple of the examples of the StepperDriver library. but not in my code. It is the library found in the resources of the Pololu driver you where using on the Simple Stepper Code thread, and what you find when writing “DRV8825 arduino” on Google.

About the AccelStepper library, I checked it but it definitely is missing some basic commenting in it’s examples to actually understand how to use it, from the few things I understood from the website you linked i believe I got it working to the point that it energizes the motor but nothing more.
It is definitely missing a lot of info for someone who is just starting coding and has only seen basic Arduino code, if I hadn’t a lil bit more understanding of C, I don’t expect to have understood how to set pins etc.

ckambiselis: About the AccelStepper library, I checked it but it definitely is missing some basic commenting in it's examples to actually understand how to use it,

It is one of the very few Arduino libraries with good documentation.

Have you tried my simple examples?

...R

If you mean the one on the "Simple Stepper Programming" thread, yes, and it does work, as it did with the StepperDriver library.

Yes that documentation is great, the classical way it is written in most manufacturers datasheets, for anyone fluent in C/C++.

But I think the point of my initial post is missed here, I'm not trying to find a different library, I can basically move the motor without a library anyway, I do understand how steppers and their drivers work, what I don't get is why it doesn't want to move when used in my code.

Basically is there someone that uses the StepperDriver library in a way that the stepper motor turns "indefinitely" until it is stopped by an interaction.

ckambiselis: Basically is there someone that uses the StepperDriver library in a way that the stepper motor turns "indefinitely" until it is stopped by an interaction.

You can do that with the AccelStepper library - though it is one of the things that is not clear in the documentation. Use the function runSpeed(). Stop calling that function when you want the motor to stop.

Or, a small modification of my example code will make the motor run for as long as you want.

...R

Well this is what I had tested yesterday, which energized the motor but didn’t move it, and when it should have de-energized it it didn’t. So I feel like the problem is in the way the steps are generated in both the libraries collides with something or I’m missing something in the code. I can also confirm that the STEP pin doesn’t output a signal with the code below or the code in my first post (got my DMM hooked and measuring pulse width).

#include <AccelStepper.h>

#define FILTER_SENS 12
#define HOME_SENS 4
#define DIR 5
#define STEP 9
#define enablePin 10
#define MOTOR_STEPS 720
#define RPM 14400
#define MICROSTEPS 1
int set_pos = 1;
int current_pos;
AccelStepper stepper(AccelStepper::DRIVER, STEP, DIR);


void setup() {

  pinMode(HOME_SENS, INPUT_PULLUP);
  pinMode(FILTER_SENS, INPUT_PULLUP);

  Serial.begin(57600);
  delay(5000);
  stepper.setSpeed(RPM);

  if (digitalRead(HOME_SENS) == HIGH){
    Serial.println("NotHomed#");
    stepper.runSpeed();
    while (digitalRead(HOME_SENS) == HIGH){
      digitalRead(HOME_SENS);
      digitalWrite(13, HIGH);
    }
  }
  if (digitalRead(HOME_SENS) == LOW){
    stepper.stop();
    stepper.disableOutputs();
    current_pos = 1;
    Serial.println("Homed#");
    digitalWrite(13, LOW);
  }
}

I did tho make it work as I needed, at least from preliminary tests, using the TimerOne library to adjust PWM period and pulse width, technically making a fully adjustable analogWrite, not as easy as the libraries made for the tasks but close enough for my project.

ps I realized that I have set steps per second to 14400 but I have also tried it with values under 1000 and nothing happens.

You said that you were able to make the motors move with my simple code. Before I look at the code in Reply #6 please post the code from my example that YOU uploaded to your Arduino and which worked. I want to be able to compare the actual working code with the problem code.

...R

I mean I basically copy pasted the code from the thread and changed the pin numbers, numberOfSteps and the millisbetweenSteps, since my stepper is a 720 step geared.

// testing a stepper motor with a Pololu A4988 driver board or equivalent
// on an Uno the onboard led will flash with each step
// this version uses delay() to manage timing

byte directionPin = 5;
byte stepPin = 9;
int numberOfSteps = 400;
byte ledPin = 13;
int pulseWidthMicros = 20;  // microseconds
int millisbetweenSteps = 100; // milliseconds - or try 1000 for slower steps


void setup() { 

  Serial.begin(9600);
  Serial.println("Starting StepperTest");
  digitalWrite(ledPin, LOW);
  
  delay(2000);

  pinMode(directionPin, OUTPUT);
  pinMode(stepPin, OUTPUT);
  pinMode(ledPin, OUTPUT);
  
 
  digitalWrite(directionPin, HIGH);
  for(int n = 0; n < numberOfSteps; n++) {
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(pulseWidthMicros); // this line is probably unnecessary
    digitalWrite(stepPin, LOW);
    
    delay(millisbetweenSteps);
    
    digitalWrite(ledPin, !digitalRead(ledPin));
  }
  
  delay(3000);
  

  digitalWrite(directionPin, LOW);
  for(int n = 0; n < numberOfSteps; n++) {
    digitalWrite(stepPin, HIGH);
    // delayMicroseconds(pulseWidthMicros); // probably not needed
    digitalWrite(stepPin, LOW);
    
    delay(millisbetweenSteps);
    
    digitalWrite(ledPin, !digitalRead(ledPin));
  }
}

void loop() { 
}

Try changing this

    stepper.runSpeed();
    while (digitalRead(HOME_SENS) == HIGH){
      digitalRead(HOME_SENS);
      digitalWrite(13, HIGH);
    }

to this

    while (digitalRead(HOME_SENS) == HIGH){
      digitalRead(HOME_SENS);
      digitalWrite(13, HIGH);
      stepper.runSpeed();
    }

You have to call runSpeed() repeatedly and much more frequently than the step rate.

…R

Robin2:     while (digitalRead(HOME_SENS) == HIGH){       digitalRead(HOME_SENS);       digitalWrite(13, HIGH);       stepper.runSpeed();     }

You have to call runSpeed() repeatedly and much more frequently than the step rate.

...R

Now it is rotating at one step per second, exactly. I also tried changing the RPM to a smaller value, in case it was too fast for the library, but the speed doesn't change. It does stop rotating when HOME_SENS goes LOW but the motor is still energized.

ckambiselis: Now it is rotating at one step per second, exactly. I also tried changing the RPM to a smaller value, in case it was too fast for the library, but the speed doesn't change.

You need to post the latest version of your program if you want help with it.

but the motor is still energized.

It is normal for stepper motors to stay energized - that is how they hold position.

...R

#include <AccelStepper.h>

#define FILTER_SENS 12
#define HOME_SENS 4
#define DIR 5
#define STEP 9
#define enablePin 10
#define MOTOR_STEPS 720
#define RPM 14400
#define MICROSTEPS 1
int set_pos = 1;
int current_pos;
AccelStepper stepper(AccelStepper::DRIVER, STEP, DIR);


void setup() {

  pinMode(HOME_SENS, INPUT_PULLUP);
  pinMode(FILTER_SENS, INPUT_PULLUP);

  Serial.begin(57600);
  delay(5000);
  stepper.setSpeed(RPM);

  if (digitalRead(HOME_SENS) == HIGH){
    Serial.println("NotHomed#");
    while (digitalRead(HOME_SENS) == HIGH){
      digitalRead(HOME_SENS);
      digitalWrite(13, HIGH);
      stepper.runSpeed();
    }
  }
  if (digitalRead(HOME_SENS) == LOW){
    stepper.stop();
    stepper.disableOutputs();
    current_pos = 1;
    Serial.println("Homed#");
    digitalWrite(13, LOW);
  }
}

In my case the motor doesn’t need to be energized when stopped since the position is being held by other mechanical means. I expect that stepper.disableOutputs(); should de-energize the the windings unless I didn’t understand the documentation.

Actually forget the whole thing, gonna close the thread later.

I don't think you have read the documentation carefully. The function setSpeed() sets the number of steps per second and not the RPM. Also it cannot produce 14400 steps per second

#define RPM 14400
stepper.setSpeed(RPM);

I believe you have to call setEnablePin() to tell it which pin is used to enable/disable the motor

...R

Robin2: I don't think you have read the documentation carefully. The function setSpeed() sets the number of steps per second and not the RPM. Also it cannot produce 14400 steps per second

#define RPM 14400
stepper.setSpeed(RPM);

I believe you have to call setEnablePin() to tell it which pin is used to enable/disable the motor

...R

ckambiselis: ......ps I realized that I have set steps per second to 14400 but I have also tried it with values under 1000 and nothing happens.

ckambiselis: ......ps I realized that I have set steps per second to 14400 but I have also tried it with values under 1000 and nothing happens.

What happens if you try it at 5 steps per second?

...R