Go Down

Topic: speed control of a nema 17 stepper motor (Read 900 times) previous topic - next topic

goalcoast

Hello,
I have a NEMA 17 stepper motor with TB6560 which is moving forward and backward between two inductive proximity switches. But i am not happy with the speed. I also want to control speed inside my code without using rotary encoder. I am attaching the code with proximity without speed control
Can someone please help me out ?
Code: [Select]
const int stepPin = 8;
const int dirPin = 9;
const int enPin = 10;
const int LimitSwitch_LEFT_Pin  = 12;
const int LimitSwitch_RIGHT_Pin = 13;

void setup() {

  Serial.begin(9600);

  pinMode(LimitSwitch_LEFT_Pin , INPUT);
  pinMode(LimitSwitch_RIGHT_Pin , INPUT);

  // Sets the two pins as Outputs
  pinMode(stepPin,OUTPUT);
  pinMode(dirPin,OUTPUT);

  pinMode(enPin,OUTPUT);
  digitalWrite(enPin,LOW);

  // Set Dir to Home switch
  digitalWrite(dirPin,HIGH); // Enables the motor to move in a particular direction


}
void loop() {

    int leftSw  = digitalRead( LimitSwitch_LEFT_Pin);
    int rightSw = digitalRead( LimitSwitch_RIGHT_Pin);
   
    if( (leftSw  == HIGH && (digitalRead(dirPin) == HIGH)) ||
        (rightSw == HIGH && (digitalRead(dirPin) == LOW)) ){
   
        motorStep(1);

    }
    else if( leftSw == LOW && (digitalRead(dirPin) == HIGH) ){
          digitalWrite(dirPin,LOW);
          delay(2000);
    }
    else if( rightSw == LOW && (digitalRead(dirPin) == LOW ) ){
          digitalWrite(dirPin,HIGH);
          delay(2000);
    }
 
}
void motorStep( int MAX){

   for(int x = 0; x < MAX; x++) {
        digitalWrite(stepPin,HIGH);
        delayMicroseconds(500);
        digitalWrite(stepPin,LOW);
        delayMicroseconds(500);
      }
     
}

Robin2

I also want to control speed inside my code without using rotary encoder. I
I don't understand what you want to do. Do you mean that you just want to change the code so the motor moves at a different speed? If that's what you want just change the interval between steps.

OR

Do you want the user to be able to control the speed while the motor is moving? If so, how will the user convey his/her wishes?

...R
Stepper Motor Basics
Simple Stepper Code
Two or three hours spent thinking and reading documentation solves most programming problems.

johnwasser

Just change your "delayMicroseconds(500);" to a lower value for more speed.
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp
See who has no social life: https://forum.arduino.cc/index.php?action=stats :)

Go Up