Control 2 Nema 17 stepper motors

// Define pin connections & motor's steps per revolution
const int dirPin = 2;            const int dirPin_alt = 8;      
const int stepPin = 3;             const int stepPin_alt = 9;
int stepDelay=1000;    int stepDelay_alt=10000;
void setup()
{
  // Declare pins as Outputs
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
    pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
}
void loop(){
 haha();
 haha_2();
}

int haha(){
  digitalWrite(dirPin, HIGH);

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

int haha_2(){
  digitalWrite(dirPin_alt, HIGH);
    digitalWrite(stepPin_alt, HIGH);           //stepper motor rotate 5 times slower than haha() due to increased delay 5 times.
    delayMicroseconds(5000);
    digitalWrite(stepPin_alt, LOW);
    delayMicroseconds(5000);
}

web hosting in Sri lanka
Hi all, I am trying to control 2 stepper motor, one with a speed of 5 times of the another one. I have written the code below, however it does not work for 2 stepper motor. I have make sure the power supply and current is enough to drive the small Nema 17 stepper motor:). I believe the problem is with the code. Is there a mistake somewhere in the code :)?

Thank you for reading and have a nice day:)

Do both steppers move?

The problem is the delay() in your code. This doesn't allow the steppers to move independently. If you want to do it by yourself, you must use the millis() function, similar to the IDE example 'BlinkWithoutDelay'. Or you use a library to control the steppers like my MobaTools lib.

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