NEMA 17 and A4988 driver - motor steps back for no reason

Hello,

I am using a NEMA 17 stepper motor and A4988 driver according to the following tutorial:

Everything seems to work relatively well. For example, I try to use the potentiometer code example in the link, and when the PM is around halfway power the motor spins smoothly in one direction.

Nevertheless, if the PM is too low or too high, the motor jerks around kind of like it can’t keep up with itself.

I tried to remove the PM part altogether and only use static code to make the motor rotate continuously in one direction at a fixed speed.

So right now I am trying to get the NEMA 17 to rotate with constant speed in only 1 direction using this code:

#include <Stepper.h>

    // Defines pins numbers
    const int stepPin = 3;
    const int dirPin = 4; 
    
    const int stepsPerRevolution = 200;  // change this to fit the number of steps per revolution
    // for your motor
    Stepper myStepper(stepsPerRevolution, 2,3);
    int stepCount = 0;  // number of steps the motor has taken
    
    void setup() {
      // Sets the two pins as Outputs
      pinMode(stepPin,OUTPUT);
      pinMode(dirPin,OUTPUT);
      digitalWrite(dirPin,HIGH); //Enables the motor to move in a particular direction
    }
    void loop() {
          myStepper.setSpeed(5); //this is the RMP, meaning 1000 steps per minute I believe...
          myStepper.step( (5*200)/360 ); //this should move it around 2.7 steps at a time

    }

What actually results is that it moves a step to the left, then a step to the right, and repeats this.

  1. This is weird, why would it move back if I have no such code?
  2. My wiring is exactly as the tutorial and the potentiometer code DOES make it run in one direction in a certain speed so it’s probably not the motor coils or anything like that…

Any help would be appreciated.

The Stepper library is not really suitable for use with a driver that takes step and direction signals such as the A4988. I suggest you use the AccelStepper library. Its DRIVER option is intended for that sort of stepper driver.

You may find these links useful Stepper Motor Basics Simple Stepper Code

...R

@Robin2

Hi, Yes I actually did also go over this page:

https://scientistnobee.wordpress.com/tag/a4988/

and downloaded and used the AccelStepper library, but it did not work properly.

I will try it again and see if I made a mistake and get back to you in any case with the result.

Thanks.

Start with the Accelstepper examples.

...R

try reversing the wires of one phase if using a sequence of black green blue red use black green red blue or the motor will appear turning forward then back . I had to do this and the motor will run smoother make sure pot is adjusted right

And please don't make the mistake of changing motor connections with the driver powered up - that can easily destroy a driver chip from inductive arcing. Power down, change winding connections, repower.

Thanks for the replies.

I’m not sure if the problem is code or wiring. I doubt the Accelstepper is the right library to use since it is meant mostly for more sophisticated movements… Stepper.h should be enough for continuous function. Nevertheless, I could not find good explanations on the functions of the accelstepper library in order to compose a basic code myself… (I have 2 wires with a motor driver, not 4 wires…)

In any case, my wiring setup was this:
1A - blue
1B - black
2A - green
2B - red

And the code was:

// defines pins numbers
const int stepPin = 3; 
const int dirPin = 4; 
 
void setup() {
  // Sets the two pins as Outputs
  pinMode(stepPin,OUTPUT); 
  pinMode(dirPin,OUTPUT);
}
void loop() {
  digitalWrite(dirPin,HIGH); // Enables the motor to move in a particular direction
  // Makes 200 pulses for making one full cycle rotation
  for(int x = 0; x < 200; x++) {
    digitalWrite(stepPin,HIGH); 
    delayMicroseconds(500); 
    digitalWrite(stepPin,LOW); 
    delayMicroseconds(500); 
  }

}

This made the motor spin just like I wanted, in one direction, continuous.
BUT
When I changed delayMicroseconds(500); to delayMicroseconds(1000); the motor gets ‘stuck’ and basically just doesn’t move but vibrates in place back and forth.
This is quite strange…

I tried the same code with wiring where I switched one of the pairs:

