Arduino and A4988 code at start up

Back in Nov 2014 Robin2 gave a basic Arduino program to run a stepper motor through the A4988 driver, Simple Stepper Program. Program works fine but I have one question. In the void setup() section there is a delay(2000) instruction. During this period on my setup the motor is energised and turning, then at the end of the delay period the body of the code kicks in and motor rotates as programmed.

I have experimented with adding the delay into my working programs and it always has same affect, once power gets to the motor it rotates until Arduino code kicks in and takes control. A consequence of this is that even when the delay is removed there is still a fraction of second delay (in the code I assume) when the motor can move momentarily, enough to a make a small but noticeable start up kick.

Web searches suggest I am alone in seeing this activity. I use a NEMA17, A4988 and Arduino Uno. Has anyone experienced this or able to duplicate it?

Thanks for any help.

Why not post the code here, ready to read?

Apologies thought included hyperlink would be sufficient.

// testing a stepper motor with a Pololu A4988 driver board or equivalent
// on an Uno the onboard led will flash with each step
// this version uses delay() to manage timing

byte directionPin = 9;
byte stepPin = 8;
int numberOfSteps = 100;
byte ledPin = 13;
int pulseWidthMicros = 20;  // microseconds
int millisbetweenSteps = 250; // milliseconds - or try 1000 for slower steps


void setup() { 

  Serial.begin(9600);
  Serial.println("Starting StepperTest");
  digitalWrite(ledPin, LOW);
  
  delay(2000);

  pinMode(directionPin, OUTPUT);
  pinMode(stepPin, OUTPUT);
  pinMode(ledPin, OUTPUT);
  
 
  digitalWrite(directionPin, HIGH);
  for(int n = 0; n < numberOfSteps; n++) {
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(pulseWidthMicros); // this line is probably unnecessary
    digitalWrite(stepPin, LOW);
    
    delay(millisbetweenSteps);
    
    digitalWrite(ledPin, !digitalRead(ledPin));
  }
  
  delay(3000);
  

  digitalWrite(directionPin, LOW);
  for(int n = 0; n < numberOfSteps; n++) {
    digitalWrite(stepPin, HIGH);
    // delayMicroseconds(pulseWidthMicros); // probably not needed
    digitalWrite(stepPin, LOW);
    
    delay(millisbetweenSteps);
    
    digitalWrite(ledPin, !digitalRead(ledPin));
  }
}

void loop() { 
}

Thanks. I
Move the delay(2000); down below the 3 lines defining the outputs.

Thanks but does not answer my query. Can you duplicate the problem of motor moving before code kicks in?

What help would a Yes give You?

It would confirm that activity was not some quirk unique to my setup and it would confirm that Arduino code did not have immediate, total control over A4988 and motor.
It would help me refine my approach to how I thought about programs. If there are things out of my control I might need to make allowances for them.

If You dig deeper in Arduino documentation You can find out what happens to the I/O pins from reset until execution starts. They might be put in tristate, with or without pullup etc. For jerk free starts the peripherals have to be selected/designed accordingly.

const byte directionPin = 9;
const byte stepPin = 8;
const byte numberOfSteps = 100;

uint16_t ValueToPlayWith = 250;               // change here to see affecting

void setup() {
  Serial.begin(9600);
  Serial.println("Starting StepperTest");
  pinMode(directionPin, OUTPUT);
  pinMode(stepPin, OUTPUT);
}

void loop() {
  digitalWrite(stepPin, HIGH);
  delay(3);
  digitalWrite(stepPin, LOW);
  delay(ValueToPlayWith);
}

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