alihan0742 / Endstop_LimitSwitch_Step_Motor

Movement in Step Motor Linear Axis with Limit Switches
3 stars 2 forks source link

needs your help on small project #1

Open nkr80 opened 4 years ago

nkr80 commented 4 years ago

hi my name is nadeem and i'm trying to built a prototype like yours but i'm trying to make a code on my Arduino (mega) using:- Stepper Motor //L298N// potentiometer.. i will link the current Arduino code that i'm using also the connection.. i need your help to make that code on my current connection.. also i need to add 2 limit switches, one on each side of the shaft

the operation needed is moving the shaft from home postion (limit swtch A) TO end postion (limit swtch B) and back to home postion by set a period of time ( like 1 min )..

the connection https://i0.wp.com/dronebotworkshop.com/wp-content/uploads/2018/02/Bipolar-Stepper-L298N.jpg?w=768&ssl=1%20768w,%20https://i0.wp.com/dronebotworkshop.com/wp-content/uploads/2018/02/Bipolar-Stepper-L298N.jpg?resize=300%2C169&ssl=1%20300w

the code is:-

/* Stepper Motor Demonstration 3 Stepper-Demo3.ino Demonstrates NEMA 17 Bipolar Stepper with L298N Driver Uses Potentiometer on Analog Input A0 Uses Arduino Stepper Library

DroneBot Workshop 2018 https://dronebotworkshop.com */

// Include the Arduino Stepper Library

include

// Define Constants

// Number of steps per output rotation const int STEPS_PER_REV = 500; const int SPEED_CONTROL = A0;

// Create Instance of Stepper Class // Specify Pins used for motor coils // The pins used are 8,9,10,11 // Connected to L298N Motor Driver In1, In2, In3, In4 // Pins entered in sequence 1-2-3-4 for proper step sequencing

Stepper stepper_NEMA17(STEPS_PER_REV, 8, 9, 10, 11);

