Struggling to change directions in Stepper motor using AccelStepper library?

I`m currently using a Nema 17 (200 steps) and a DRV8825. This is the configuration I am using:

I am using a 9 Volt power supply and my dirPin is connected to pin number 3 in the arduino board and my stepPin to pin number 2. For now what I'm trying to do is to move the motor 100 steps clockwise and then move it 100 steps anti-clockwise, so that it returns to the starting position. This is the code I am using:

#include <AccelStepper.h>

#define dirPin 3
#define stepPin 2
#define motorInterfaceType 1

int SPR = 200;

AccelStepper stepper(motorInterfaceType, stepPin, dirPin); 

void setup() {
  pinMode(dirPin,OUTPUT);
  pinMode(stepPin,OUTPUT);
  // put your setup code here, to run once:
  stepper.setMaxSpeed(200);
  stepper.setAcceleration(30);
}

void loop() {
  // put your main code here, to run repeatedly:
  stepper.moveTo(100);

  stepper.runToPosition();
  delay(1000);

  stepper.moveTo(-100);

  stepper.runToPosition();
  delay(1000);
}

But I am having 2 issues:

  1. Firstly before moving the motor 100 steps, it moves like a couple of steps (like 15 degrees), and then it starts moving. Firstly 100 steps, and then 200 steps. That would be the first iteration, and I don´t understand why it moves those 200 steps. And then, in the following iterations, the motor just moves 200 steps.

  2. Secondly, it just moves in one direction. I can´t manage to move it in the anti-clockwise direction.

Thanks for reading, if someone can help me I would greatly appreciate it.

This topic covers changing motor direction using AccelStepper library...

Check that your motor coil wires aren't crossed. If you swap one of the coil A wires for a coil B wire, you can get one-directional rotation and weakness.

Also:

How strong is your 9V power supply? An inadequate supply relative to your stepper could give strange results.

Nema 17 is the size of the mounting plate (1.7").
It tells us nothing about the electrical specs of the motor.

Absolute minimum motor/driver voltage is 8.2volt, so 9volt is very close to the edge of the cliff.
And that better not be a 9volt smoke alarm battery.

Now tell us what you are actually using.
Leo..

1 Like

"I have problems towing my trailer. It is a 4-door car and a 2 wheel trailer."

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.