NEMA 17 behaving randomly with A4988 driver

So as the title suggests, I am using a NEMA 17 motor with A4988 driver. I am using the following simple code as a test to rotate it clockwise, then counterclockwise, single rotations.

The motor, when running, rotates in random directions. It should start clockwise and then alternate, but it starts randomly on its own and doesnt change directions. It randomly move like 3 turns clockwise then 4 counterclockwise etc. I cant seem to understand how to resolve this, I have tried a lot of different libraries as well including accelstepper and the motor just doesn't run in those libraries, it vibrates randomly.

Any help will be greatly appreciated. TIA

`// Define pin connections & motor's steps per revolution`

`const int dirPin = 2;`

`const int stepPin = 3;`

`const int stepsPerRevolution = 200;`

`void setup()`

`{`

`// Declare pins as Outputs`

`pinMode(stepPin, OUTPUT);`

`pinMode(dirPin, OUTPUT);`

`}`

`void loop()`

`{`

`// Set motor direction clockwise`

`digitalWrite(dirPin, HIGH);`

`// Spin motor slowly`

`for(int x = 0; x < stepsPerRevolution; x++)`

`{`

`digitalWrite(stepPin, HIGH);`

`delayMicroseconds(2000);`

`digitalWrite(stepPin, LOW);`

`delayMicroseconds(2000);`

`}`

`delay(1000); // Wait a second`

`// Set motor direction counterclockwise`

`digitalWrite(dirPin, LOW);`

`// Spin motor quickly`

`for(int x = 0; x < stepsPerRevolution; x++)`

`{`

`digitalWrite(stepPin, HIGH);`

`delayMicroseconds(1000);`

`digitalWrite(stepPin, LOW);`

`delayMicroseconds(1000);`

`}`

`delay(1000); // Wait a second`

`}`

The step pulse usually needs to be greater then 10 microseconds, and it is.
The second delay might be too short. Try 20000 microseconds.
If that doesn't help please post schematics, not Fritzing pictures.

Thanks for the reply. It still doesnt work after increasing the delay.
Here's the simple schematic, ignore the difference in arduino pins 1,2 and 2,3

That's un unwanted Fritzing and it does not provide all information.

Unfortunately this is the only schematic I have at the moment but it does provide all the information. There is nothing else to the circuit. I'm using 3x 3.4V cells to give the power to the motors and the arduino is connected to my laptop.

Pen and paper works fine.
The logic names on the driver are not readable. It only shows connection points at the board but that's not useful information.
No powering of the controller.

Hi, @sumranms

This means very little, only the physical size.
Can you post a link to part number/spec/data so we can check that it will work with the driver?

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

The arduino is powered connected to my laptop. I'm attaching a photo below of the schematic on paper

Hi Tom, thanks for the reply.
I got the motor from a local electronics store, here is a link to their page. Nema17 12-24V Stepper motor 1.8/step 6 Wire in Pakistan
I have in fact bought 2 motors from them and the two of them are different in size because one was a used motor. Both of them are having the exact same issue here.
Thanks again for taking time to help out!

Fine.
Leaving pins not connected, like EN, MS1, MS2 and MS3 sometimes bring trouble. Connecting them to Vcc or GND according to what's needed makes sure the electrical noise does not affect those pins.
Sometimes pins like that have pullups, or pulldowns and is's safe not connecting anything to them.
Sometimes pins like that work as antennas picking up whatever is in the air.
I've only used thar driver mounted in CNC shields, never alone like this.

Okay so do you know which ones should go to vcc and gnd respectively? Thanks!

Hi, @sumranms
Thanks for the info.

Have you used a DMM to identify the two windings of the stepper?
image

Check that you have got the windings connected correctly.

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

What difference? It should be connected to pins 2 and 3 (direction and step pin) as in code you posted from this tutorial :https://lastminuteengineers.com/a4988-stepper-motor-driver-arduino-tutorial/
If all is wired as on the picture it should work withot a problem.

Yes I have checked the windings and they are connected properly

No that's a confusion in my part, I had my actual code and connections with pins 1,2 which I thought I had pasted here exactly but it's not the case.... in other words it's not an issue and please ignore it; the connections are done appropriately according to the code.

Hi,

What is your 12V supply?
What current is it rated at?

Have you monitored the 12V supply while trying to operate the motors?

What have you set the current limit on the 4988 to?

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

This should also be fine, when I was making robot with two Nema 17 and A4988 drivers, it worked without a problem with 2 x 18650 cells in series, maybe trimmers for current limit are at minimum?

I did have to adjust the resistance on the driver to at least make the motor rotate, at the default position that the driver came in, the motor was just erratically vibrating.

Have you read all above? Checked motor coils with DMM? Do you have a voltage drop on your battery power supply, when motors are running and changing directions?

Thanks again for replying. Yes I have tried all the above. I have also used a constant 12V power supply instead of using 3.4V cells but experience the same issue...