Stepper motor random direction change

I'm so confused
I'm running stepper motor with stepper driver A4988. I have setup motor direction to HIGH but direction changes randomly. How it is possible?
This is my code"

const int dirPin = 9;
const int stepPin = 8;
const int stepsPerRevolution = 1000;
const int CH_2_PIN = 11;
int ch_8;

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

void loop()

void stepper()
digitalWrite(dirPin, HIGH);

// Spin motor slowly
for(int x = 0; x < stepsPerRevolution; x++)
digitalWrite(stepPin, HIGH);
digitalWrite(stepPin, LOW);


Any help please ...

How to you have it wired up? What are you using to power it?

Logical 5V from arduino to VDD on A4988 and external 12V to VMOT on A4988. Stepper motor (12V 0.4A)

A 12V stepper motor driven by an A4988 that is powered by 12 V is not a good idea.
The A4988 needs a much higher supply voltage than the rated voltage of the motor to operate.
Have you set the current for your stepper motor on the A4988 correctly? And did you place a bulk capacitor at the powerpins of the A4988?
A 12V stepper isn't a good choice for an A4988.

The A4988 stepper drive voltage is specified as 8 - 35V, so 12V is fine - except that this will severely limit the maximum step rate. This is because the inductance of the windings slows the build up of current until it reaches the set limit - it also slows the decay of current when the voltage is reversed.

If the step rate is higher than this maximum and the inertial load is small, the movement of the motor is unpredictable because the coil currents are usually quite different to those you expected from the state of the step and direction inputs.

As the previous poster says, set the current limits to those defined in the stepper specification and use a low ESR high value capacitor across the 12V supply. If the required step rate cannot be achieved when the motor is loaded, then you have two choices - use a motor with a lower voltage rating (3.3V and 5V are common), or increase the supply voltage. Worst case you may have to do both!!.

To achieve maximum step rate under load, controlled acceleration and deceleration is necessary!! This is a whole new ball game and not simple to implement!!

Hi, @jaceksoltys
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".

To add code please click this link;

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom..... :grinning: :+1: :coffee: :australia:

is it being driven too fast -- step rate ~ 1ms/step?

There are Libs to do that, E.g. Accelstepper or MobaTools.

Thanks for the reference to Accelstepper - being new to Arduino I wasn't aware of that.

My only experience of accel/decel is on PIC24F running at max speed - actually used a sqrt calculation to drive the maths. It was a hobby project and this was a learning exercise for me in programming the 24 series PICs.

I have 100uF capacitors and all is connected as on the picture. On A4988 a set the voltage to 0,3V

here is the stepper motor I use

and the driver

I increased voltage from 12V to 16V still the same, randomly changes direction :frowning:

How did you increase the input to 16V when your schematic shows 12V? Please make your words match your pictures.

True, forgot to update the picture. Anyway 12V or 16V doesn't change behavior, still randomly changes direction.

Can you please post a picture(s) of your project so we can see your component layout?

It sounds like you have the motor connections misplaced, have you identified the stepper windings with a DMM?

Thanks.. Tom... :grinning: :+1: :coffee: :australia:

It is a mess of wires on Breadboard. But I conneted it as on the picture

If you use this diagram from Pololu.
And use a DMM to check what wires are the the two coils in your stepper.

Tom... :grinning: :+1: :coffee: :australia:


RED and WHITE is one winding and should go to 1A and 1B.
YELLOW and BLACK is one winding and should go to 2A and 2B.

Check this with your DMM please.

Tom.... :grinning: :+1: :coffee: :australia:

Stupid me :slight_smile: How I could not see that I connected motor wrong way. Adding capacitor and correct motor connection did the trick. Many thanks to you all.
BTW, anything I can do to reduce very high motor tempreture?

That probably means that you do not have the coil current set properly on the A4988 driver.
To set the coil current properly you must know the value of the sense resistors on your modules so that you can plug their values into the equation to compute the proper setting for Vref.
See the Pololu A4988 product page.

That said, steppers can run hot. Hot enough to be uncomfortabe to hold a finger on. Burning the finger could be too hot.

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