Accelstepper Homing with TB6600

465 views
Skip to first unread message

andrew heron

unread,
May 28, 2019, 7:12:50 AM5/28/19
to accelstepper

Hi;

I am new to programming so would like some help.  I am using a NEMA 23 with Arduino, TB6600 and accelstepper.

My goal is to set the system to home to a position and then return back to a set position.

The home movement is counterclockwise and the move to the new position is clockwise.

Based upon the program I have below it moves to the home position fine, but then continues in the same direction.   It doesn’t change direction.

 

Hoping you can help.

 

/*  Motor Homing code using AccelStepper */

#include <AccelStepper.h>

// AccelStepper Setup

// Define a stepper and the pins it will use

 

AccelStepper stepperX(AccelStepper::DRIVER, 6, 7);  // pin 6 -> PUL+, pin 7 ->  DIR+

 

// Define the Pins used

#define home_switch 9                               // Pin 9 connected to Home Switch (MicroSwitch)

// Stepper Travel Variables

long initial_homing=-1;                                 // Used to Home Stepper at startup

 

void setup()

{

      Serial.begin(9600);                                  // Start the Serial monitor with speed of 9600 Bauds

      pinMode(8,OUTPUT);

      pinMode(home_switch, INPUT_PULLUP);

      delay(5);

      stepperX.setPinsInverted(false,false,true);

      stepperX.setEnablePin(8);             //pin8 -> ENA+

           

//  Set Max Speed and Acceleration of each Steppers at startup for homing

      stepperX.setMaxSpeed(100.0);             // Set Max Speed of Stepper (Slower to get better accuracy)

      stepperX.setAcceleration(100.0);         // Set Acceleration of Stepper

 

// Start Homing procedure of Stepper Motor at startup

   Serial.print("Stepper is Homing . . . . . . . . . . . ");

 

  while (digitalRead(home_switch)) {  // Make the Stepper move CCW until the switch is activated  

    stepperX.moveTo(initial_homing);  // Set the position to move to

    initial_homing--;                 // Decrease by 1 for next move if needed

    stepperX.run();                   // Start moving the stepper

    delay(5);

  }

  stepperX.setCurrentPosition(0);     // Set the current position as zero for now

  stepperX.setMaxSpeed(200.0);        // Set Max Speed of Stepper (Slower to get better accuracy)

  stepperX.setAcceleration(200.0);    // Set Acceleration of Stepper

  initial_homing=1;

 

  while (!digitalRead(home_switch))   { // Make the Stepper move CW until the switch is deactivated

    stepperX.moveTo(initial_homing); 

    initial_homing++;

    stepperX.run();

    delay(100);

  }

}

void loop()

        {

        stepperX.setMaxSpeed(1000.0);            // Set Max Speed of Stepper (Faster for regular movements)

        stepperX.setAcceleration(1000.0);

        stepperX.moveTo(2000);

        }

    stepperX.run();

    stepperX.enableOutputs();

}

Message has been deleted

Geoff Smith

unread,
May 29, 2019, 9:15:24 AM5/29/19
to accelstepper
Andrew,

Try using move() instead of moveTo().

Untested code suggestion...


/*  Motor Homing code using AccelStepper */
#include <AccelStepper.h>
// AccelStepper Setup
// Define a stepper and the pins it will use
 
AccelStepper stepperX(AccelStepper::DRIVER, 6, 7);  // pin 6 -> PUL+, pin 7 ->  DIR+
 
// Define the Pins used
#define home_switch 9                               // Pin 9 connected to Home Switch (MicroSwitch)
// Stepper Travel Variables
long initial_homing=-1;                                 // Used to Home Stepper at startup
 
void setup() 
{
      Serial.begin(9600);                                  // Start the Serial monitor with speed of 9600 Bauds
      pinMode(8,OUTPUT);
      pinMode(home_switch, INPUT_PULLUP);
      delay(5);
      stepperX.setPinsInverted(false,false,true);
      stepperX.setEnablePin(8);             //pin8 -> ENA+
            
//  Set Max Speed and Acceleration of each Steppers at startup for homing
      stepperX.setMaxSpeed(100.0);             // Set Max Speed of Stepper (Slower to get better accuracy)
      stepperX.setAcceleration(100.0);         // Set Acceleration of Stepper
 
// Start Homing procedure of Stepper Motor at startup
   Serial.print("Stepper is Homing . . . . . . . . . . . ");
   
 void homing1()
 {
long i = -1;
while (digitalRead(home_switch))  // Make the Stepper move CCW until the switch is activated  
stepperX.move(i);
stepperX.run();                   // Start moving the stepper
delay(5);
}
  stepperX.setCurrentPosition(0);     // Set the current position as zero for now
  stepperX.setMaxSpeed(200.0);        // Set Max Speed of Stepper (Slower to get better accuracy)
  stepperX.setAcceleration(200.0);    // Set Acceleration of Stepper
 }

 void homing2()
 {
long i = 1;    // Make the Stepper move CW until the switch is deactivated
 
while (!digitalRead(home_switch))
{
stepperX.move(i); 
stepperX.run();
delay(100);
}
 }
 
void loop()
{  
void homing1();
void homing2();
Reply all
Reply to author
Forward
0 new messages