I'm using two tb6600 to control two step motors. It's for letter writing cnc machine. Moving single axis works fine.

I want to push 2 steppers in the same time along a diagonal.

I tried to calculate ratio x:y, so if x is making more steps than y for 1 y do n x steps. This worked, but machine was shaking no mater how i adjusted delays between step high and low. When i use PWM there is no shaking, but i can't figure out how to make it follow diagonal.

Second try was to use PWM with analogWrite.

if(delayX > delayY){
      analogWrite(stepPinX, 128);
      analogWrite(stepPinY, 128);

      analogWrite(stepPinY, 0);
      delay(delayX - delayY);
      analogWrite(stepPinX, 0);
    }else if(delayX <= delayY){
      analogWrite(stepPinX, 128);
      analogWrite(stepPinY, 128);

      analogWrite(stepPinX, 0);
      delay(delayY - delayX);
      analogWrite(stepPinY, 0);

This doesn't works obviouslyit follows blue. It reaches end point, but it doesn't follow diagonal. If i set duty cycle to 2 or 128 it doesn't matter. Isn't duty cycle supposed to change speed of stepper motor?

What am i missing here and how can this be done?

  • No, PWM won't change the speed - you're pulsing the STEP input of your stepper drivers, where each signal edge will create a movement. PWM only changes the duration of the HIGH and LOW sections of that movement, not the frequency (which would change the speed). You'll probably want to use a library designed to handle stepper motors / drivers.
    – towe
    Oct 15, 2020 at 5:41

1 Answer 1


Changing the duty cycle does not change the frequency of the PWM signal.

The tb6000 needs a pulse to trigger a step of the motor. If the frequency does not change the pulses per second do not change too and thus the speed is constant.

If you control just one motor the method of using delays to control the frequency works.

But in the case of two motors with different speed you need counter variables to immetate two frequencies (speeds) you need to control each speed of the motors.

First you caculate the ration of the speeds (you said y=1 and x=n steps. Then, the ratio is n:1, i.e the X Motor is faster then the Y Motor by factor n).

The following is just an incomplete example to explain my idea.

int cntX = 0;
int cntY = 0;

// I don't know you value n
// Let's say n=5
int ratioX = 5;
int ratioY = 1;

// One step per second at max speeed
// choose whatever fits your needs   
int delayForMaxSpeed = 1000;

const int stepPinX = 5; 
const int dirPinX = 2; 
const int stepPinY = 6; 
const int dirPinY = 3; 
const int enPin = 8;

void setup() 
  pinMode( stepPinY, OUTPUT ); 
  pinMode( dirPinY, OUTPUT );

  pinMode( enPin, OUTPUT );
  digitalWrite( enPin, LOW );

void loop() 
  // Enables the motor to move in a particular direction
  digitalWrite( dirPin, HIGH ); 


  if ( countX >= ratioX )
     countX = 0;
     digitalWrite( stepPinX, HIGH ); 

  if ( countY >= ratioY )
    countY = 0;
    digitalWrite( stepPinY, HIGH ); 

  delayMicroseconds( 500 ); 

  digitalWrite( stepPinX, LOW ); 
  digitalWrite( stepPinY, LOW ); 

  delay( delayForMaxSpeed );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.