void setup() { // nothing to do inside the setup }

void loop() { // read the sensor value: int sensorReading = analogRead(SPEED_CONTROL); // map it to a range from 0 to 100: int motorSpeed = map(sensorReading, 0, 1023, 0, 100); // set the motor speed: if (motorSpeed > 0) { stepper_NEMA17.setSpeed(motorSpeed); // step 1/100 of a revolution: stepper_NEMA17.step(STEPS_PER_REV / 100); } }

alihan0742 commented 4 years ago

hello nadeem; Sorry, my work is very intense. While I am thinking over your Code, I can suggest you this. First of all, you should get the speed code you set with the potentiometer at the beginning. For example with a while loop ... Then you should embed my endstop code line (I have shared below) containing the flag I wrote in the center of your code. I would like to help you more. Do you have a delivery time? ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// int limSw1 = digitalRead( limitSw_1); int limSw2 = digitalRead( limitSw_2);

if( limSw1 == HIGH && (digitalRead(dirPin) == LOW) ){
    motorStep(2000); 
}

else if( limSw1 == LOW && (digitalRead(dirPin) == LOW) ){
      digitalWrite(dirPin,HIGH);
      delay(2000);
}
else if( limSw2 == HIGH && (digitalRead(dirPin) == HIGH) ){
    motorStep(2000);
}
else if( limSw2 == LOW && (digitalRead(dirPin) == HIGH) ){
      digitalWrite(dirPin,LOW);
      delay(2000);
nkr80 commented 4 years ago

thank you brother for your fast response.. sure i will.. i also will identify both limit switches on my Arduino pins like const int limitSw_1 = 4 const int limitSw_2 = 5

nkr80 commented 4 years ago

and for the speed code that i need to set it with the potentiometer, it will be set it up manually..

nkr80 commented 4 years ago

question so for your code, i have to set it as [ void setup } right..?? like

void setup() { if( limSw1 == HIGH && (digitalRead(dirPin) == LOW) ){ motorStep(2000); }

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

nkr80 commented 4 years ago

and just to give you more idea about what i'm looking for, is just like shown on this YouTube like https://www.youtube.com/watch?v=y-od0Gzj7P0&feature=youtu.be

it will be same movement principle..

alihan0742 commented 4 years ago

No this code snippet will be in loop..

void loop() if( limSw1 == HIGH && (digitalRead(dirPin) == LOW) ){ motorStep(2000); }

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

alihan0742 commented 4 years ago

Nice project, İnductive sensor = endstop, codes almost the same

nkr80 commented 4 years ago

Thank you man I will try that code..

nkr80 commented 4 years ago

i got an error it is said 'dirPin' was not declared in this scope

nkr80 commented 4 years ago

i'm using L298N Motor Driver and this one does not have pin for direction it is connect to Arduino through In1, In2, In3, In4

https://i0.wp.com/dronebotworkshop.com/wp-content/uploads/2018/02/Bipolar-Stepper-L298N.jpg?w=768&ssl=1%20768w,%20https://i0.wp.com/dronebotworkshop.com/wp-content/uploads/2018/02/Bipolar-Stepper-L298N.jpg?resize=300%2C169&ssl=1%20300w

nkr80 commented 4 years ago

OK, i think i made it right could you check this code for me please..?

include

const int STEPS_PER_REV = 500; const int SPEED_CONTROL = A0; const int limitSw_1 = 4; const int limitSw_2 = 5; int limSw1 = digitalRead( limitSw_1); int limSw2 = digitalRead( limitSw_2); int IN1 = 8; //control pin for first motor int IN2 = 9; //control pin for first motor int IN3 = 10; //control pin for second motor int IN4 = 11; //control pin for second motor

Stepper stepper_NEMA17(STEPS_PER_REV, 8, 9, 10, 11);

void setup() { pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT); pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT); }

void loop() { // read the sensor value: int sensorReading = analogRead(SPEED_CONTROL); // map it to a range from 0 to 100: int motorSpeed = map(sensorReading, 0, 1023, 0, 100); // set the motor speed: if (motorSpeed > 0) { stepper_NEMA17.setSpeed(motorSpeed); // step 1/100 of a revolution: stepper_NEMA17.step(STEPS_PER_REV / 100);

if( limSw1 == HIGH ){ digitalWrite(IN1, LOW); digitalWrite(IN2, HIGH); digitalWrite(IN3, LOW); digitalWrite(IN4, HIGH); }

else if( limSw2 == HIGH ){ digitalWrite(IN1, HIGH); digitalWrite(IN2, LOW); digitalWrite(IN3, HIGH); digitalWrite(IN4, LOW); } }

}

nkr80 commented 4 years ago

ok for this part int IN1 = 8; //control pin for first motor int IN2 = 9; //control pin for first motor int IN3 = 10; //control pin for second motor int IN4 = 11; //control pin for second motor

i meant int IN1 = 8; //control pin for first motor coil#1 int IN2 = 9; //control pin for first motor coil#1 int IN3 = 10; //control pin for first motor coil#2 int IN4 = 11; //control pin for first motor coil#2

nkr80 commented 4 years ago

another fail..!! it is not reversing like the limit switch pins (4, 5) are not doing anything.. please help

alihan0742 commented 4 years ago

My work is very heavy bro. Using ln298 first, make the engine go back and forth in a particular region. Because there is a long way ahead of you.

alihan0742 commented 4 years ago

Then you need to add the sensor code so you can understand better.

nkr80 commented 4 years ago

I ends up with this code It doing the job needed By switching to a different rotations using these limit switches, only if i keep holding the limit switch for each direction.

nkr80 commented 4 years ago

include

const int STEPS_PER_REV = 700; const int SPEED_CONTROL = A0; const int limitSw_1 = 4; const int limitSw_2 = 5;

Stepper stepper_NEMA17(STEPS_PER_REV, 8, 9, 10, 11);

void setup() { stepper_NEMA17.setSpeed(60); }

void loop() {

// int sensorReading = analogRead(SPEED_CONTROL);

// int motorSpeed = map(sensorReading, 0, 1023, 0, 100);

//if (motorSpeed > 0) { // stepper_NEMA17.setSpeed(motorSpeed);

// stepper_NEMA17.step(STEPS_PER_REV / 100); //}

int limSw1 = digitalRead( limitSw_1); int limSw2 = digitalRead( limitSw_2);

if( limSw1 == HIGH && digitalRead( limitSw_1) == LOW){ Serial.println("clockwise"); stepper_NEMA17.step(STEPS_PER_REV); delay(10); }

if( limSw2 == HIGH && digitalRead( limitSw_2) == LOW){ Serial.println("counterclockwise"); stepper_NEMA17.step(-STEPS_PER_REV); delay(10); }

}

nkr80 commented 4 years ago

all I need now is just to have the stepper motor keep rotating with only one press on the limit switch ((without needing to hold the limit switch)).. So what I need to add to this code to make it works.. thank you

nkr80 commented 4 years ago

meaning:- one press on the first LIMIT SWITCH ( limSw1 ) to rotate clockwise and keeps rotating till press the other LIMIT SWITCH ( limSw2 ) for one time only and also keep rotating ( counterclockwise ) till hit the first LIMIT SWITCH ( limSw1 )..

alihan0742 commented 4 years ago

Man, I can't look at it. I need to use all of the limit switch codes. Otherwise, the limit switch or inductive sensor circuit will not work properly.,