Run 2 stepper motors

Hi I am struggling with my project and would like some help if possible, I want stepper 1 to move one complete turn then stop then stepper 2 to half turn forward then return and for this sketch to run continuously but I can’t find any YouTube videos on this showing me how to wire it correctly and a Arduino sketch to make this work, I’m using a nema 17 and a tb6600 driver, I have 24v powering tb6600 driver and 12v powering Arduino uno , Any help would be greatly appreciated. I am completely new to this so I have very basic knowledge.
Thank you
David

What is stepper 2 and what are you using to drive it? What are you using for power supply for your motors and for your Arduino?
Guess you will have to begin at the beginning to learn a bit about C and writing a program for you unknown Arduino board.

Hi it’s 2 nema 17 stepper motors stepper 1 I want to turn forward 1 revolution (200 steps) then wait for the stepper motor 2 to move a half turn forward then return and this is to continue until I turn it off. If that make any sense.
Cheers
David

Hi I am using 24v for both tb6600 drivers and 12v for Arduino uno.
Cheers
David

Here is a demo code using the MobaTools stepper library to perform your sequence as I understand it. This code has been tested using an Uno with CNC shield (A4988 drivers) and NEMA 17 steppers. The code should work, without modification, with your TB6600 drivers. There is no provision to "home" the motors. Microstepping is used to mitigate the effects of resonance on these motors at certain step speeds.

// stepper sequence by groundFungus aka c.goulding

#include <MobaTools.h>

const byte xStepPin = 2;
const byte xDirPin = 5;
const byte yStepPin = 3;
const byte yDirPin = 6;


const unsigned int motorStepsPerRev = 200;
const unsigned int microstepMultiplier = 4;
const int STEPS_REVOLUTION = motorStepsPerRev * microstepMultiplier;

MoToStepper xStepper( STEPS_REVOLUTION, STEPDIR );
MoToStepper yStepper( STEPS_REVOLUTION, STEPDIR );

byte steps = 0; // step in sequence

void setup()
{
   Serial.begin(115200);
   Serial.println("stepper sequence");

   xStepper.attach( xStepPin, xDirPin );
   xStepper.setSpeedSteps(10000); // = 1000 steps/sec (steps /10)
   xStepper.setRampLen(10);
   xStepper.setZero();

   yStepper.attach( yStepPin, yDirPin );
   yStepper.setSpeedSteps(10000); // = 1000 steps/sec (steps /10)
   yStepper.setRampLen(10);
   yStepper.setZero();

   delay(2000);
}

void loop()
{
   if (steps == 0)
   {
      xStepper.move(STEPS_REVOLUTION); // roatate 1 rev
      steps = 1;
   }

   if (steps == 1)
   {
      if (xStepper.distanceToGo() == 0)
      {
         Serial.println("step 1 complete");
         steps = 2;
      }
   }

   if (steps == 2)
   {
      yStepper.moveTo(STEPS_REVOLUTION / 2);
      steps = 3;
   }

   if (steps == 3)
   {
      if (yStepper.distanceToGo() == 0)
      {
         Serial.println("step 2 complete");
         steps = 4;
      }
   }

   if (steps == 4)
   {
      yStepper.moveTo(0);
      steps = 5;
   }
   if (steps == 5)
   {
      if (yStepper.distanceToGo() == 0)
      {
         Serial.println("step 4 complete");
         steps = 0;
      }
   }
}


Hi thank you so much. I will try this and hopefully that will work.
Cheers
David

If it does not, please post a schematic or wiring diagram and some photos showing the wiring.

Leave the enable terminals on the driver disconnected to start.

To get accurate rotations, the microstepping must be set to 4 or the code adjusted to reflect the microstep setting.

Hi thank you this is almost there I need to change the 4 to 1 as it rotates 4 times and I only want 1 turn but not sure if it’s that simple and I need a little bit longer delay between the first stepper turning one revolution to the second stepper how do I increase the time between each of them. But it’s working.
Cheers
David

How's it going today? Here's the improved code to help out:

