Control Stepper Motor with Limit Switches - Arduino Programming



Sketch

// defines pins numbers
const int stepPin = 5;
const int dirPin = 2;
const int enPin = 8;
const int homeSwitchPin = 7;

void setup() {

   Serial.begin(9600);

  // Sets the two pins as Outputs
  pinMode(stepPin,OUTPUT);
  pinMode(dirPin,OUTPUT);
  pinMode(homeSwitchPin , INPUT);
  pinMode(enPin,OUTPUT);
  digitalWrite(enPin,LOW);

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


}
void loop() {

    int homeSw = digitalRead( homeSwitchPin);

    if( homeSw == HIGH && (digitalRead(dirPin) == LOW) ){
     
        motorStep(1);

    }
    else if( homeSw == LOW && (digitalRead(dirPin) == LOW) ){
          digitalWrite(dirPin,HIGH);
          delay(2000);
    }

    if( (digitalRead(dirPin) == HIGH) ){

        motorStep(2000);
        digitalWrite(dirPin,LOW);

        delay(2000);
    }
 
}
void motorStep( int MAX){

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


Limit switch wiring diagram



Stepper motor wiring diagram

Stepper Motor NEMA 17
TB6560 Stepper driver



TB6560  CLK+ (STEP) →  Arduino D5
TB6560 CW+ (DIR) → Arduino D2
TB6560 EN+ → Arduino D8
TB6560  CLK - , CW - , EN- → Arduino GND

ความคิดเห็น

  1. How can get the maximum speed of the motor ???

    ตอบลบ
  2. I am trying to mimic this linear motion with different hardware. I have a DC motor mounted to a platform that sits on a rail that I made out of some square tubing. As far as my program, I have found success Using an h-bridge to control the direction of the motor, but I would like to be able to detect the end of the rail to initiate the switch in motor direction, opposed to having a program dictate a switch in direction based on an estimated time. I am struggling to add the limit switches to this setup, being that I am new to all of this. any Help you can offer as I get started trying to add in the limit switches to change the direction? Also does it seem like my version of this setup can be wired similarly?

    ตอบลบ

แสดงความคิดเห็น