Stepper Motor Jitters

Hi, I am using the A4988 stepper motor driver with a RAMPS 1.4 and arduino Mega 2560 to run a 200 step 1.8 degree stepper motor. When I run a basic code, the motor runs smoothly for a little and then starts to jitter while rotating. Is there any reason for this?

Here is an example of the code I’ve been using, though I have been playing around with different sample codes I have found online and experienced the same issues.

#define STEP_PIN         54
#define DIR_PIN          55
#define ENABLE_PIN       38

int i = 0;

void setup() {
  pinMode(STEP_PIN, OUTPUT);
  pinMode(DIR_PIN, OUTPUT);
  pinMode(ENABLE_PIN, OUTPUT);
  
  digitalWrite(ENABLE_PIN, LOW);
  
}

void loop(){
 
  if ( i < 3230){ //it appears that for i = 3230 is for one rotation
    // if i < 32300 (10 rotations) sputtering is more obvious
    digitalWrite(DIR_PIN, HIGH);
   
    i++;
  }
  else{
    digitalWrite(ENABLE_PIN, HIGH);
    //digitalWrite(DIR_PIN, LOW);
  
  }
  digitalWrite(STEP_PIN    , HIGH);
      
      
 
  delayMicroseconds(100); //play with this for speed!... still jitters at slower paces
  // delayMicroseconds can be as low as 28, max without sputtering = 
      
  // delay(1);
      
      
  digitalWrite(STEP_PIN    , LOW);
  
}

Also if anyone knows how to control the motor via the 200 steps/rotation the motor it is supposed to operate at, rather than the random iteration variable, i, which seems to work when run through a for loop 2320 times for 1 rotation I would appreciate it!

Any further example code on having precise control of the motor would be greatly appreciated!

Hello.

It looks like you have no delay between when you make STEP go LOW and when you make it go HIGH again. I recommend adding a delay there to comply with the timing parameters described on page 6 of the A4988 datasheet (the low pulse width should be at least one microsecond).

- Ryan

But I do have the delay…

[code]digitalWrite(STEP_PIN , HIGH);

delayMicroseconds(100);

digitalWrite(STEP_PIN , LOW);[/code]

Also can someone please direct me on how to actually STEP through running a motor?! I can’t find ANYTHING!!! I’m using a 200 step/revolution motor so I would need a code that if I say run 100 steps (as in I want to be able to have steps as an input) it should move 180 degrees exactly. Again, it moves smoothly initially the first number of revolutions at lower speeds, then makes awkward pauses until it is told to stop! I’ve already tried the AccelStepper library with no luck, and I’ve been searching ALL auduino related websites and cannot find anything! Please help!

You do not have a delay between going LOW and then going HIGH again – the next iteration of loop() runs right after the first iteration returns.
Add another delay right after the LOW write, and see what happens.

This is a video on what is still happening…

Jitterring may not be the right word, but it is definitely not doing what it is supposed to…

Also can someone please direct me on where to find code to control the number of steps the motor goes through??? I know it exists because people have mentioned it on all of the forums I’ve been checking out, but none seem to be compatible with the A4988 driver or they only mention it in passing like it is so obvious, but it isn’t to me! I really need full precise control of the motor for what I want to eventually use it for.

Based on that video, it doesn’t look like the missing delay is your problem, but it would be good to have that just for correctness sake.

It looks like your A4988 could be going into thermal shutdown. What do you have the current limit set to? Can you measure the coil current as it is running? Can you try turning down the current limiting to see if the problem goes away or takes longer to start?

- Ryan

How do I change the current limit?

I do not have much of an electronics background… I’ve been following wiring directions from reprap.org/wiki/RAMPS_1.4

THANK YOU!!! That was it!!! it runs WONDERFULLY now!!!

But… now that I have your attention do you know can you share with me an arduino code that would give me precise control on the motor through steps? and not the arbitrary iterations I use now???

Great! I’m glad the fix was so easy.

I do not exactly understand how you want to improve your code. You could try using the AcellStepper library.

- Ryan

You should also understand how to simply send out a certain number of steps using a for loop. For example, this code would send the specified number of high pulses out to the step pin, making your motor turn by a certain amount:

int step_count = 100;
for(int i = 0; i < step_count; i++)
{
    digitalWrite(STEP_PIN, HIGH);
    delay(9);
    digitalWrite(STEP_PIN, LOW);
    delay(1);
}

Once you understand how and why that works, you can better appreciate the advanced features that the AccelStepper library would give you.

–David

Do you by any chance have a sample code using the pololu driver and the accelStepper library?

Or can you at least help me understand how to control the motor by steps? Is a step defined as everytime the stepper pin is turned on and off? If that’s so, why is it that every time I program it to turn on and off 200 times, my motor does not make a full rotation (I know that I am using a 200 step motor).

If you search Google for “A4988 accelstepper” I am sure you will find lots of good examples. I haven’t read it carefully, but this one is from a reputable source and looks good at first glance:
pjrc.com/teensy/td_libs_AccelStepper.html

The number of steps your motor makes depends on which micro-stepping mode you have selected. If you are using half-stepping, for example, you would need to send two pulses to the STEP pin for every full step you want the motor to make. If you want more help, please post your code, connections, and a description of what the motor is actually doing (is it rotating 359 degrees or just 180 degrees)? Videos and photos would help.

–David

There’s a great introduction here: schmalzhaus.com/EasyDriver/E … mples.html

He goes from the most basic iteration, to using accelstepper. It’s the same process as the Pololu’s driver, because it’s the same chip (he either uses a4988 or a4983, but pololu’s a4988 is the superior version of a4983)

over break, I hope to put up an instruct-able about using accelstepper and then switching to manual mode after you read accelstepper’s limit (which on the Arduino is about 4000steps/second, and on BabyO would be 5000steps/second.) There probably exists something somewhere, but mine will be newb optimized, at least insofar as I’m a newb. :slight_smile: