Go Down

Topic: Stepper Motor with Limit Switch Not Working (Read 177 times) previous topic - next topic

KennethRedaj



https://www.screencast.com/t/Bmd05CCNN

Code: [Select]

// constants won't change. They're used here to set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
  Serial.begin(9600);
}

void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
    // turn LED on:
    digitalWrite(ledPin, HIGH);
    Serial.println(buttonState);
  } else {
    // turn LED off:
    digitalWrite(ledPin, LOW);
     Serial.println(buttonState);
  }
}



So, this code basically uses the pushbutton to turn on and off the LED. When the button is pressed down it turns on. This part works fine.



https://www.screencast.com/t/wohj6YFe3Z

Now, all that was added to the previous sketch is a stepper motor with a controller. The switch circuit remains the same.

Code: [Select]
//Include the Arduino Stepper Library
#include <Stepper.h>

const float STEPS_PER_REV = 32;
const float GEAR_RED = 64;
const float STEPS_PER_OUT_REV = STEPS_PER_REV * GEAR_RED;
const int buttonPin = 2;     // the number of the pushbutton pin

int StepsRequired;
int buttonState = 0;
int counter = 0;
int PreviousbuttonState = 0;

// Create Instance of Stepper Class
// Specify Pins used for motor coils
// Connected to ULN2003 Motor Driver In1, In2, In3, In4
// Pins entered in sequence 1-3-2-4 for proper step sequencing
 
Stepper steppermotor(STEPS_PER_REV, 8, 10, 9, 11);
 
void setup()
{
pinMode(buttonPin, INPUT);
Serial.begin(9600);

 
}

void loop()
{
 buttonState = digitalRead(buttonPin);
  
 if (buttonState == LOW)
 {
  StepsRequired  =  STEPS_PER_OUT_REV;  
  steppermotor.setSpeed(500);  
  steppermotor.step(StepsRequired);
  Serial.println(buttonState);
  delay(50);
  
 }
if (buttonState == HIGH)
{
StepsRequired  =  STEPS_PER_OUT_REV;  
  steppermotor.setSpeed(0);  
  steppermotor.step(StepsRequired);
  Serial.println(buttonState);
  delay(50);
}
}
 


Now this code does not work. I want the motor to stop when the pushbutton is pressed and resume when it is let go. I want this to work as a limit switch for a slider im making. There will eventually be 2 limit switches on either side of a rail system. Can you help me figure out what the problem is with my code or connection? Thank you!

MechEng87

Is this a unipolar stepper motor (5 wires) or a bipolar stepper(4wires)? the ULN2003 youve got in your comments tells me its a 5 wire unipolar stepper motor. I havent used one of those before but if thats the case I can get a set setup going with your code and see whats going on.

larryd

No technical PMs.
If you are asked a question, please respond with an answer.
If you are asked for more information, please supply it.
If you need clarification, ask for help.

larryd

The top cct. will not work as there is no GND  to the Arduino.

Let see your actual wiring.



No technical PMs.
If you are asked a question, please respond with an answer.
If you are asked for more information, please supply it.
If you need clarification, ask for help.

KennethRedaj

#4
Aug 13, 2019, 07:20 am Last Edit: Aug 13, 2019, 07:28 am by KennethRedaj
Is this a unipolar stepper motor (5 wires) or a bipolar stepper(4wires)? the ULN2003 youve got in your comments tells me its a 5 wire unipolar stepper motor. I havent used one of those before but if thats the case I can get a set setup going with your code and see whats going on.
The motor itself (28BYJ-48) is unipolar as it has 5 wires out of it.

https://mega.nz/#F!TVxx2IaA!OV8d3PGvaHUbU9uH6BqS6g

KennethRedaj

The top cct. will not work as there is no GND  to the Arduino.

Let see your actual wiring.




Pictures in this folder below

https://mega.nz/#F!TVxx2IaA!OV8d3PGvaHUbU9uH6BqS6g

MechEng87

Wiring looks fine, what I think is causing it to not work in general is the second if statement, the code seems to break when you put a (0) in for steppermotor.setSpeed(). I deleted that section and got the following to work.

Code: [Select]

//Include the Arduino Stepper Library
#include <Stepper.h>

const float STEPS_PER_REV = 32;
const float GEAR_RED = 64;
const float STEPS_PER_OUT_REV = STEPS_PER_REV * GEAR_RED;
const int buttonPin = 2;     // the number of the pushbutton pin

