Controlling Stepper to Start Same Direction Each Time

Hi,

I'm having trouble with an art installation in which I am having a lead screw attached to a NEMA 17 stepper motor move a monitor back and forth. The issue is a that ~90% of the time when the Arduino is turned on the motor starts in turning clockwise and 10% it rotates counter clockwise. Ideally I want it to rotate the same direction each time from power on. How can I do this? Below is the basic script I am using. The driver for the motor is a TB 6600 4.5A one I bought from Amazon. Any help would be appreciated, thanks.

#include <AccelStepper.h>


// Define a stepper motor 1 for arduino 
// direction Digital 9 (CW), pulses Digital 8 (CLK)
AccelStepper stepper(1, 8, 9);
void setup()
{  
  // Change these to suit your stepper if you want
  stepper.setMaxSpeed(1000);//1100
  stepper.setAcceleration(1100);
  stepper.moveTo(19000);
}

void loop()
{
    // If at the end of travel go to the other end
    if (stepper.distanceToGo() == 0){
      stepper.moveTo( -stepper.currentPosition() );
    }
    
    stepper.run();
}

scg80:
Hi,

I'm having trouble with an art installation in which I am having a lead screw attached to a NEMA 17 stepper motor move a monitor back and forth. The issue is a that ~90% of the time when the Arduino is turned on the motor starts in turning clockwise and 10% it rotates counter clockwise. Ideally I want it to rotate the same direction each time from power on. How can I do this? Below is the basic script I am using. The driver for the motor is a TB 6600 4.5A one I bought from Amazon. Any help would be appreciated, thanks.

#include <AccelStepper.h>

// Define a stepper motor 1 for arduino
// direction Digital 9 (CW), pulses Digital 8 (CLK)
AccelStepper stepper(1, 8, 9);
void setup()
{  
 // Change these to suit your stepper if you want
 stepper.setMaxSpeed(1000);//1100
 stepper.setAcceleration(1100);
 stepper.moveTo(19000);
}

void loop()
{
   // If at the end of travel go to the other end
   if (stepper.distanceToGo() == 0){
     stepper.moveTo( -stepper.currentPosition() );
   }
   
   stepper.run();
}

Where do you ever set pin 9 to anything? The comment tells you what to set it to for each direction.
Paul

Post a diagram or picture of your connections.

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