Programming a stepper motor to do specific operations

Hi Guys,

I am trying to work on a program that requires my motor to do specific operations:-

Step 1) rotate the shaft till it reaches a limit switch.
Step 2) after step one triggers the limit switch, I want the shaft to move certain no. of steps in the
opposite direction.
Step 3) After step 2 I want the shaft to oscillate CW and CCW for a specific no. of steps.
Step 4) after step 3 i want the stepper to stop oscillating for a certain amount of time.
Step 5) Lastly I want step 3 and 4 to be done a certain no. of times or in a loop.

The program that I have got so far is given below:-

#include <Stepper.h>

const int dirPin =8;
const int pulPin = 9;
const int limit1 = 2;                                                                             // limit switch

int stepsPerRev1 = 2740;
Stepper motor1 (stepsPerRev1,8,9);

void homing()                                                                                    // moving motor to find home sensor limit1
{       motor1.setSpeed(5000);  
        digitalWrite(dirPin, HIGH);                                                              // setup motor turn left 
        do 
        {
           digitalWrite(pulPin , HIGH);
            delayMicroseconds(500);
            digitalWrite(pulPin , LOW);
           delayMicroseconds(500);
        } 
        while (digitalRead(limit1) == HIGH);                                                    // motor turn left until touches limit 1
            delay(500);
            motor1.step(-4000);
             delay(1000);
}

void form()
{
         int i=10;
         do
          { motor1.setSpeed(5000);
            motor1.step(stepsPerRev1);
            delay(200);
            motor1.step(-stepsPerRev1);
            delay(200);
            i=i-1;
          }   
         while(i>0);
          
          delay(5000);
}

void setup()
{                                                                                              // Set up stepper motor pins 
       Serial.begin(9600);
       pinMode(dirPin, OUTPUT);
       pinMode(pulPin, OUTPUT);
       pinMode(limit1 , INPUT_PULLUP);                                                         //Set up the limit switch
      
       homing();                                                                               // call homing function;
}       




void loop()
{        
  for(int n=5;n>=5;n--)
  {  
            form();
  }       
}

So my questions are as follows:-

  1. I run into a problem of executing step 5. Though I have put it into a 'FOR' loop for running the form function 5 times, It does not stop at 5, it keeps running. Am I using the For loop wrong?

  2. Second question is just a doubt- Its about the 'homing' function- Though my limit switch 'limit1' is not pressed continuously, the state remains in 'HIGH' and my guess is, that is the reason for the while loop to run the statements in the do continuously- Is my assumption correct?
    FYI- I have used a 10K pull up resistor for my limit switch.

Thank you
Bijoy

Forget your code for right now. We can't see what you have or how you have it wired or how you are powering the whole project. How are you triggering the limit switch with the stepper motor rotation? Does that work if you trigger the limit switch by hand?

Hi, @bijoythomaschandy
I think we need a circuit diagram and possibly an image(s) of your project so we can see your component layout.
A hand drawn circuit will be fine, just make sure it includes powersupply as well.

Thanks.. Tom... :grinning: :+1: :coffee: :australia:

The 'for' cycle is executed only once but is repeated constantly due to the 'void loop' cycle.

Hi @Paul_KD7HB , @TomGeorge

My apologies, The diagram is as below:-

The setup is shown below :-

Currently, I have not placed the limit switch in the exact location it should be so I use my hand to trigger it to simulate the process.

Components used are :-

  1. Power supply from stepper online S-100-24
  2. mega 2560 board
  3. gear coupled motor from stepper online 16HS13-0604S-PG14
  4. stepper driver form stepper online DM320T
  5. simple limit switch
  6. 10K ohm resistor
  7. bread board

Thanks for the help.

Bijoy

Thanks Flashko, so how do I disable the motor after the 5 runs? should it be in the void setup() itself? Programming newbie question I guess.

Hi,
Thanks for the image, but how is the simple limit switch wired, COM/NO or COM/NC.

Sorry but a schematic would have been better rather than cut and paste.

Tom... :grinning: :+1: :coffee: :australia:

Hi @TomGeorge,

Hope this helps:-

image

Bijoy

That's one way to do it. You'll have to reset or cycle the power to run it again though.

You have set motor speed to 5000 RPM and steps per rev to 2740, that's 5000 * 2740 = 13700000 steps per minute or 13700000 / 60 = 228333 steps per second. Impossible without acceleration control and probably WITH. What is the gear ratio? What will the motor speed be if the output shaft is turning 5000 RPM.

sorry, maybe not a calculation that i am familiar with:-
gear ratio is 14:1.

The datasheet says about 13.73. Without acceleration and driving the gear train you might get 200 steps per second without missing steps, but that would only be about 4.4 RPM. How fast do you want the output shaft to turn?

i need the o/p shaft to turn atleast 60rpm

WAIT, WAIT! That driver WILL NOT work with the stepper.h library.
PHEW! You need a library like AccelStepper that works with step / direction drives, Sorry.

ok. Thats Interesting, Any recommendations on how I need to modify my program? The acceleration function in the accel stepper library maybe ?
But I am still trying to get my head around how the loop does not stop even after I have placed it in the void loop() function with a for loop OR placing it in the setup() function. @JCA34F @TomGeorge @wildbill.

See if this works:

#include <Stepper.h>

const int dirPin = 8;
const int pulPin = 9;
const int limit1 = 2;                                                                             // limit switch

int stepsPerRev1 = 2747;
Stepper motor1 (stepsPerRev1, 8, 9);

void homing()                                                                                    // moving motor to find home sensor limit1
{ motor1.setSpeed(60);
  digitalWrite(dirPin, HIGH);                                                              // setup motor turn left
  do
  {
    digitalWrite(pulPin , HIGH);
    delayMicroseconds(5000);
    digitalWrite(pulPin , LOW);
    delayMicroseconds(5000);
  }
  while (digitalRead(limit1) == HIGH);                                                    // motor turn left until touches limit 1
  delay(500);
  motor1.step(-4000);
  delay(1000);
}

void form()
{
  int i = 10;
  do
  { motor1.setSpeed(60);
    motor1.step(stepsPerRev1);
    delay(200);
    motor1.step(-stepsPerRev1);
    delay(200);
    i = i - 1;
  }
  while (i > 0);

  delay(5000);
}

void setup()
{ // Set up stepper motor pins
  Serial.begin(9600);
  pinMode(dirPin, OUTPUT);
  pinMode(pulPin, OUTPUT);
  pinMode(limit1 , INPUT_PULLUP);                                                         //Set up the limit switch

  homing();                                                                               // call homing function;
}
void loop()
{
  for (int n = 5; n > 0; n--)
  {
    form();
  }
while(1); // press RESET to repeat
}

Hello bijoythomaschandy
Generally spoken your sketch needs some pure basic functions like motor control, fwd, rev and stop. A time handler and may be a FSM take care about the reqiured steps 1 to 5 specified in a structured array. Thus you are able to add new steps without changing the method to handle the actions the stepper motor.
Have a nice day and enjoy coding in C++.

Ah! perfect! it works- Thank you @JCA34F .

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