1A - black (was blue)
1B - blue (was black)
2A - green
2B - red

I disconnected all power before switching the wires.

Thanks.

yafimski: I doubt the Accelstepper is the right library to use since it is meant mostly for more sophisticated movements... Stepper.h should be enough for continuous function.

That is not the correct assessment.

You can do simple or complex stuff with AccelStepper. But you cannot properly control a specialized stepper driver such as the A4988 with the simple Stepper library.

Of course, with an A4988 it is not at all difficult to control the motor without using any library. See the examples I linked to in Reply #1

...R

@Robin2

Thank you.

You can do simple or complex stuff with AccelStepper. But you cannot properly control a specialized stepper driver such as the A4988 with the simple Stepper library.

I wasn’t aware of this :slight_smile:

So I now went to the Simple Stepper Program you published (after reading again the Stepper Motor Basics post), and I tried (again) the first code example. I ran the code as is, but it has the same effect that I will describe below.
I modified the code a bit to get rid of the LED flashing and I moved the ForLoop to the void loop in order to keep it spinning in one direction continuously.

byte directionPin = 4;
byte stepPin = 3;
int numberOfSteps = 200;
int pulseWidthMicros = 20;  // microseconds
int millisbetweenSteps = 250; // milliseconds - or try 1000 for slower steps

void setup() {

  pinMode(directionPin, OUTPUT);
  pinMode(stepPin, OUTPUT);  
  digitalWrite(directionPin, HIGH);
}

void loop() {
  for(int n = 0; n < numberOfSteps; n++) {
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(pulseWidthMicros); // this line is probably unnecessary
    digitalWrite(stepPin, LOW);
    
    delay(millisbetweenSteps);
  }
  
  delay(2000);
}

Although it should work, it just goes 1 step forward and then back, ticking like a clock…
I think that it does register the “whole 200 step cycle” and then waits 2 seconds like in the code because if left to tick for a while it suddenly will go back and forth really fast for 2 seconds and then start ticking again.

I tried to disconnect all power again and switch the black/blue wires again in the motor coils, but nothing changed…

Do you have any idea what may be causing this? Maybe it’s the power… I am using a 12V/2A adapter, with a 47uF capacitor along with the A4988 driver and Arduino Uno…
My guess is that the motor wiring has something to do with it.

Thank you!

yafimski:
Although it should work, it just goes 1 step forward and then back, ticking like a clock…
I think that it does register the “whole 200 step cycle” and then waits 2 seconds like in the code because if left to tick for a while it suddenly will go back and forth really fast for 2 seconds and then start ticking again.

Taking account of the changes you have made you can simplify it further like this. It should make one step every second. If this does not work it suggests the problem is not in the program

byte directionPin = 4;
byte stepPin = 3;
int numberOfSteps = 200;
int pulseWidthMicros = 20;  // microseconds
int millisbetweenSteps = 1000; // milliseconds 

void setup() {

  pinMode(directionPin, OUTPUT);
  pinMode(stepPin, OUTPUT); 
  digitalWrite(directionPin, HIGH);
}

void loop() {

	digitalWrite(stepPin, HIGH);
	delayMicroseconds(pulseWidthMicros); // this line is probably unnecessary
	digitalWrite(stepPin, LOW);
   
	delay(millisbetweenSteps);

}

Make a simple drawing showing how you have everything connected and post a photo of the drawing.

…R

Hi,
Thanks for the help.

I tried your code, still same result…

I’m attaching the diagram you asked for. I made it in Fritzing but it’s essentially identical to the A4988 specification sheet you have in your Stepper Motor Basics post.

Let me know if there is any mistake you can spot…

Thanks.

Image from Reply #10 so we don’t have to download it. See this Image Guide

8cf93e9a45413142b1e84d1fb7403975f05856a8.jpg

…R

Sorry, but I did use the word "drawing" in Reply #9 in the hope that you would not use Fritzing. Fritzing images are just confusing. It is impossible to read the pinouts on the circuit boards. And it is impossible to tell how the wires link to the different motor coils. A poor drawing is generally a lot more informative.

