2 Stepper Motor Control

Hii everyone, I wrote this code to control 2 stepper motors, only the first driver works. Although voltage goes to the 2nd drive but the motor does not drive. I think there is an error in the code. I'm adding it here too. I will be grateful if you could help me.

// 1.Step

const int dirPin = 2;
const int stepPin = 3;
const int stepsPerRevolution = 200;

const int dirPin2 = 4;
const int stepPin2 = 5;
const int stepsPerRevolution2 = 180;

void setup()
{
 
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
  pinMode(stepPin2, OUTPUT);
  pinMode(dirPin2, OUTPUT);

}
void loop()
{
  digitalWrite(dirPin, HIGH);
  digitalWrite(dirPin2, HIGH);


  for(int x = 0; x < stepsPerRevolution; x++)
  {

    digitalWrite(stepPin, HIGH);
    delayMicroseconds(2000);

    digitalWrite(stepPin2, HIGH);
    delayMicroseconds(2000);
    
    digitalWrite(stepPin, LOW);
    delayMicroseconds(2000);
    

    digitalWrite(stepPin2, LOW);
    delayMicroseconds(2000);
  }
  delay(1000); // Wait a second
  

  
  digitalWrite(dirPin, LOW);
  digitalWrite(dirPin2, LOW);

  
  for(int x = 0; x < stepsPerRevolution; x++)
  {
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(1000);

    digitalWrite(stepPin2, HIGH);
    delayMicroseconds(1000);
    
    digitalWrite(stepPin, LOW);
    delayMicroseconds(1000);

        
    digitalWrite(stepPin2, LOW);
    delayMicroseconds(1000);
  }
  delay(1000); // Wait a second

}

Please use the autoformat function in the IDE, tab tools.
Then use code tags when posting.
Please post schematics.
Al according to this topic: How to get the best out of this forum - Using Arduino / Project Guidance - Arduino Forum

1 Like

Your code is not ramping up step rates - motors cannot accelerate instantly, not if they obey the laws of physics :slight_smile:

Try the AccelStepper library which gives automatic step ramping and runs non-blocking in the background via the run() method, allowing multiple motors to be driven in a nice simple framework.

1 Like

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