Using setEnablePin and setPinsInverted in Accellstepper

I am trying to control the enable line on a stepper motor. Although I have closely copied several examples I have not been able to get these to work.

#include <AccelStepper.h>

AccelStepper stepper(1, 12, 13); // (Use A4988 physical driver with 2 pins, STEP, DIR)

// #define enablePin 9

void setup() {

  stepper.setEnablePin(9);
  stepper.setPinsInverted(false, false, true);
  stepper.setMaxSpeed(200);
  stepper.setAcceleration(1000);
  stepper.setCurrentPosition(0);
 
//  pinMode(enablePin, OUTPUT);
//  digitalWrite(enablePin, LOW);
}

void loop() {

  stepper.moveTo(200); 
  stepper.runToPosition(); 
  delay(1000);

// more of the same in here
  
  }

If I comment out
stepper.setEnablePin(9);
stepper.setPinsInverted(false, false, true);
and uncomment the commented lines it works as expected

Clarification of what I have misunderstood will be greatly appreciated.

Mike

This bypasses the AccelStepper library. If you want to enable the stepper with the AccelStepper library you should use: stepper.enableOutputs();. To disable the stepper you should use stepper.disableOutputs();

Thank you johnwasser, I will try that. Having said that, I have read that stepper.runToPosition() should enable the outputs and that stepper.enableOutputs() should only be used if the stepper has been disabled by stepper.disableOutputs()
Mike

I don't see that in the documentation. The documentation implies that if you call disableOutputs() you have to call enableOutputs() before you can move.

https://www.airspayce.com/mikem/arduino/AccelStepper/classAccelStepper.html

Thanks again, that worked well and you have helped to fill in a bit of my misunderstanding.

For info, where I failed is that I thought that " ....Called automatically by the constructor. If the enable Pin is defined, sets it to OUTPUT mode and sets the pin to enabled." meant that it would be enabled implicitly.

Mike

That won't do much good since you can't call "stepper.setEnablePin(9);" or "stepper.setPinsInverted(false, false, true);" until AFTER the 'stepper' object is constructed.

For safety I would add:
stepper.enableOutputs();
after:

  stepper.setEnablePin(9);
  stepper.setPinsInverted(false, false, true);

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