#include <MobaTools.h>

const byte xStepPin = 2;
const byte xDirPin = 5;
const byte yStepPin = 3;
const byte yDirPin = 6;

const unsigned int motorStepsPerRev = 200;
const unsigned int microstepMultiplier = 4;
const int STEPS_REVOLUTION = motorStepsPerRev * microstepMultiplier;

MoToStepper xStepper(STEPS_REVOLUTION, STEPDIR);
MoToStepper yStepper(STEPS_REVOLUTION, STEPDIR);

byte steps = 0; // step in sequence

void setup()
{
   Serial.begin(115200);
   Serial.println("Stepper Sequence");

   xStepper.attach(xStepPin, xDirPin);
   xStepper.setSpeedSteps(10000); // 1000 steps/sec (steps / 10)
   xStepper.setRampLen(10);
   xStepper.setZero();

   yStepper.attach(yStepPin, yDirPin);
   yStepper.setSpeedSteps(10000); // 1000 steps/sec (steps / 10)
   yStepper.setRampLen(10);
   yStepper.setZero();

   delay(2000);
}

void loop()
{
   if (steps == 0)
   {
      xStepper.move(STEPS_REVOLUTION / 4); // Rotate 1/4 rev
      steps = 1;
   }

   if (steps == 1 && xStepper.distanceToGo() == 0)
   {
      Serial.println("Step 1 complete");
      steps = 2;
      delay(1000); // Add a 1-second delay before the next step
   }

   if (steps == 2)
   {
      yStepper.move(STEPS_REVOLUTION / 2); // Rotate 1/2 rev
      steps = 3;
   }

   if (steps == 3 && yStepper.distanceToGo() == 0)
   {
      Serial.println("Step 2 complete");
      steps = 4;
   }

   if (steps == 4)
   {
      yStepper.move(0); // Move back to the original position
      steps = 5;
   }

   if (steps == 5 && yStepper.distanceToGo() == 0)
   {
      Serial.println("Step 4 complete");
      steps = 0;
   }
}

Here are the fixes and optimizations:

  1. Comment Headers: Added comments for better code readability and understanding.

  2. Reduced First Stepper Rotation: To make the first stepper rotate only one-fourth of a revolution, I changed xStepper.move(STEPS_REVOLUTION) to xStepper.move(STEPS_REVOLUTION / 4).

  3. Added Delay: To increase the time delay between the first and second stepper actions, I added a delay(1000) after the first stepper completes its rotation.

  4. Simplified the Code: I simplified the code structure by reducing the number of if conditions and made it more concise while retaining the same functionality.

  5. Added Comments: Added comments to explain the purpose of each section of the code for better understanding.

These changes improve the code's readability and make it more efficient.

Hi thank you I will try this and see if this is better. I also might have to tweak the first stepper to possibly more or less than a full revolution what would be the best way to do that.
Cheers
David

So change microstepMultiplier to 1, but beware of resonance if you do not use any microstepping or other means to mitigate it.

I prefer to write non-blocking code, so no delay(). So if you decide to add a stop button or something else, the code will be responsive. No waiting for the delay() to time out.

#include <MobaTools.h>

const byte xStepPin = 2;
const byte xDirPin = 5;
const byte yStepPin = 3;
const byte yDirPin = 6;


const unsigned int motorStepsPerRev = 200;
const unsigned int microstepMultiplier = 1; // 4;
const int STEPS_REVOLUTION = motorStepsPerRev * microstepMultiplier;

MoToStepper xStepper( STEPS_REVOLUTION, STEPDIR );
MoToStepper yStepper( STEPS_REVOLUTION, STEPDIR );

byte steps = 0; // step in sequence

unsigned long timer = 0;
unsigned long pauseTime = 2000;

