AccelStepper enabling acceleration

I hope I might be able to hear a suggestion or two here without receiving personal insults. Please just keep scrolling if this is too much to ask.

I'm trying to get a stepper motor to run with acceleration/deceleration using an Uno with TMC2208 stepper driver and the AccelStepper library. The examples I have seen have all used a line like this in setup...

stepper1.setAcceleration(100);

But I can't get it to operate with any observable acceleration and the motor just runs with abrupt starts and stops instead. Here is the whole sketch, which I modified from an example for the TMC2208...

#define stepPin 11
#define dirPin 12 
#define EN_PIN    9
#include <TMC2208Stepper.h>
#include <AccelStepper.h>

AccelStepper stepper1(1, stepPin, dirPin);

 
void setup() {

  pinMode(stepPin,OUTPUT); 
  pinMode(dirPin,OUTPUT);
    pinMode(EN_PIN, OUTPUT);

     stepper1.setAcceleration(100);
     

}
void loop() {
  digitalWrite(dirPin,HIGH); 
  for(int x = 0; x < 5000; x++) {
 
    digitalWrite(stepPin,HIGH); 
    delayMicroseconds(100);    
    digitalWrite(stepPin,LOW); 
    delayMicroseconds(50); 
 
  }
  delay(500); 
  
  digitalWrite(dirPin,LOW);
  for(int x = 0; x < 5000; x++) {
    digitalWrite(stepPin,HIGH);
    delayMicroseconds(100);
    digitalWrite(stepPin,LOW);
    delayMicroseconds(50);

  }
  delay(500);
}

What am I missing?

You're not using accel stepper, you're just manually controlling the timing and motion of your stepper with:

Look into stepper1.move(5000); and stepper1.run(); or others per examples like:

Thanks. I wondered about a conflict like that. And I had wondered if setAcceleration would apply globally.

OK here's an edit still with no result. What am I missing?

// defines pins
#define stepPin 11
#define dirPin 12 
#define EN_PIN    9
#include <TMC2208Stepper.h>
#include <AccelStepper.h>

AccelStepper stepper1(1, stepPin, dirPin);

 
void setup() {

  pinMode(stepPin,OUTPUT); 
  pinMode(dirPin,OUTPUT);
    pinMode(EN_PIN, OUTPUT);


     

}
void loop()
{
 delay(1000);
  stepper1.move(200);
  stepper1.setAcceleration(100);
  stepper1.run();
    }

You deleted too much.

// defines pins
#define stepPin 11
#define dirPin 12
#define EN_PIN    9
//#include <TMC2208Stepper.h> // You don't need this in this sketch
#include <AccelStepper.h>

AccelStepper stepper1(1, stepPin, dirPin);


void setup() {
  pinMode(EN_PIN, OUTPUT);
  digitalWrite( EN_PIN, LOW );
  stepper1.setMaxSpeed( 1000 );

}
void loop()
{
  if ( stepper1.distanceToGo() == 0 ) {
    delay(1000);
    stepper1.move(200);
    stepper1.setAcceleration(100);
  }
  stepper1.run();
}
1 Like

This is an alternative Version with the MobaTools library. Maybe it's a little bit more easy :wink:

// defines pins
#define stepPin 11
#define dirPin 12
#define EN_PIN    9
//#include <TMC2208Stepper.h> // You don't need this in this sketch
#include <MobaTools.h>

MoToStepper stepper1(200, STEPDIR);

void setup() {
  stepper1.attach(stepPin, dirPin);
  stepper1.attachEnable( EN_PIN, 100, LOW ); // disable stepper while not moving
  stepper1.setSpeedSteps( 10000, 100 ); // 1000 steps/sec, ramp length 100 steps

}
void loop()
{
  delay(1000);            // start move every second
  stepper1.move(200);  // this is not blocking, stepper moves during delay()
}
1 Like

Thanks very much. I got this to work with acceleration. I need to control forward and reverse direction. I tried some things with dirPin, but with no luck yet. Any suggestions?

#define stepPin 11
#define dirPin 12
#define EN_PIN    9
#include <AccelStepper.h>

AccelStepper stepper1(1, stepPin, dirPin);


void setup() {
  pinMode(EN_PIN, OUTPUT);
  digitalWrite( EN_PIN, LOW );
  stepper1.setMaxSpeed( 5000 );

}
void loop()
{
  if ( stepper1.distanceToGo() == 0 ) {
    delay(1000);
    stepper1.move(5000);
    stepper1.setAcceleration(3000);
  }
  stepper1.run();
}

Try negative numbers:

stepper1.move(-5000);

or moveTo()

1 Like

This needs to be said; Why did you accuse me, or any of the thousands of readers, of this? Maybe you should... "Keep scrolling if you must start your post with insults."

Thanks to DaveX and MicroBahner for providing clear, helpful suggestions in a friendly way. And thanks to you for providing an example to illustrate the kind of response that I was hoping to avoid. In the few times I have visited this board I have usually received responses with your kind of tone. Read through posts here and you will find many other examples of beginners being treated in a similar manner. I did not want to hear any more of that and that is why I included this basic request at the top.

Got it. That worked. Thanks very much.

I've tried setMaxSpeed setSpeed with very high values, but the stepper is only spinning at about 100rpm max. Any ideas for faster speeds? It's not critical, but more like 300rpm would be better for this application.

I loaded MobaTools and tried that sketch, but the motor wouldn't budge. I'll try to experiment some more with it though.

I consider myself "new" at three years in this hobby. I have read a LOT on this forum (because I need it and it interests me). It is my observation that you have skipped the parts where "beginners" ignore the rules, then ignore the requests to "please follow the rules" then lean on "my homework is due tomorrow" with repetitious use of "give me the codes"... but you head straight for the money shot. Your last post was a year ago and upon your return, you open topic and lead with "You jerks better not be jerks, or else."

TL;DR: I think you focused on the wrong thing and accused thousands of kind people of your own behaviour. I hope you recover and visit more often. I have.

...your own behaviour

I hope you recover...

What is the point of responding with personal insults? If you read something you don't like, why not just ignore it, suggest a better way to ask the question or express plain displeasure? What do you think you're accomplishing?

For the record, you've convinced me and I'm permanently done with this board.

"Your own behaviour" is an insult? Wow. Bullseye.

You make your choices, I make mine.

I did.

I doubt it.

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