Problems with Stepper Motor + Pololu a4998

Good afternoon,

I have made all the connections following the scheme at the webpage of Pololu but my stepper motor does not respond. It is a NEMA 17 4V 1.8°/step. My purpose is rotating the motor in one single direction a number of steps.

This is the code I am using:

#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);
}

Following the instructions of Pololu I have connected the pin DIR to the GND of Arduino, even if I am not interested in using it, the voltage can fluctuate if I leave it without connection.

Apart from this I have tried another code. The motor responds but it does not do what it is written in the code.

Here is the second code:

#include <Stepper.h> //Importamos la librería para controlar motores paso a paso

#define STEPS 200 //Ponemos el número de pasos que necesita para dar una vuelta. 200 en nuestro caso

// Ponemos nombre al motor, el número de pasos y los pins de control
Stepper stepper(STEPS, 2, 3); //Stepper nombre motor (número de pasos por vuelta, pins de control)

void setup()
{
// Velocidad del motor en RPM
stepper.setSpeed(100);
}

void loop()
{
//Girar una vuelta entera en un sentido
stepper.step(200);
delay(500); //Pequeña pausa

//Girar una vuelta entera en sentido contrario
stepper.step(-200);
delay(500); //Pequeña pausa
}

What can be the problem. You have any suggestions to modify the code, in particular the first one?

Thank you

Hi,

Can you please post a copy of your sketch, using code tags?
They are made with the </> icon in the reply Menu.
See section 7 http://forum.arduino.cc/index.php/topic,148850.0.html

This will give your sketch its own scroll-able window.

Tom… :slight_smile:

Please post a link to the datasheet for your stepper motor.

You should do pinMode() before digitalWrite()

Have a look at stepper motor basics and this simple stepper code ...R

Its probably setup for microsteps, doing 2 microsteps per second is very slow, change those delays to 5 ms perhaps?

Hello,

Sorry for the code. I post it again.

#define STEP_PIN 2

int numberOfSteps = 100;

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

}

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);
}

I have put before the pinmode and change the delay value but still does not work… my motor is bipolar, 1.2 A/phase and 4V.

Regarding the second code it works now. I post it:

#include <Stepper.h> //Importamos la librería para controlar motores paso a paso
 
#define STEPS 200 //Ponemos el número de pasos que necesita para dar una vuelta. 200 en nuestro caso
 
// Ponemos nombre al motor, el número de pasos y los pins de control
Stepper stepper(STEPS, 8, 9); //Stepper nombre motor (número de pasos por vuelta, pins de control)
 
void setup()
{
  // Velocidad del motor en RPM
  stepper.setSpeed(100);
}
 
void loop()
{
  //Girar una vuelta entera en un sentido
  stepper.step(200);
  delay(500); //Pequeña pausa
 
  
}

If I use the second one I would be interested in stoping the motor once it has rotated the steps I want. Is it possible to do it directly from the code or is it neccesary to place a button on the breadboard? If so could you please tell me how should I modify the second code and how to do the connection of the button?

Thanks for your time

victorhernaiz: If I use the second one I would be interested in stoping the motor once it has rotated the steps I want.

I don't understand.

Your code contains

stepper.step(200);

Doesn't that stop after 200 steps ? I presume 200 is the number of steps you want ?

...R

Yes exactly. It makes one turn but after the delay that is set it starts making another turn, and then another...as the function it is inside the loop.

I would like to stop it. That is why I was asking you about incorporating a button or if you know how to do it directly from the code.

Thanks

victorhernaiz: Yes exactly. It makes one turn but after the delay that is set it starts making another turn, and then another...as the function it is inside the loop.

I would like to stop it. That is why I was asking you about incorporating a button or if you know how to do it directly from the code.

Thanks

Then you need a variable to record the state of your system - perhaps motorHasMoved and code like this

if (motorHasMoved == false) {
    stepper.step(200);
    motorHasMoved = true;
}

...R