Speeding up my stepper motor

Hi there
I am trying to speed up my stepper motor and get more torque as this is for my Metal lathe lead screw.
TB6600 Stepper Driver
My Code work but I just need to speed up my stepper. no micro stepping.
it got 3 buttons one for anticlockwise one for clockwise and one for stop and also a 10K Potentiometer

Please see code .

#include <LiquidCrystal.h>
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);

#define potentiometer  A0  //10k Variable Resistor
#define bt_F A1 // Clockwise Button
#define bt_S A2 // Stop Button
#define bt_B A3 // Anticlockwise Button

#define dirPin 8
#define stepPin 9
#define dirPin1 9
#define stepPin1 8

int read_ADC;
int Speed_LCD;
int Speed = 200;
int Step;
int Mode=0;
int spd = A0;


void setup() { // put your setup code here, to run once
  
pinMode(potentiometer, INPUT); // declare potentiometer as input 

pinMode(bt_F, INPUT_PULLUP); // declare bt_F as input
pinMode(bt_S, INPUT_PULLUP); // declare bt_S as input
pinMode(bt_B, INPUT_PULLUP); // declare bt_B as input

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

  // Set the spinning direction CW/CCW:
digitalWrite(dirPin, HIGH);
  
lcd.begin(16,2);  
lcd.setCursor(0,0);
lcd.print(" WELCOME Lappies");
lcd.setCursor(0,1);
lcd.print(" Project");
delay(2000); // Waiting for a while
lcd.clear();
}

void loop() { 

read_ADC = analogRead(potentiometer); // read analogue to digital value 0 to 1023 
Speed = map(read_ADC, 0, 1023, 100, 0);
Speed_LCD = map(read_ADC, 0, 1023, 0, 100); 


lcd.setCursor(0,0);
lcd.print("   Speed: ");
lcd.print(Speed_LCD); 
lcd.print("%  ");

if(digitalRead (bt_F) == 0){Mode = 0;} //For Clockwise
if(digitalRead (bt_S) == 0){Mode = 1;} //For Stop
if(digitalRead (bt_B) == 0){Mode = 2;} //For Anticlockwise

lcd.setCursor(0,1);

if(Mode==1){ lcd.print("      Stop      ");}
if(Mode==2){ lcd.print("    Clockwise   ");}
if(Mode==0){ lcd.print("  Anticlockwise ");}

if(Speed_LCD>0){
if(Mode==0){  
Step = Step+1;
if(Step>3){Step=0;}    
call_Step(Step);// Stepper motor rotates CW (Clockwise)
}
if(Mode==2){
Step = Step-1;
if(Step<0){Step=3;}  
call_Step(Step);// Stepper motor rotates CCW (Anticlockwise)
}

delay(Speed);
}  

}

void call_Step(int i){
    switch (i) {
      case 0:  // 1010
        digitalWrite(dirPin, HIGH);
        digitalWrite(stepPin, LOW);
                      break;
      case 1:  // 0110
               digitalWrite(dirPin1, HIGH);
        digitalWrite(stepPin1, LOW);
              break;
      case 2:  //0101
        digitalWrite(dirPin, LOW);
        digitalWrite(stepPin, HIGH);
                     break;
      case 3:  //1001
               digitalWrite(dirPin1, LOW);
        digitalWrite(stepPin1, HIGH);
       
      break;
    }
  }type or paste code here
1 Like

Hi, welcome to the forum.

Could you please post your code in code tags. You can edit your existing post.

Can you please tell us what motor you are using at the moment? A link to the data sheet.
What rpm are you getting now?
What is your desired rpm?
What is your desired torque?
Make, model, size of lathe?

Hi There
Data sheet
https://roboeq.ir/files/id/2664/name/57BYGH56-401A.pdf/
about 20 rpm
would like about 500 rpm
The most torque I can get out of that motor
It is a DIY lathe I want to use the motor for the leadscrew.