void setup()
{
   Serial.begin(115200);
   Serial.println("stepper sequence");

   xStepper.attach( xStepPin, xDirPin );
   xStepper.setSpeedSteps(10000); // = 1000 steps/sec (steps /10)
   xStepper.setRampLen(10);
   xStepper.setZero();

   yStepper.attach( yStepPin, yDirPin );
   yStepper.setSpeedSteps(10000); // = 1000 steps/sec (steps /10)
   yStepper.setRampLen(10);
   yStepper.setZero();   
}

void loop()
{
   if (steps == 0)
   {
      xStepper.move(STEPS_REVOLUTION); // roatate 1 rev
      steps = 1;
   }

   if (steps == 1)
   {
      if (xStepper.distanceToGo() == 0)
      {
         Serial.println("step 1 complete");
         timer = millis();
         steps = 2;
      }
   }

   if (steps == 2)
   {
      if (millis() - timer >= pauseTime)
      {
         steps = 3;
      }
   }

   if (steps == 3)
   {
      yStepper.moveTo(STEPS_REVOLUTION / 2);
      steps = 4;
   }

   if (steps == 4)
   {
      if (yStepper.distanceToGo() == 0)
      {
         Serial.println("step 2 complete");
         timer = millis();
         pauseTime = 1000;
         steps = 5;
      }
   }

   if (steps == 5)
   {
      if (millis() - timer >= pauseTime)
      {
         steps = 6;
      }
   }

   if (steps == 6)
   {
      yStepper.moveTo(0);
      steps = 7;
   }
   if (steps == 7)
   {
      if (yStepper.distanceToGo() == 0)
      {
         Serial.println("step 4 complete");
         steps = 0;
      }
   }
}

Some non-blocking timing tutorials:
Blink without delay().
Blink without delay detailed explanation
Beginner's guide to millis().
Several things at a time.

To adjust the first stepper to rotate more or less than a full revolution, you can modify the value passed to xStepper.move(STEPS_REVOLUTION / 4) in the code. Currently, it's set to rotate one-fourth of a revolution. If you want it to rotate more, increase the denominator; if you want it to rotate less, decrease the denominator.

For example, if you want the first stepper to rotate one-eighth of a revolution, you can change it to:

xStepper.move(STEPS_REVOLUTION / 8); // Rotate 1/8 rev

Or if you want it to rotate three-fourths of a revolution, you can change it to:

xStepper.move(STEPS_REVOLUTION * 3 / 4); // Rotate 3/4 rev

Adjust the fraction as needed to achieve the desired rotation amount for the first stepper.

The best way to do that is to experiment with the code that you have been given.

If you don't learn anything you will depend fully 100% on the grace of the users to modify your code again and again and again and again and .... no more help
And it will take you weeks and months to get the code to be working fully satisfying because you have to

post the question and wait for the answer
post the question and wait for the answer
post the question and wait for the answer
post the question and wait for the answer
....

So to speed up learning start to experiment with the given code and then ask specific questions

best regards Stefan

Hi thank you I understand, I am new to this so I am asking so I can learn how to identify what to change without ruining the sketch. But I agree I will play with the code and see what does what. Thanks for all the help so far I really appreciate it.
Cheers
David

Hi thanks for that. Much appreciated.
Cheers
David

Hi in the sketch it uses pins 2,5,3,6 do you not use the ena+ on the driver to any pins on the Arduino uno board?
Cheers
David

Hi I used the sketch and it seams to work fine but the stepper that goes forward a half turn and then returns seams to be inconsistent if I hold the stepper shaft I stop the shaft but the motor sounds like it’s still working. Any ideas what the problem could be.
Cheers
David

Hi I think the nema 17 has not got enough power I think the issue is torque the nema 17 I have are only 59m,cm.
Cheers
David

DronebotWorkshop has a video (and transcript of the video) for controlling bigger stepper motor with microstepper drivers like the TB6600:
https://dronebotworkshop.com/big-stepper-motors/

Hi thank you for the link. Do you agree with my thoughts. The nema 17 has got 59n.cm of torque which I’m finding it easy to hold the drive shaft and stop it from turning. I need more torque for the project I’m doing so I’m going to order a couple of nema23 unless there is a way of making the nema17 more powerful.
Cheers
David