Stepper motor rotates incorrectly

We are doing a project at school and I need to move the stepper motors with the joystick module. My motor is nema23, I use TB6600 as motor driver. I'm 99% sure my connections are correct. My code is as follows;

#define joyX A0  // Joystick X ekseni
#define joyY A1  // Joystick Y ekseni

#define dirPin1 9   // 1. Motor Yön pini
#define stepPin1 8  // 1. Motor Adım pini
#define dirPin2 3   // 2. Motor Yön pini
#define stepPin2 2  // 2. Motor Adım pini

#define enaPin1 10   // 1. Motor Enable pini
#define enaPin2 4  // 2. Motor Enable pini

#define stepDelay 50 // Adımlar arası gecikme (mikrosaniye)

void setup() {
    pinMode(dirPin1, OUTPUT);
    pinMode(stepPin1, OUTPUT);
    pinMode(dirPin2, OUTPUT);
    pinMode(stepPin2, OUTPUT);
    
    pinMode(enaPin1, OUTPUT); // Enable pini ayarlama
    pinMode(enaPin2, OUTPUT); // Enable pini ayarlama
    
    pinMode(joyX, INPUT);
    pinMode(joyY, INPUT);
    
    // Motorları aktif hale getirme (ENA+ pinlerini LOW yapma)
    digitalWrite(enaPin1, LOW); 
    digitalWrite(enaPin2, LOW); 
}

void loop() {
    int xValue = analogRead(joyX);
    int yValue = analogRead(joyY);
    
    // X ekseni motor kontrolü
    if (xValue > 600) {
        digitalWrite(dirPin1, HIGH); // İleri
        stepMotor(stepPin1);
    } else if (xValue < 400) {
        digitalWrite(dirPin1, LOW); // Geri
        stepMotor(stepPin1);
    }

    // Y ekseni motor kontrolü
    if (yValue > 600) {
        digitalWrite(dirPin2, HIGH); // İleri
        stepMotor(stepPin2);
    } else if (yValue < 400) {
        digitalWrite(dirPin2, LOW); // Geri
        stepMotor(stepPin2);
    }
}

void stepMotor(int stepPin) {
    digitalWrite(stepPin, LOW);
    delayMicroseconds(stepDelay);
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(stepDelay);
    digitalWrite(stepPin, LOW);
}

I'm also adding a video.

[Motor test1]

[Motor test2]

As you can see in the video, I'm trying to turn it right but sometimes it also turns to left.

I know it's a bit complicated, I can try to make it more precise if needed.

I am also adding the TB6600 motor drivers microstep settings
SW1- ON
SW2- OFF
SW3- OFF
SW4- OFF
SW5- OFF
SW6- OFF

Does it matter? or are these correct?

stepDelay is set to 50 µs, which results in a frequency of 10 kHz. Make sure your stepper motors can operate correctly at this speed.

1 Like

As a start, set delay between step pulses to 10- 50 milliseconds.
Nema 23 tells nothing useful but a link to the datasheet does.

Increase the delay. Then try changing the pulse to HIGH/LOW without the initial LOW.

void stepMotor(int stepPin) {
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(stepDelay);
    digitalWrite(stepPin, LOW);
}

Thank you for all the supports. I was sure about connections %99 but that %1 broke my system. I tried to change step motors cable and it fixed :sweat_smile:.

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