Stepper Motor Step Accuracy Issues

Please post your images here. You clearly know how.

Forum was not displaying the images inline from imgur.com, hence I posted the links.
Fixed it now. I needed to link to the image location with the extensions.

Code is as follows

// Arduino Digital Pins and Motor Connections
int dPin = 6; // Direction motor || HIGH - Towards the Motor || LOW - Away from the Motor
int sPin = 7; // Step Pin for providing square pulses
int wake = 9; // Sleep Pin || HIGH - Driver Enabled || LOW - Enter Sleep Mode
int M1 = 4; // Mode Pins
int M0 = 5; // Mode Pins
int enb = 8; // Enable Pin for driver || HIGH - Driver Disabled || LOW - Driver Enabled
int rst = 10; // Reset Pin for driver || HIGH - Driver Enabled || LOW - Driver Disabled
int pulsePeriod = 800; // Pulse Period for the Step Waveform
char input;

// Following parameters can be changed in the tests
int delayTime = 0; // Delay between Each Step in milliseconds
int nMoveSteps = 200; // Testing with values 100, 200, 400, 800, 1000

void stepMotor(boolean dir, int nSteps, int dirPin, int stepPin)  // Input - Value for Direction Pin, Number of Steps to be moved pins, uC Pins for direction and step
{
  digitalWrite(dirPin, dir);    // HIGH - Towards the Motor || LOW - Away from the Motor
  delayMicroseconds(2);
  for (int i = 0; i < nSteps; i++)
  {
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(pulsePeriod);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(pulsePeriod);
  }
}

void setup()
{
  Serial.begin(9600);
  pinMode(dPin, OUTPUT);
  pinMode(sPin, OUTPUT);
  pinMode(wake, OUTPUT);
  pinMode(enb, OUTPUT);
  pinMode(M0, OUTPUT);
  pinMode(M1, OUTPUT);

  // Full Step Mode
  digitalWrite(M0, LOW);
  digitalWrite(M1, LOW);

  // Half Step Mode
  //  digitalWrite(M0, HIGH);
  //  digitalWrite(M1, LOW);

  // One Fourth Step Mode
  //    digitalWrite(M0, LOW);
  //    digitalWrite(M1, HIGH);

  // One Eight Mode
  // digitalWrite(M0, HIGH);
  // digitalWrite(M1, HIGH);

  digitalWrite(enb, HIGH);  // Logic HIGH - Driver Disabled
  digitalWrite(wake, HIGH); // Logic HIGH - Driver Enabled
  digitalWrite(rst, HIGH);  // Logic HIGH - Driver Enabled

  digitalWrite(enb, LOW); // Logic LOW - Driver Enabled
  delay(5000);
}

void loop()
{
  for (int i = 0; i < nMoveSteps; i++)
  {
    stepMotor(HIGH, 1, dPin, sPin);
    delay(delayTime); // For Delay between each step
  }
  delay(5000);
  for (int i = 0; i < nMoveSteps; i++)
  {
    stepMotor(LOW, 1, dPin, sPin);
    delay(delayTime); // For Delay between each step
  }
  while(1)
  {
  }
}

Your code seems to be very confused.

In this function

void stepMotor(boolean dir, int nSteps, int dirPin, int stepPin)  // Input - Value for Direction Pin, Number of 
Steps to be moved pins, uC Pins for direction and step
{
  digitalWrite(dirPin, dir);    // HIGH - Towards the Motor || LOW - Away from the Motor
  delayMicroseconds(2);
  for (int i = 0; i < nSteps; i++)
  {
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(pulsePeriod);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(pulsePeriod);
  }
}

you have a pulse width of 800 microsecs (when 10 is probably sufficient) AND you have also an interval after the pulse of 800 µsecs.

Then in this code

for (int i = 0; i < nMoveSteps; i++)
  {
    stepMotor(HIGH, 1, dPin, sPin);
    delay(delayTime); // For Delay between each step
  }

you add an extra interval of 2 millisecs.

I suggest you change your function to

void stepMotor(boolean dir, int nSteps, int dirPin, int stepPin)  
{
  digitalWrite(dirPin, dir);    // HIGH - Towards the Motor || LOW - Away from the Motor
  delayMicroseconds(2);
  for (int i = 0; i < nSteps; i++)
  {
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(stepPin, LOW);
    delay(intervalBetweenSteps);
  }
}

and then remove the speed control from loop().

And start with a value of 20 millisecs for the intervalBetweenSteps as that will give a modest 50 steps per second - or even try a longer interval.

...R

And I am still not clear how to interpret your tables.

Are you saying that the vertical action moves forward 5mm or 10mm but does not move back at all?

If so, that suggests to me a mechanical problem - something slipping in one direction and not in the other.

...R

you have a pulse width of 800 microsecs (when 10 is probably sufficient) AND you have also an interval after the pulse of 800 µsecs.

I put it as 800microsecs as motor wont move in loaded condition. Torque provided vs speed of steps inverse relation.
10usecs wont even turn my motor.

The code you suggested does is almost similar to what I had posted for delayTime = 20ms.

And I am still not clear how to interpret your tables.

Linear encoder starts at position labelled B(which is Zero) then moves to position A(which should actually be 5mm or 10mm depending on number of steps)
So lets Sl No. 3 in the vertical motor picture, it moves to 4.990mm when it should have moved 5.000mm, then when it goes back same number of steps it reaches 0.002mm.

If its 2000 steps this error is large. It should be 10.000mm but I am getting in the range 9.907-9.915mm. Error of around 0.085mm.

OK. I was interpreting the data incorrectly.

It seems that the motor is not going out far enough but it is coming back as much as it goes out. That suggests that your steps/millimetre value is not quite right.

...R

dev_000:
Any idea on why its working only in the global enable mode and not in other ones? I cant use the global enable in the final design because of constant power draw constraints, hum of the motor and switching noise its inducing. So I have to get it working in one of the other enable modes and also achieve good accuracy. I cant seem to think of a reasonable explanation as to why it is stepping only in Global enable mode.

Because that is how steppers must be driven. If you need it to work some other way then you need some other kind of motor.

A few days back, you asked how 3D printers achieve this accuracy. They do not turn the motors off while printing. If that did happen then the entire print is ruined.