Question on test code for stepper

I’ve used the test code below:

#define stepPin 2 #define dirPin 3 #define enablePin 4 void setup() { // We set the enable pin to be an output pinMode(enablePin, OUTPUT); // then we set it HIGH so that the board is disabled until we // get into a known state. digitalWrite(enablePin, HIGH); Serial.begin(9600); Serial.println("Starting stepper exerciser."); pinMode(stepPin, OUTPUT); pinMode(dirPin, OUTPUT); } void loop() { int j; // set the enablePin low so that we can now use our stepper driver. digitalWrite(enablePin, LOW); // wait a few microseconds for the enable to take effect // (That isn't in the spec sheet I just added it for sanity.) delayMicroseconds(2); // we set the direction pin in an arbitrary direction. digitalWrite(dirPin, HIGH); for(j=0; j<=10000; j++) { digitalWrite(stepPin, LOW); delayMicroseconds(2); digitalWrite(stepPin, HIGH); delayMicroseconds(1000); }}
To check out my Arduino, driver and stepper. All worked very well. The thought crossed my mind that i can use this code almost as is. I would just like to change the number of steps per second.

So my question is this: what line / value is the step rate?
I thought maybe this: Serial.begin(9600); But 9600 seemed high for the slow speed the motor turned at.

Hello.

Serial.begin() sets the baud rate of the serial communication for your Arduino. It does not set the step rate of your stepper motor. It might be helpful for you to take look at the documentation on Serial.begin(), so you can get a better understanding of what the function does.

To vary the step speed of your stepper motor, you would need to control the delays used in your FOR loop that switches the STEP input pin between logic low and high (which I have copied below). You would need to change either one or both values sent to delayMicroseconds in that FOR loop to control the step rate.

for(j=0; j<=10000; j++) {
   digitalWrite(stepPin, LOW);
   delayMicroseconds(2);	// delay transition to high for 2 us
   digitalWrite(stepPin, HIGH);
   delayMicroseconds(1000);	// delay transition to low for 1000 us
}

- Amanda

OK… now it makes sense. I was looking at other code examples and they would have a code line that would say “step_rate” or “set_speed” but they worked off of a library. This one you literally define the time for each high and low signal.

I’ve experimented with changing the speed a stepper will run at using the stepper test code.

From this small section of the code:

for(j=0; j<=10000; j++) { digitalWrite(stepPin, LOW); delayMicroseconds(2); digitalWrite(stepPin, HIGH); delayMicroseconds(1000);

I find that changing this:

digitalWrite(stepPin, LOW); delayMicroseconds(2);
To this:

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

Will slow the motor step rate down.

Changing this line ( From 2000 to 20 ) seems to have little effect, other than if you set it to a low value ( under 500 ) the stepper just hums and does not move:

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

So I have two questions, first with this line set to:

digitalWrite(stepPin, LOW); delayMicroseconds(2);
The motor steps at about 120 RPM… There is little room to change the value any more, how would one increase the step rate so the motor ran about 50% faster? (180RPM )

Last question, what aspect is controlled by this line of code:

Thanks for the input.

When you change either of the delay values in the FOR loop, you are essentially changing the wait time between each step pulse, which is the reason why your motor’s step rate decreases when you increase those values. It sounds like your stepper motor stalled when you changed the second delay to be less than 500us, so I suspect 500us between each step is close to the top speed for you stepper motor. You could try looking at this forum post for adjustments you could make to your system to increase the maximum step speed.

As for you last question, the FOR loop in your code will step the stepper motor 10,001 steps. However, since that FOR loop is nested inside the void loop() function in your code, it will just keep repeating, so the motor will never stop stepping.

  • Amanda