Stepper motor making knocking noises when being programed please help!

Hey guys! I am using a nema 17 motor paired with the SparkFun easy driver to move a stepper motor based on an analog input of a laser after triggering a toggle switch.

The motor spins but it spins very slowly and makes knocking noises when I am using it. Can someone look into this and help me out? Here is my code below:

  #define step_pin 3
  #define dir_pin 2
  #define MS1 5
  #define MS2 4
  #define SLEEP 7
  //define pins for stepper driver
  //step_pin-When moves from LOW -> HIGH it will complete 1 step
  //dir_pin-Controlls direction of stepper motor
  //SLEEP-puts controller to sleep or wake up
  //MS1/MS2-Activates microstepping and others
  //-------------------------------------------
  /* Configure type of Steps on EasyDriver:
  // MS1 MS2
  //
  // LOW LOW = Full Step //
  // HIGH LOW = Half Step //
  // LOW HIGH = A quarter of Step //
  // HIGH HIGH = An eighth of Step //
  */
int switchState=0;
int ledPin=13;
int buttonPin=8;
int CCC= A1;
int CWC= A2;
int CCF= A3;
int CWF= A4;
//define what pins controll what component
int buttonStateNew;
int buttonStateOld=1;
int toggleOn=0;
int laserPin = A0;
int laserVal;
//define int variables

void setup() 
{  
  //Driver Pins
   pinMode(MS1, OUTPUT); //Set MS1 as an output signal pin
   pinMode(MS2, OUTPUT);
   pinMode(dir_pin, OUTPUT);
   pinMode(step_pin, OUTPUT);
   pinMode(SLEEP, OUTPUT);

  //Other Pins
  pinMode(ledPin,OUTPUT);
  pinMode(buttonPin,INPUT);
  //pinMode(laserPin,INPUT);
  //MAKE SURETO ADD MORE PINMODES FOR BUTTONS AND THINGS

  Serial.begin(9600);

  digitalWrite(MS1, LOW);
  digitalWrite(MS2, LOW);

  
//Start serial monitor
}

void loop() 
{

  buttonStateNew = digitalRead(buttonPin); //record current button value
  laserVal=analogRead(laserPin);//read the laser value
 //print laser value to serial monitor

  if(buttonStateOld == 0 && buttonStateNew == 1)//checks to see if button was pressed
{
if (switchState==0){//checks to see if Toggle State is off
  digitalWrite(ledPin,HIGH); //turn an LED on
  switchState=1;//change toggle state to on
  toggleOn=1;//define toggle on variable
  
}
else
{ 
  digitalWrite(ledPin,LOW);//turn off LED
  digitalWrite(SLEEP, LOW);
  switchState=0;//set togggle state to off
  toggleOn=0;//define toggle off variable
  
}
}

if(toggleOn==1)
{
  digitalWrite(SLEEP,HIGH);//Wake up driver
}

if(toggleOn==1 && laserVal<462) // 512/2-50 for analog input of laser.  If toggle is on and laser is in range spin until in operating distance.
{
  digitalWrite(dir_pin, LOW); //set direction to counter clockwise
  digitalWrite(step_pin, HIGH); //begin stepping
  digitalWrite(step_pin,LOW);
  Serial.println("Motor is spinning counterclockwise because of laser");
}
if(toggleOn==1 && laserVal>562)// 512/2+50 for analog input of laser. If toggle is on and laser is in range spin intul in operating distance.
{
  digitalWrite(dir_pin, HIGH); //set direction to clockwise
  digitalWrite(step_pin, HIGH); //begin stepping
  digitalWrite(step_pin, LOW);
  Serial.println("Motor is spinning clockwise because of laser");
}
if(toggleOn==1 && laserVal>462 && laserVal<562)
{
  toggleOn=0; //Turn off toggle (stepping is complete)
  switchState=0; //Turn off toggle (stepping is complete)
  digitalWrite(SLEEP, LOW);
}


buttonStateOld=buttonStateNew;
Serial.println(digitalRead(SLEEP));
}

thanks everyone!

Which one.
Nema 17 is the size of the mounting plate (1.7 inch).
Couldn't find it in your cross-post either.
Leo..

Crank it up. I use 115200 baud. After each step pulse try a delay of some milliseconds. Else the stepper might be over triggered and loose steps.

You rock, this worked perfectly!

1 Like

@

Your other topic on the same subject deleted.

Please do not duplicate your questions as doing so wastes the time and effort of the volunteers trying to help you as they are then answering the same thing in different places.

Please create one topic only for your question and choose the forum category carefully. If you have multiple questions about the same project then please ask your questions in the one topic as the answers to one question provide useful context for the others, and also you won’t have to keep explaining your project repeatedly.

Repeated duplicate posting could result in a temporary or permanent ban from the forum.

Could you take a few moments to Learn How To Use The Forum

It will help you get the best out of the forum in the future.

Thank you.

Thanks!
I am, having a good time in the mechanics shop!

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