Sorry I do not know what code tags is
not sure how to use code tags

Thanks for posting the data sheet. It appears to be a 1.2 Nm motor. Not sure if you will have it, but what you will find very helpful is to look for the torque vs rpm graph for the motor. It will tell you what the usable range is for your stepper. You may find that it drops off pretty quickly.

In answer to your question, the most torque you will get is 1.2 Nm at 0 rpm (holding torque).

What stepper driver are you using?

Code tags are the </> at the top of the posting screen.
If you click on that button, a message appears that says post your code here. Simply cut and paste your code in between the ''' lines.

Hi There
I am using a TB6600 Stepper driver

Your code does print to the lcd with each iteration of function loop
and that is what slows your stepper-motor down.

install the MobaTools library with the library-manager and use the step/dir-interface
Your code uses a switch-case-statement that was originally coded for a 4-pin interface.
You modified to work with a two pin Step/Dir -interface.
The great advantage of the MobaTools-library is that the step-pulses are created in the background independant from executing other code

best regards Stefan

Hi Stefan
I added the MobaTool.zip to my Library
But I am not sure how to do the other bits that you suggested as I am quite new with this.
Regards
Johannes

the library has a subfolder with examples

there is a example stepper mimimum which uses the function rotate which makes the stepper-motor rotate (almost) infinetely

/* ====== minimumStepper =======================================
 *  Bare minimum to get a stepper with step/dir driver turning
 */
#include <MobaTools.h>
// Stepper connections - Please adapt to your own needs.
const byte stepPin = D6;
const byte dirPin = D5;

const int stepsPerRev = 200;    // Steps per revolution - may need to be adjusted

MoToStepper stepper1( stepsPerRev, STEPDIR );  // create a stepper instance

void setup() {
  stepper1.attach( stepPin, dirPin );
  stepper1.setSpeed( 300 );              // 30 rev/min (if stepsPerRev is set correctly)
  stepper1.setRampLen( stepsPerRev / 2); // Ramp length is 1/2 revolution
  stepper1.rotate(1);                    // start turning, 1=vorward, -1=backwards                    
}

void loop() {
}

This code shows the basic principle

This is an excerpt from the documentation of the library

void myStepper.rotate( int direction );

The motor rotates up to the next stop command. (1= forward, -1 = backwards )
Rotate (0) stops the motor (with ramp). The motor stops at the end of the braking ramp.

void myStepper.stop( );
Stops the motor immediately ( emergency stop ).

So at that places where you set your mode in yur existing code you code

rotate(1)
rotate(-1)
rotates(0)

best regards Stefan

So the movement of your lead screw in discrete steps instead of a steady movement linked to the spindle is ok for your use?

Hi Stefan
I will have a look.
you mentioned that my code was for a switch-case-statement that was originally coded for a 4-pin interface and I modified to work with a two pin Step/Dir -interface
Can you please tell me how I can change it to a 2 pin instead of a 4 pin?
Regartds
Johannes

Hi There
Will you be able to help with this issue as I am new and not so great with the programming yet?
Regards
Johannes

Hi Stefan
I am obviously not experienced enough to understand everything you telling me to do.
Can I pay you to help me sort out my code?

Hi Johannes, I would be happy to help.

@StefanL38 has posted a very detailed walk through.
I just wanted to have you clarify something: in the original code, you have:

Are you using two stepper motors?
It appears that you are declaring the same pins twice!

Even if you want to use 2 steppers, please remove one of the set of lines. Please try the code in post#10. It will show you how to control one stepper.
Once you have that running, you can start to add other lines.
Lets assume that you have connected the driver so that dirPin is 8 an stepPIn is 9. You would then modify the pin declaration lines in the code from the library example as follows:

const byte dirPin = 8;
const byte stepPin = 9;

Note that you don not need a semicolon after #define statements, but you do in most every other case. You could keep the pin declarations:
#define dirPin 8
#define stepPin 9
if you wanted to.

Please look at all the other variables that need to be declared in the example code (not many). They are pretty much self explanatory. If you don't see the answer clearly, do a Google search for moba tools github. There will probably be a ReadMe page.

After you have got the simple motor example running, report back to us.

If it still doesn't make sense, we are still here :slightly_smiling_face:.

Hi There
Thank you for helping me.
I only got 1 stepper motor.
The only reason that I got the pins declared twice it was the only way that I got it working.
If I remove the #define dirPin1 9 #define stepPin1 8 it does not work for me.
Hi the code in post 10, I am just not sure where to put the code in and what to remove>
Regards
Johannes

To start with, please put only the code in post 10 into your Arduino and run it.
I presume you have the pins connected already, since your motor was running.

Which Arduino are you using?
Can you post a diagram of how you have everything connected? A hand drawn diagram is fine. Do not use Fritzing (can be too fuzzy) to draw the diagram.

Hi
Thank you very much for your help.
I only got this picture I hope this will be ok?
10k Potentiometer, and a 10k resistor with a TB6600 stepper driver.
Regards
Johannes

Does your stepper work without the ENAble pin connected?

Once again, please remove all inputs (pot, button, resistor) from the Arduino, try the stepper program and see what results you get.

Hi it does yes
I got new code that work well it is just the motor does not run smooth when I turn the potentiometer to slow it down.
Can you please have a look why?
This code works better that the other code.
Stepper speed control with 3 buttons Diagram and Code.pdf (655.4 KB)

#include <AccelStepper.h>
// Define the stepper and the pins it will use
AccelStepper stepper1(AccelStepper::DRIVER, 9, 8);
// Define our three input button pins
#define  LEFT_PIN  4
#define  STOP_PIN  3
#define  RIGHT_PIN 2
// Define our analog pot input pin
#define  SPEED_PIN 0
// Define our maximum and minimum speed in steps per second (scale pot to these)
#define  MAX_SPEED 1000
#define  MIN_SPEED 0.1
void setup() {
  // The only AccelStepper value we have to set here is the max speeed, which is higher than we'll ever go
  stepper1.setMaxSpeed(10000.0);
   // Set up the three button inputs, with pullups
  pinMode(LEFT_PIN, INPUT_PULLUP);
  pinMode(STOP_PIN, INPUT_PULLUP);
  pinMode(RIGHT_PIN, INPUT_PULLUP);
}
void loop() {
  static float current_speed = 0.0;         // Holds current motor speed in steps/second
  static int analog_read_counter = 1000;    // Counts down to 0 to fire analog read
  static char sign = 0;                     // Holds -1, 1 or 0 to turn the motor on/off and control direction
  static int analog_value = 0;              // Holds raw analog value.
   // If a switch is pushed down (low), set the sign value appropriately
  if (digitalRead(LEFT_PIN) == 0) {
    sign = 1;
  }
  else if (digitalRead(RIGHT_PIN) == 0) {    
    sign = -1;
  }
  else if (digitalRead(STOP_PIN) == 0) {
    sign = 0;
  }
  // We only want to read the pot every so often (because it takes a long time we don't
  // want to do it every time through the main loop).  
  if (analog_read_counter > 0) {
    analog_read_counter--;
  }
  else {
    analog_read_counter = 1023;
    // Now read the pot (from 0 to 1023)
    analog_value = analogRead(SPEED_PIN);
    // Give the stepper a chance to step if it needs to
    stepper1.runSpeed();
    //  And scale the pot's value from min to max speeds
    current_speed = sign * (((analog_value/1023.0) * (MAX_SPEED - MIN_SPEED)) + MIN_SPEED);
    // Update the stepper to run at this new speed
    stepper1.setSpeed(current_speed);
  }

  // This will run the stepper at a constant speed
  stepper1.runSpeed();
}