Stepper motors vibrates for 1 seconds on start


I want to control a Nema 17 stepper motor using Arduino Mega and a4988 driver.

The problem is when I turn the Arduino on, or when I press reset button in it, or when I try to upload code on it, the stepper motor randomly vibrates and rotates back and forth for 1 second, and after that all things works fine just as programmed. There is no delay between turning the Arduino on and the vibration. I tried to remove unnecessary codes but still stepper motor vibrates on start even with uploading following code to the Arduino:

#define dirPin 2
#define stepPin 3
#define enablePin 4

void setup() {
  pinMode(enablePin, OUTPUT);
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
  digitalWrite(enablePin, HIGH);

void loop() {

Here is my setup from A4988 Stepper Motor Driver with Arduino Tutorial (4 Examples)

Note that I also added Enable pin from driver to pin 4 of Arduino to disable driver on setup to check if vibation goes away (as in the code), but didn't helped.

I use a 320 watts computer power supply to power stepper motors with 12volts/18amps.

I also tried 5 different stepper motors and 5 different a4988 drivers, but same thing was happening all the time.

Any helps would be appreciated.

Please post links to the stepping motor product pages or data sheets. "NEMA 17" tells us nothing useful.

To what value did you set the extremely important current limit on the A4988 driver?

Stepper motors have no label on it. All I know is that the store owner that I bought the Prusa i3 kit from said the motors are made in Taiwan and their torque is That's all I know about it. But I sent a message to him to tell me the exact model and I'll tell you when he replies to my message.

I just checked impedance between ground pin and potentiometer and it says it's 4. All drivers are set on 4 by default (and it was working with that setting on the 3d printer).

You have probably not set the A4988 current limit correctly. Study the A4988 data sheet to learn how, and watch Pololu's video (guaranteed to work properly only for their drivers, though).

You also need to know the motor details, and we can't help until you provide those details.

I tried to set current limit from very low values to higher values but didn't fixed the issue. But I will stop guessing values after the store owner replies to my message (right now, it's not working hours here).

Also I noticed something new, when I remove following two lines from the code above, the motor keeps vibrating and spins randomly for ever.

  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);

Obviously I'm not an expert in electronics, but my skeptical programmer mind keeps saying that there is a problem with step pin. I'm suspecting that Arduino sends signals to the driver as soon as it starts to boot, before running setup function.

I tried to change driver pins from PWM pins 2,3,4 to digital pins 22,23,24 and also disabling driver in first line of setup function (following code), but no success.

void setup() {
digitalWrite(stepPin, LOW);
digitalWrite(enablePin, HIGH);
pinMode(enablePin, OUTPUT);
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);

Sounds like you have floating inputs and/or a grounding problem.

Post a hand drawn diagram showing YOUR wiring, with pins and parts labeled. The Fritzing idiot diagram is nearly useless.

The part in the left is Arduino Mega, I forgot to label it.
Also SLEEP and RESET pins on the driver is shorted.

One other thing that I just noticed is that the weird vibration goes away as soon as Arduino's LED stops blinking on start.

Also when I connect/disconnect 12v power source to the driver (V-Mot) after that first second, there is no vibration on motors and everything works fine.

Disconnect motor power on startup or when uploading new code.

So your'e suggesting to ignore the elephant in the room? :o
That's not a real scientific solution.

The correct solution is to ensure that all inputs have appropriate pullup or pulldown resistors, so they are not floating during program upload, or Arduino starting up.

Another solution is switch to the DRV8825 module which has built-in pulldowns and can handle more current / run cooler.