STEPPER MOTOR HOME POSITION

respected Sir,

Here i a code in which a stepper motor will
rotate 360 degree according to the potentiometer rotation, I
have also added 2 limit switches.

Sir, how to set home position of steppermotor, Sir what I want that
just after powering Arduino seepper motor should reverse and hit limit
switch 01, and that position hould be the origin point.

Sir please guide

Thanks

#include <Stepper.h>
int current_position = 0;
const char direction_pin = 2;
const char step_pin = 3;
const char pot_pin = 1; //analog input 1
#define Limit01 7  
#define Limit02 8   
void setup()
{
pinMode(Limit01, INPUT);
pinMode(Limit02, INPUT);
pinMode(direction_pin, OUTPUT);
pinMode(step_pin, OUTPUT);
pinMode(pot_pin, INPUT);
}

void loop()
{
int  readvalue = analogRead(pot_pin);
readvalue = map(readvalue,0,1023,0,1151);
if (readvalue > current_position+20)
{
 step_motor_forward();
 current_position += 1;
}
else if (readvalue < current_position-20)
{
 step_motor_back();
 current_position -= 1;
}
}//end of loop

void step_motor_forward()
{
if (digitalRead(Limit02)) {}  
    
else {
digitalWrite(direction_pin, LOW);
digitalWrite(step_pin, HIGH);
digitalWrite(step_pin, LOW);
}
}

void step_motor_back()
{
if (digitalRead(Limit01)) {} 
    
else {
digitalWrite(direction_pin, HIGH);
digitalWrite(step_pin, HIGH);
digitalWrite(step_pin, LOW);
}
}

In the setup function you should take one step and then check the end stop. Repeat this in a while loop until the end stop is triggered. This is then your zero position so set the current position variable accordingly.

Respected Sir,

In this project a stepper motor will follow the rotation of
a poterntiometer, 360 degree, I have also added 2 limit switches
the programe is working fine.
Homing- The idea is that as soon as powering arduino
the stepper motor should reverse and hit the limit switch and
that should be the origin point.
As directed by the Forum, I have added a While loop
in the setup, now what is happning assoon as powering
stepper motor does not move at all, but after hiting the limit
switch manualy it goes in to the main loop and works properly,
but homing is not achieved.

Sir please guide

Thank

#include <Stepper.h>
int current_position = 0;
const char direction_pin = 2;
const char step_pin = 3;
const char pot_pin = 1; //analog input 1
#define Limit01 7  
#define Limit02 8   
void setup()
{
pinMode(Limit01, INPUT);
pinMode(Limit02, INPUT);
pinMode(direction_pin, OUTPUT);
pinMode(step_pin, OUTPUT);
pinMode(pot_pin, INPUT);



while (!digitalRead(Limit01)) 
{
digitalWrite(direction_pin, HIGH);
digitalWrite(step_pin, HIGH);
digitalWrite(step_pin, LOW);
}



}

void loop()
{
int  readvalue = analogRead(pot_pin);
readvalue = map(readvalue,0,1023,0,1151);
if (readvalue > current_position+20)
{
 step_motor_forward();
 current_position += 1;
}
else if (readvalue < current_position-20)
{
 step_motor_back();
 current_position -= 1;
}
}//end of loop

void step_motor_forward()
{
if (digitalRead(Limit02)) {}  
    
else {
digitalWrite(direction_pin, LOW);
digitalWrite(step_pin, HIGH);
digitalWrite(step_pin, LOW);
}
}

void step_motor_back()
{
if (digitalRead(Limit01)) {} 
    
else {
digitalWrite(direction_pin, HIGH);
digitalWrite(step_pin, HIGH);
digitalWrite(step_pin, LOW);
}
}

Hi, How have you got your limits wired.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

while (!digitalRead(Limit01)) 
{
digitalWrite(direction_pin, HIGH);
digitalWrite(step_pin, HIGH);
digitalWrite(step_pin, LOW);
}

