Arduino Mega and TB6560 control Nema 17 with Joystick

I recently purchased an Arduino Mega board with two controllers TB6560 as well as a joystick breakout board. My aim is to use the thumb joystick to control the direction of the stepper motors on the X-axis and Y-axis, left to right, up to down as well as the speed. I've been looking for good tutorials but had no luck so far.

My stepper motor: Nema 17 - 17HS19-1684S-PG51 - Bipolar Stepper

Any help would really be appreciated!

My wiring setup is as follows:

BL- TB6560 V2.0 24v = +12V battery GND = GND battery A+ = Stepper Red wire A - = Stepper Blue wire B+ = Stepper Green wire B- = Stepper Black wire

En+ = nothing En- = nothing Cw+ = arduino pwm pin 8 Cw- = arduino GND Clk+ = arduino pwm pin 9 Clk- =arduino GND

Joystick key - nothing y - analog pin A1 x - analog pin A2 vdc - arduino 3.5V gnd - arduino GND

Have a look at stepper motor basics and use this simple stepper code to ensure you can control the motors.

You may also get something useful from planning and implementing a program.

Write a very short program so you can learn how to get input from the joysticks and display values in the Serial Monitor.

Joysticks usually self centre. What do you want to happen when that happens?

I distrust descriptions of wiring connections. If you need to communicate your wiring arrangements draw a diagram and post a photo of the diagram.

...R

Any reason not to drive enable? Stepper drivers usually have step, direction and enable inputs and you have to drive enable for anything to happen, then provide step pulses...

I managed to run it with code below.

byte directionPin = 9;
byte stepPin = 8;
int numberOfSteps = 10000;
byte ledPin = 13;
int pulseWidthMicros = 50;  // microseconds
int millisbetweenSteps = 1; // milliseconds


void setup() { 

  Serial.begin(9600);
  Serial.println("Starting StepperTest");
  digitalWrite(ledPin, LOW);
  
  delay(2000);

  pinMode(directionPin, OUTPUT);
  pinMode(stepPin, OUTPUT);
  pinMode(ledPin, OUTPUT);
  
 
  digitalWrite(directionPin, HIGH);
  for(int n = 0; n < numberOfSteps; n++) {
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(pulseWidthMicros); // this line is probably unnecessary
    digitalWrite(stepPin, LOW);
    
    delay(millisbetweenSteps);
    
    digitalWrite(ledPin, !digitalRead(ledPin));
  }
  
  delay(3000);
  

  digitalWrite(directionPin, LOW);
  for(int n = 0; n < numberOfSteps; n++) {
    digitalWrite(stepPin, HIGH);
    // delayMicroseconds(pulseWidthMicros); // probably not needed
    digitalWrite(stepPin, LOW);
    
    delay(millisbetweenSteps);
    
    digitalWrite(ledPin, !digitalRead(ledPin));
  }
}

void loop() { 
}

I have an another question for you. Is there a chance that we can get more speed out of it? Now we are running it at about 255 rpm. I want it more if its possible. I have got a Nema 17HS19-1684S

Thank you for further replies.

If you are getting 255 rpm (probably 300 for a 200 step motor?) with a gap between steps of 1 millisecond you could try a shorter gap using delayMicroseconds()

Can you post a link to the datasheet for your motor?

...R

http://www.omc-stepperonline.com/download/pdf/17HS19-1684S-PG51.pdf

I changed all the millis to micros and the code goes like this:

// testing a stepper motor with a Pololu A4988 driver board or equivalent
// on an Uno the onboard led will flash with each step
// this version uses delay() to manage timing

byte directionPin = 9;
byte stepPin = 8;
int numberOfSteps = 10000;
byte ledPin = 13;
int pulseWidthMicros = 10;  // microseconds
int microsbetweenSteps = 1; // milliseconds


void setup() { 

  Serial.begin(9600);
  Serial.println("Starting StepperTest");
  digitalWrite(ledPin, LOW);
  
  delay(2000);

  pinMode(directionPin, OUTPUT);
  pinMode(stepPin, OUTPUT);
  pinMode(ledPin, OUTPUT);
  
 
  digitalWrite(directionPin, HIGH);
  for(int n = 0; n < numberOfSteps; n++) {
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(pulseWidthMicros); // this line is probably unnecessary
    digitalWrite(stepPin, LOW);
    
    delay(microsbetweenSteps);
    
    digitalWrite(ledPin, !digitalRead(ledPin));
  }
  
  delay(3000);
  

  digitalWrite(directionPin, LOW);
  for(int n = 0; n < numberOfSteps; n++) {
    digitalWrite(stepPin, HIGH);
    // delayMicroseconds(pulseWidthMicros); // probably not needed
    digitalWrite(stepPin, LOW);
    
    delay(microsbetweenSteps);
    
    digitalWrite(ledPin, !digitalRead(ledPin));
  }
}

void readPotentiometer() 
{
const byte potPin = A0;
int potValue;

potValue = analogRead(potPin);


  
  
}


void loop() { 
 }

klemode: I changed all the millis to micros and the code goes like this:

Glad to see you have a solution.

...R

Sorry, I made ​​a mistake when writing post.

I changed it , from millis to micros, but the motor speed does not change. Did I do some mistake in code ?

You have changed the wrong thing so you have not changed the timing.

In the original code you have the variable millisBetweenSteps set to 1 and the line delay(millisBetweenSteps); therefore causes a delay of 1 millisecond.

You need to change that line to delayMicroseconds(1000); to get the exact same speed. Then you can reduce the value to get smaller gaps between steps. Try 750 or 500 and see what happens.

If you want to change the variable name it could be unsigned long microsbetweenSteps = 1000;

...R