1

I am new to Arduino. I am trying to run two stepper motors NEMA 17 through respective A4988 driver and controlling via Arduino Uno.

The thought is that first stepper motor will carry out a first horizontal scan and then as it reaches to its initial position, second stepper motor will move a single step in vertical direction. This is a kind of raster scan that I want to perform with both motors.

When I am running the code, only horizontal scan motor is running and vertical scan motor is not moving. I have checked my circuit, motors and drivers and everything is working fine. I think there is some mistake in my code. It is a basic level code and I would really appreciate the help for helping me out.

int x, y; //stepper motor 'x' and stepper motor 'y'
#define BAUD (9600)


void setup() 
{
  Serial.begin(BAUD);
  pinMode(6,OUTPUT); // Enable for x
  pinMode(5,OUTPUT); // Step for x
  pinMode(4,OUTPUT); // Dir for x
  digitalWrite(6,LOW); // Set Enable low for x

  pinMode(9,OUTPUT); // Enable for y
  pinMode(8,OUTPUT); // Step for y
  pinMode(7,OUTPUT); // Dir for y
  digitalWrite(9,LOW); // Set Enable low for y
}

void loop() 
{

  digitalWrite(9,LOW); // Set Enable low for y



  for(y = 0; y < 20; y++)
  {
     digitalWrite(6,LOW); // Set Enable low for x
     digitalWrite(4,HIGH); // Set Dir high for x - Clockwise

     Serial.println("CLOCKWISE HORIZONTAL SCANNING IN PROGRESS");

     for(x = 0; x < 50; x++) // Loop 50 times
     {
      digitalWrite(5,HIGH); // Output high for x
      delay(10); // Wait
      digitalWrite(5,LOW); // Output low for x
      delay(100); // Wait
     }

    digitalWrite(6,LOW); // Set Enable low for x
    digitalWrite(4,LOW); // Set Dir low for x - Anticlockwise

    Serial.println("ANTICLOCKWISE HORIZONTAL SCANNING IN PROGRESS");

    for(x = 0; x < 50; x++) // Loop 50 times
    {
      digitalWrite(5,HIGH); // Output high
      delay(10); // Wait
      digitalWrite(5,LOW); // Output low
      delay(100); // Wait
    }

    Serial.println("ONE STEP CLIMB ON VERTCAL AXIS");

    digitalWrite(7,HIGH); // Set Dir high for y

    digitalWrite(8,HIGH); // Output high for y
    delay(100); // Wait
    digitalWrite(8,LOW); // Output low for y
    delay(100); // Wait  
  }

  digitalWrite(9,LOW); // Set Enable low for y
  digitalWrite(7,LOW); // Set Dir low for y

  Serial.println("RESETTING ON VERTCAL AXIS");

  for(y = 0; y < 20; y++) // Loop 20 times
    {
      digitalWrite(8,HIGH); // Output high for y
      delay(10); // Wait
      digitalWrite(8,LOW); // Output low for y
      delay(100); // Wait
    }

}

Do i need to look after the delay being provided??

4
  • Stupid question, but are you expecting the motors to move at the same time? Commented Aug 1, 2018 at 11:47
  • 1
    First of all I find it strnage that you put the baud rate as a macro and the pins as constants. I strongly suggest to declare the pin names as constants (e.g. const byte X_EN = 6;), so that you can easily redefine them and avoid forgetting one. Then are you sure the second motor is not moving? Maybe the stepper is a 200 step/turn, and in one turn you move 1mm, so the full 20 steps are 0.1mm and you can't see it... Can you try swapping X and Y motors?
    – frarugi87
    Commented Aug 1, 2018 at 11:51
  • @ code gorilla - No both motors will not run at the same time. X motor - 50 steps to right, then 50 steps to left. Then Y motor will movie one step up. Again X motor moves 50 steps to right and then 50 steps to left. Then again Y motor will move one step up. This loop continues for 20 steps of Y motor. At the end of 20th, Y motor moves 20 steps down. the loop continues.
    – anshul_p
    Commented Aug 2, 2018 at 13:06
  • @ frarugi - Yes, second motor is not at all working. I have also tried to swap both motors. But no result. One thing i want to share that i am using A4988 and HR4988 drivers. Is that any problem?? Because when they are used together, A4988 connected motor is not working and HR4988 connected motor is working. When i swapped the drivers, neither of the two motors is woking.
    – anshul_p
    Commented Aug 2, 2018 at 13:09

1 Answer 1

1

A minor issue that probably won't affect your result is that

Serial.println("ONE STEP CLIMB ON VERTCAL AXIS");
--> digitalWrite(9,LOW);  // This line 'needs' adding
digitalWrite(7,HIGH); // Set Dir high for y

I can't see anything in the code that will prevent the motors running, OK it will take ~13 seconds to start moving, but it should move.

So - without any hardware attached to the motor spindles - is it possible to switch the motors over to check for a wiring breakage issue? With one motor working and one not it implies to me that either the motor is broken, the wiring is broken or the driver circuit is broken. If the fault moves then its a motor/wiring fault if it stays the same then its a driver fault.

2
  • it's at the beginning, and it never gets modified...
    – frarugi87
    Commented Aug 1, 2018 at 11:48
  • @frarugi87 - Yes and so is the one for the X axis, but that's also set in the loop. Its just my natural software engineer's distrust of hardware that says it could be that, because you never truly know what the pixies are doing on that blue board.:) Commented Aug 1, 2018 at 11:58

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.