Stepper Motor Inaccuracy, Code or Motor?

Hi all, I'm new to Arduino and need some advice regarding my project. I'm trying to use a stepper motor to perform very specific set of movements (rotate CW 90, CCW 180, etc etc), it's not precise to the point of 3d printing, but it needs to be as fast as possible (fastest acceleration/deceleration the motor can provide).

I've included my schematic and very simple code below. I'm running 5V through a Nema17HS4023 (1A, 4.1V).

Right now I just want my code to rotate the motor accurately and quickly from 0-90, and then 90-0. Such that for every loop, the motor should've returned back to the same starting position.

On start up, the motor starts sweeping correctly, but quickly ends up going to a non-0 position, because it stops turning the full 90 degrees. Sometimes it also makes whining noises and stop alternating movements completed. Is this code, or misstep, or vRef issue?

Any guidance much appreciated.

#include <MobaTools.h>

const int stepPin = 9;
const int dirPin = 8;
const int stepsPerRev = 200;

MoToStepper stepper( stepsPerRev, STEPDIR );

void setup()
{  
  stepper.attach( stepPin, dirPin );
  stepper.setSpeed(2500);
  stepper.setRampLen( stepsPerRev / 2);
  Serial.begin(9600);
  stepper.setZero();
}
 
void loop()
{
  Serial.println("clockwise");
  stepper.write(90);    
  while ( stepper.moving() ); // wait
  delay(500);

 Serial.println("Counter clockwise");
  stepper.write(-90);         
  while ( stepper.moving() ); // wait
  delay(500);
}


What’s the motor driving (load) ?
What are you using for positional feedback ?
Driver / power supply to the motor ?

1 Like

Could be Vref. Could be missteps. Could be 5V isn't enough. What will your unspecified driver take? What happens if you slow things down?

With a ramp length of stepsPerRev / 2 =100 steps, it is still accelerating up to the midpoint of 200/4/2=25 steps. I'd try halving it to stepper.setSpeed(1250); or stepper.setSpeed(625); and see if it stabilizes.

1 Like

I'm not quite shure what your driver is. From the picture it seems to be a DRV8825. But in any case 5V isn't enough for this kind of driver. Try with at least 12V. And did you set the coil current accordingly?

2 Likes

Sorry, should have specified, my stepper driver is the standard A4988. For some reason fritzing didn't have the exact board in the templates.

I'm using a 12V 2A adapter connected to a breadboard power supply module (5V) to power the motor. Is this likely the problem?

Very likely. 5V is definitely not enough for the A4988.
Connect the 12 V directly to the Vmot of the driver.

1 Like