Help with Arduino Code Pololu A4998 stepper motor

Good morning,

Following the scheme that appears in the datashhet of the A4998 I have already made the connections between one arduino uno, the driver a4998 and one stepper motor.
I would like to know if you already have or know where I can find an arduino code that lets me control the number of steps that the stepper motor turns, as well as the rotation speed.

Thanks in advance. Regards

Hello.

You can use the stepper motor script we posted in this blog post with our A4988. In that script, you can change the number of times the FOR loop will executive to control the number of steps that you stepper motor steps. To control how fast the stepper motor steps, change the delays used in the loop that switches between logic low and high. By changing the delay values, you are essentially changing the wait time between each step pulse.

- Amanda

Hello Amanda,

Thank you very much for your time. I’ve checked out the code and it makes the motor changing the direction each 50 steps. What I want is to rotate the motor in one single direction and stop it chosing the steps I want the motor to do. What modification do I have to do in your code?

Thanks and regards

If you want to make the motor take 100 steps instead of 50, change the 50 in the following line to 100.

I suggest that you learn some C/C++ programming. There are many on-line tutorials.

Hello,

Thanks for the advice Jim, I know that I should start with tutorials…but I do not have too much time and that is why I thought there must have been some code already done for my purpose.
Just few clarifications… to make the motor just run in one direction should I modify the “if” that it is inside the loop and also delete “digitalWrite(STEP_PIN, LOW)” ?

Do you have any suggestions?

Thanks again

Or maybe I thought about disconnecting the pin of direction from the driver considering I am not interested in changing the direction.

Thanks

[quote]but I do not have too much time and that is why I thought there must have been some code already done for my purpose.[/quote]It is extremely unlikely that anyone will have written and/or posted code that does exactly what you want.

This particular forum is primarily addressed toward hobbyists and aims to help them learn how to achieve their goals. Most people who contribute on this and similar forums don’t want to write a program for other users, but are very willing to help if the requester shows willingness to learn.

It is easy to learn the pidgin C of Arduino-speak, but you don’t have time or the interest, then your best option would be to collaborate with someone or hire a programmer. See the “gigs & collaborations” section on the Arduino forum at forum.arduino.cc/index.php?board=26.0

Since you do not need to control the direction of your stepper motor’s rotation, you do not need to connect the DIR pin to your Arduino. However, leaving the DIR pin unconnected means its voltage will be floating, so you should connect it to VCC or GND. (You can find that information under the “Control inputs” section on any of the A4988 product pages.)

I recommend keeping the minimal connections between your Arduino and stepper motor driver and making some modifications to the code to fix the direction of rotation of your stepper motor. If you look at the comments in the example code carefully, you will see that the IF / ELSE code block “toggle[s] the DIR pin to change direction”, and the FOR loop “trigger[s] the motor to take one step” with each execution. You should remove the IF / ELSE code block if you do not want your stepper motor to switch direction every 100 steps (or the number of steps the FOR loop will execute). For example:

int numberOfSteps = 100;

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

void loop()
{
  for(int i = 0; i < numberOfSteps; i++)
    digitalWrite(STEP_PIN, HIGH);
    delay(250);
    digitalWrite(STEP_PIN, LOW);
    delay(250);
  while(1);
}

- Amanda

Good morning Amanda,

Thanks for your last post. As you said I have connected the DIR pin to GND of Arduino. I have defined at the beginning of the last code you wrote the STEP_PIN and not the STEP_DIR. The motor does not respond. I have also tried the example code that you gave me in your first post but the motor does not move. I am using a NEMA 17, 4V and 1.8°/step and I have made all the connections following your schemes. What can be the problem? I have also tried other simple codes, at least the motor turns but it does not follow the code either.

Here I leave you the code you sent me. I have just introduced the definition of the variable STEP_PIN.

Thank you

[code]

#define STEP_PIN 2

int numberOfSteps = 100;

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

void loop()
{
for(int i = 0; i < numberOfSteps; i++)
digitalWrite(STEP_PIN, HIGH);
delay(250);
digitalWrite(STEP_PIN, LOW);
delay(250);
while(1);
}[/code]

Sorry, I forgot the opening and closing curly braces for the FOR loop code block. I corrected the code:

#define STEP_PIN 2

int numberOfSteps = 100;

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

void loop()
{
  for(int i = 0; i < numberOfSteps; i++)
  {  // Opening brace
    digitalWrite(STEP_PIN, HIGH);
    delay(250);
    digitalWrite(STEP_PIN, LOW);
    delay(250);
  }  // Closing brace
  while(1);
}

