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.
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 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.
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.
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...