If you run this loop. how long does the stepper pulse stay high then low to let the stepper step?

Thanks.. Tom.. :)

MBose: As directed by the Forum

The forum never directs, merely guides or suggests. :)

I've suggested to moderators that this thread be merged with the other one to keep everything in one place.

Try putting a delay in that homing while loop. It looks like you are trying to step the motor too fast.

Sir, every thing including while loop is working as expected, except that motor is not moving in the while loop.

Sir, every thing including while loop is working as expected, except that motor is not moving in the while loop.

I have attached the switch circuit Image also.

Hi,

MBose: Sir, every thing including while loop is working as expected, except that motor is not moving in the while loop.

I have attached the switch circuit Image also.

Sorry the diagram didn't appear. The while loop as has been shown, is working, but because you do not PAUSE after going HIGH then PAUSE after going LOW, the stepper has not had a long enough pulse to make a step. Edit your while loop as shown below.

while (!digitalRead(Limit01)) 
{
digitalWrite(direction_pin, HIGH);
digitalWrite(step_pin, HIGH);
delay(100);
digitalWrite(step_pin, LOW);
delay(100);
}

Tom... :)

Sir, as per your advice , I have added a delay of 10 ms , it is working normal as expected, problem is solved.

Thanks a lot to all.

How come you didn’t believe me in reply#5?

Hi,

Grumpy_Mike: How come you didn't believe me in reply#5?

I did.... The identical reply's after it looked like we were talking to a spam robot. Tom....

Respected, All,
After guidance from forum, I did some changes in code for stepper motor homing, and worked perfectly fine with easy driver and arduino uno.

Now, I am at final stage of project, using Arduino NANO and Industrial stepper motor drive.

Here is the product link

Sir, every thing is working fine except that homing is not working ,earlier homing function worked with arduino uno and easy driver.Sir please guide.

SIR , SORRY ,I could not find the <> button.

code

#include <Stepper.h>
int current_position = 0;
const char direction_pin = 2;
const char step_pin = 3;
#define Limit01 7  
#define Limit02 8

void setup()
{
pinMode(Limit01, INPUT);
pinMode(Limit02, INPUT);
pinMode(direction_pin, OUTPUT);
pinMode(step_pin, OUTPUT);
Serial.begin(9600);

while (!digitalRead(Limit01)) 
{
digitalWrite(direction_pin, HIGH);
delay(100);
digitalWrite(step_pin, HIGH);
delay(100);
digitalWrite(step_pin, LOW);
delay(100);
}
}

void loop()
{
if ( Serial.available() > 0) { 
int readvalue = Serial.read();
readvalue = map(readvalue,0,255,0,1151);
if (readvalue > current_position+20)
{
step_motor_forward();
step_motor_forward();
step_motor_forward();
step_motor_forward();
current_position += 4;
}
else if (readvalue < current_position-20)
{
step_motor_back();
step_motor_back();
 step_motor_back();
step_motor_back();
current_position -= 4;
}
}
delay(2);
}//end of loop

void step_motor_forward()
{
if (digitalRead(Limit02)) {}  
   
else {
digitalWrite(direction_pin, LOW);
digitalWrite(step_pin, HIGH);
digitalWrite(step_pin, LOW);
}
}

void step_motor_back()
{
 if (digitalRead(Limit01)) {} 
   
else {
digitalWrite(direction_pin, HIGH);
digitalWrite(step_pin, HIGH);
digitalWrite(step_pin, LOW);
}
}

Well, now that you’ve made your post, you can go back and edit it to use the </> button. Instead of using Quick-edit, you need to use the full editor, which may be called “Modify” under the “More” button.

Also, please run the Arduino auto-format function (on the Tools menu) on your code to make it easier to read.

So, the function step_motor_back() works? Then use that to take steps towards the home position.

Why do you #include Stepper.h when you never use any functions from that library?