Stepper Motor Wiring help needed

I've been trying to get my Stepper motor to work for a very long time now and I'm just stuck at this point. The motor keeps vibrating back and forth and not turning correctly. The code I am using is just a simple example code from a tutorial video. The Motor I am using is a Two trees Nema 17 if that matters, with a 12 Volt plugin from the wall. I believe it may be the wiring but I am not sure`// Define pin connections & motor's steps per revolution. Here's my wiring and code (Ignore the buttons).

const int dirPin = 4;
const int stepPin = 3;
const int stepsPerRevolution = 200;
int stepDelay=400;

void setup()
{
  // Declare pins as Outputs
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
}
void loop()
{
  //clockwise
  digitalWrite(dirPin, HIGH);

  // Spin motor
  for(int x = 0; x < stepsPerRevolution; x++)
  {
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(stepDelay);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(stepDelay);
  }
  delay(2000); // Wait 2 seconds
  
  //counterclockwise
  digitalWrite(dirPin, LOW);

  // Spin motor
  for(int x = 0; x < stepsPerRevolution; x++)
  {
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(stepDelay);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(stepDelay);
  }
  delay(2000); // Wait 2 seconds
}



Hi Newbie welcome to the community

Kindly go through it
Guidelines

Also kindly post proper circuit diagram as the images are not sufficient to understannd

1 Like

Cant tell from the pics:

i would say change the wires around to test ive had the same and switched wire positions and it started working normally.. on a way smaller stepper tho but it should work the same

With step delay of 400 microseconds that's 1250 steps per second (375 RPM). Your motor cannot jump from 0 to 1250 SPS instantly, increase step delay to 3000 (3 thousand, 50 RPM) and work your way up by making step delay gradually lower.

int stepDelay=3000;

EDIT: Please excuse my math blunder, should be correct now. :grimacing:

Thank you it is now working correctly

1 Like

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