I don't think you have told us what power supply (volts and amps) you are using for the motor.

And now when I look back over this Thread I don't think you have provided a link to the datasheet for the motor - I should have mentioned that earlier but I get mixed up with reading different Threads.

Have you adjusted the current limit on the A4988 to suit your motor?

...R

In reply #8 I mentioned the power supply is a "12V/2A adapter". This is according to the tutorial I followed.

I believe you are right that since the code seems fine, it might be the current limit on the A4988. The motor I am using is:

NEMA17 / 42BYGHW208 Stepper Motor 36oz-in/ 2600g/cm 3D Printer RepRap Medel Prusa

which I bought from Ebay.

The A4988 can be adjusted as I've seen on some videos and tutorials, but I know I need a voltage meter for this and mine doesn't seem to work, I think it's broken. If there is any other way to test the A4988 current let me know, otherwise I might need to try and fix the 'multimeter' I have first...

Thanks

Yes. I missed the reference to the power supply in Reply #8

You need to post a link to the datasheet for the motor. There are hundreds of different Nema17 sized motors.

You do need a working multimeter to measure the voltage while adjusting the A4988. The symptoms you describe might be due to the current limit being set at a very low level - not enough to make the motor take a step.

...R

Thanks. I will try to fix the multimeter.

For now the best I could find from the ebay page is:

Voltage 12 volts .8 to 1 amp You should be able to go up to DC 24 volts 1.2 amps,

Specification: NEMA 17 1.8 deg / step width 42mm x 42mm Voltage: 12V Amps: 0.4A

Does this help to identify what exactly to do?

Thanks!

yafimski: Does this help to identify what exactly to do?

Yes. It seems you need to adjust the current to a max of 0.4 amps. But I would be more comfortable if you post a link to the source of your info. And I don't understand this bit

Voltage 12 volts .8 to 1 amp You should be able to go up to DC 24 volts 1.2 amps,

With a 12v supply there should be no danger of overloading the motor and while it is possible, it is less likely that the driver will be providing insufficient current.

The motor will work much better (higher speed and more torque at speed) with a higher supply voltage. I think the A4988 can go up to 35v. With the higher voltage it will be essential to set the current limit to protect the motor. But don't worry about a higher voltage until you get reliable motion at slow speed.

I assume that you are testing the motor without any load on it?

...R

Hi,

Yes for now my tests involve no large load (maybe a small plastic piece but nothing big). I'm not really interested in fast speed anyway, I would just like to to move in a slow speed, something like 5RPM but very smoothly.

The tutorial I followed uses a 12V/2A adapter: http://howtomechatronics.com/tutorials/arduino/how-to-control-stepper-motor-with-a4988-driver-and-arduino/

And the link for the motor: http://www.ebay.com/itm/NEMA17-42BYGHW208-Stepper-Motor-36oz-in-2600g-cm-3D-Printer-RepRap-Medel-Prusa-/172172587418?hash=item281649319a

Thank you.

This is a quote from your link (same as you already posted)

Voltage 12 volts .8 to 1 amp You should be able to go up to DC 24 volts 1.2 amps,

Specification:

NEMA 17 1.8 deg / step width 42mm x 42mm Voltage: 12V Amps: 0.4A

Assuming the rated current for the motor is 0.4 amps (as in the last line) then the two lines I have coloured red are complete rubbish. The motor current should not be allowed to rise above 0.4 amps - and that is the purpose of the adjuster on the A4988.

If the motor is not working with the simple program in Reply #1 then either the motor is wired incorrectly (or broken - less likely) or the A4988 is broken. It is very easy to destroy a stepper driver if you disconnect a wire from the driver to the motor (even very briefly) while the driver is powered up.

Note that I refer to the program in Reply #1 because I have not tested the program in Reply #9

...R

Thank you.

I will try to fix the multimeter and see what the current on the motor is, maybe it's not 0.4.