int StepsRequired;
int buttonState = 0;
int counter = 0;
int PreviousbuttonState = 0;
#define ledPin 13
// Create Instance of Stepper Class
// Specify Pins used for motor coils
// Connected to ULN2003 Motor Driver In1, In2, In3, In4
// Pins entered in sequence 1-3-2-4 for proper step sequencing

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

void setup()
{
  pinMode(buttonPin, INPUT);
  Serial.begin(9600);

  pinMode(ledPin, OUTPUT);
}

void loop()
{
  buttonState = digitalRead(buttonPin);


  if (buttonState == LOW)
  {
    StepsRequired  =  STEPS_PER_OUT_REV;
      steppermotor.setSpeed(500);
      steppermotor.step(STEPS_PER_OUT_REV);
    Serial.println(buttonState);
    delay(50);

  }
  else
  {
    /*StepsRequired  =  STEPS_PER_OUT_REV;
    steppermotor.setSpeed(0);
    steppermotor.step(StepsRequired);*/
    Serial.println(buttonState);
    delay(50);
  }

}


Although I don't think it will work as you intend. The code currently looks at the switch state, then goes the full STEPS_PER_OUT_REV before checking the switch again. If the limit switch gets pressed mid way through the motion, it will  be ignored much like the difference between the Blink example sketch and the Blink Without Delay example sketch.

You could change the steppermotor.step(StepsRequired) line to just do one step, check the switch again, then do another step but that's going to tie up the arduino having to constantly check the switch and not doing anything else in your code. I would set it up to spin the motor which ever way you need then generate a hardware interrupt on the switch pin to stop the rotation that way you can do other stuff with your code too while the motor is rotating and whenever the switch is pressed no matter what it will stop the motor.

KennethRedaj

Wiring looks fine, what I think is causing it to not work in general is the second if statement, the code seems to break when you put a (0) in for steppermotor.setSpeed(). I deleted that section and got the following to work.

Code: [Select]

//Include the Arduino Stepper Library
#include <Stepper.h>

const float STEPS_PER_REV = 32;
const float GEAR_RED = 64;
const float STEPS_PER_OUT_REV = STEPS_PER_REV * GEAR_RED;
const int buttonPin = 2;     // the number of the pushbutton pin

int StepsRequired;
int buttonState = 0;
int counter = 0;
int PreviousbuttonState = 0;
#define ledPin 13
// Create Instance of Stepper Class
// Specify Pins used for motor coils
// Connected to ULN2003 Motor Driver In1, In2, In3, In4
// Pins entered in sequence 1-3-2-4 for proper step sequencing

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

void setup()
{
  pinMode(buttonPin, INPUT);
  Serial.begin(9600);

  pinMode(ledPin, OUTPUT);
}

void loop()
{
  buttonState = digitalRead(buttonPin);


  if (buttonState == LOW)
  {
    StepsRequired  =  STEPS_PER_OUT_REV;
      steppermotor.setSpeed(500);
      steppermotor.step(STEPS_PER_OUT_REV);
    Serial.println(buttonState);
    delay(50);

  }
  else
  {
    /*StepsRequired  =  STEPS_PER_OUT_REV;
    steppermotor.setSpeed(0);
    steppermotor.step(StepsRequired);*/
    Serial.println(buttonState);
    delay(50);
  }

}


Although I don't think it will work as you intend. The code currently looks at the switch state, then goes the full STEPS_PER_OUT_REV before checking the switch again. If the limit switch gets pressed mid way through the motion, it will  be ignored much like the difference between the Blink example sketch and the Blink Without Delay example sketch.

You could change the steppermotor.step(StepsRequired) line to just do one step, check the switch again, then do another step but that's going to tie up the arduino having to constantly check the switch and not doing anything else in your code. I would set it up to spin the motor which ever way you need then generate a hardware interrupt on the switch pin to stop the rotation that way you can do other stuff with your code too while the motor is rotating and whenever the switch is pressed no matter what it will stop the motor.
Thanks a lot for the reply. It definitely works better than it did before but I will show you my end goal and maybe you'll be able to help me. So I am trying to make a motorized camera slider with limit switches on either side so that the slider does not keep going which would break the belt and the motor. I want it to do something similar to this video https://www.youtube.com/watch?v=y-od0Gzj7P0&t=2s. I would like it to go to the end of the rail at the speed I choose until it hits the limit switch. Then I want it to stop and wait for a command to go the other way at a chosen speed.

MechEng87

No problem, glad you got it working somewhat. If you want help with the project maybe PM me? Not sure if the thread needs to go on since you got the general concept working? Always love to help out another maker. Kinda new to the forum stuff so if the thread should keep going, definitely let me know how i can help you out!

Go Up