If you try adding the curly braces to you code as shown above and are still having trouble, can you let me know more about your setup? Can you post pictures of your setup that clearly show how everything is connected (including your power supply)? What is the rated current of your stepper motor? What is the current limit of your DRV8825 set to? If you are not sure how to set the current limit, you might find this video helpful.

- Amanda

Hello Amanda,

I tried your code and still does not work… I attach you a picture of the setup but I guess it is well connected. I am using an ATX source, black cables connect the grounds of arduino, the driver and the source. Pin 2 (blue wire) is the step pin and the green wire, pin 3, is the direction pin.

The rated current is 1.2 A/phase. I have already set the current of the driver following the equation that appears in the datasheet and the video.

By the way I have found another code that works from the library of arduino. The only problem is that I want to stop the motor once it has rotated the degrees I want, right now as the instruction it is inside the loop it keeps doing the function. Shall I place a button on the breadboard or is there a possibility to do it directly from the code? If so how can I connect the button and what is it the modification I have to do in the code? I show you the code that it is working

Thank you very much

[code]#include <Stepper.h> //Imported from the library

#define STEPS 200 //set the number of steps

// define the motor name, step pin, and direction pin
Stepper stepper(STEPS, 2, 3);

void setup()
{
// speed RPM
stepper.setSpeed(100);
}

void loop()
{
//make a turn in one direction
stepper.step(200);
delay(500); //pause

}[/code]

Unfortunately, it looks like the link to your picture is broken. Can you try posting it again?

I tested the code from my previous post using an Arduino Uno, an A4988, and a stepper motor and was able to get the stepper motor to step. Can you be more specific when you said the code “does not work” (e.g. the code does not move your stepper motor the way you expect it to or your stepper motor does not move at all)? Are you using any microstepping modes? When using microstepping that code will make the motor take 100 microsteps, so sometimes it is difficult to plainly see if a stepper motor is moving while microstepping. Did you try holding the shaft of your stepper motor very lightly to verify that it was not moving? Could you measure the VREF voltage of your driver?

After briefly looking at the Arduino’s Stepper.h library, it looks like it is meant to be used with a different type of driver. Are you using the A4988 stepper motor driver with the example code you posted?

- Amanda

Hello Amanda,

Thanks for your post. What I mean is that the motor does not move at all, even if I hold the shat I do not feel a try to start moving. I adjusted the Vref voltage considering the formula Current/0.7=Vrefx2.5. With my stepper of 1.2 A I get 0.685 V, also my setup is done following your scheme of the a4998.

Image of my setup: goo.gl/photos/XYyXuRCYK1xjHLSt6

With the second code I posted the motor works. I only have the problem that the steps that it turns do not correspond with the ones I write in the code. In particular to make a whole turn I need to write 800, to make a quarter 200, etc…At the beginning I thought it was due to the microstepping but I do not know now. what do you think? and why do you think the stepper library is not useful with the a4998?

Do you have a picture of your setup so I can compare it with mine? And last thing, did you give a thought to how to stop the motor?

Thank you very much Amanda

From your picture, it looks like you have the A4988’s STEP pin connected to the Arduino’s GND pin and the DIR pin connected to the Arduino’s pin 2. Since you had the STEP pin grounded, the A4988 cannot make your stepper motor move at all. Can you fix the connections between your Arduino and A4988 by connecting the STEP pin to pin 2 and the DIR pin to GND? After fixing those connections, can you try running the sample code I gave you to see if your stepper motor steps? Also, I noticed that there is a mark on the driver’s chip which might indicate damage. If there is still no movement from your stepper motor after those changes, I suspect the driver is damaged.

As for your question about stopping your stepper motor, the WHILE loop while(1) at the end of the FOR loop will prevent further execution of the code, essentially halting further movement of your stepper motor.

- Amanda

Hello Amanda,

Yeap you were right about the connections…what a mistake…

However the motor steps it is not working accurately, it misses steps and sometimes it stops. Do you have any idea of why i doesn’t work properly?

Also I have noticed that the driver gets really hot. I do not know what can it be because I regulated the voltage according to the motor amperage…

Thank you again

It sounds like your driver might be overheating. Although the A4988’s datasheet lists an absolute maximum output current of 2A, our A4988 stepper motor driver carrier can typically only deliver about 1A continuously without additional cooling. Can you try lowering the current limit of your A4988 to less than 1A (VREF=0.4V) to see how the motor behaves? If nothing changes, then your A4988 is likely damaged in some way.

- Amanda