Stepper Motor Stop Switch using IR Speed Motion Sensor

Dear All,

I am new to both Arduino and Programming. I want to develop a stepper motor application which involves 360 degree forward and backward motion. I am using adafruit stepper motor driver for this. Till now I have managed to move the stepper motor in clockwise and anticlockwise direction.

I want stepper motor to start from specific point whenever the process (Arduino) starts. For this I tried to hook up a IR Speed Motion Sensor (got it from friend. It gives ttl output, when circuit breaks it shows value around 800 when circuit closes its around 45) but I am not able to use as a switch for stepper motor.

I can understand this might be a silly question and my sincere apologies for that. If someone can help me or suggest me some other alternative which can used as a switch for stepper motor.

Thank you

Following is the code.

#include <AFMotor.h>
AF_Stepper Stepper(200,2);
int posPin=A0;
int readValue;
int i=1;

void setup() {

Stepper.setSpeed(5);
pinMode(posPin,INPUT);
analogRead(posPin);
readValue = analogRead(posPin);

if(readValue>100)
{
readValue = analogRead(posPin);
Stepper.step(i,FORWARD,MICROSTEP);
i=i+1;
delay(100);
}
else
{
delay(100);
Stepper.release();
}

}

void loop() {

Stepper.step(200, BACKWARD, INTERLEAVE);
Stepper.step(200, FORWARD, MICROSTEP);

}

I want stepper motor to start from specific point whenever the process (Arduino) starts.

Do you mean you want to set the shaft of the stepper to a specific angle at start?

is there something moving connected to the motor (i would assume so)? typically you would use a limit switch and turn your motor in one direction until that switch gets activated by your mobile part hitting the switch. then you know where you are, at a reference point and you can count steps to the "home" position

A speed sensor, as its name implies, is for measuring speed not position

Thank you Mr. J.M.L. for reply.

Stepper motor is supposed to move a platform attached to it. And yes I want to set a home position for stepper motor.

If I can not use IR switch, Can you suggest alternative way to do it?

Regards
Manish

Look for limit switch onyour favorite store - they look somewhat like this with a contact switch that might be different based on the needs of your moving platform

Thank you,

I will try this.

Do you have an IR switch or some kind of speed sensor ? If it's a switch it can work.

Does your component look like a small wheel with holes and a circuit with a "U" shaped component with IR emitteron one side and receiver on the other?

If you have something with the U shape then as long as your motorized part can have something coming inside the U to block the IR then you can use it to detect position

Yes Mr. J.M.L.

It is IR opto slot switch. I tried to code it but the motor is not stopping when it crosses the slot. I tried with while, if and do while loop.

Thank you
Regards
Manish S Hiray

Do you power it correctly? are you reading from the right pin?

Yes Mr. J.M.L.,

I had powered it correctly. I tried to print numbers when there is obstacle in the switch (circuit break), it was working with all loops. I was reading the input from analog pin. Following is the code.

#include <AFMotor.h>
AF_Stepper Stepper(200,2);
int posPin=A0;
int readValue;
int i=0, j=0;

void setup() {

Stepper.setSpeed(5);
pinMode(posPin,INPUT);
analogRead(posPin);
Serial.begin(9600);
readValue = analogRead(posPin);
Serial.println(readValue);

if(readValue>100)
{
readValue = analogRead(posPin);
Serial.println(readValue);
Stepper.step(i,FORWARD,MICROSTEP);
i=i+1;
delay(100);
}
else
{
delay(100);
Stepper.release();
}

}

void loop() {

for (i=0;i<=200;)
{

Stepper.step(i, BACKWARD, INTERLEAVE);
i=i+2;

// Serial.println(readValue);
//delay(1000);
}
Stepper.release();
for (j=0;j<=200;)
{

Stepper.step(i, FORWARD, INTERLEAVE);
j=j+2;

Serial.println(readValue);
delay(1000);
}
Stepper.release();

}

Should I use digital Pin?

Thank you
Regards
Manish

Forget your current set up for a while.

Take your component, wire it (power and ground) correctly and attach the digital pin to D3 and the analog pin to A0 (if it offers both Output)

Write a very small piece of code where in setup() you open the serial connection, declare the pins as input and in the loop just read the values (so digitalRead and analogRead) and print them.

Now take a piece of cardboard and stick it and remove it from the middle of the U shaped element and see how the values change in the console.

Once you get this working andunderstand how the values change, then you can move to the next stage of integrating that with your motor