# Homing Help with 28BYJ-48 Stepper Motor with ULN2003 driver

I’m new to Arduino and completely YouTube and Arduino Forum self-taught as of 2020 quarantine. I have some base level HTML and C coding skills from 20 yrs ago when I was in college but really feel like I’m starting over. I’ve learned a lot this summer and have pieced together this code that allows me to use a joystick to control a sliding door on a small train-set model building, but can’t figure out a homing algorithm.

Description: My model uses a standard joystick module and an Arduino Mega knock-off (Elegoo 2560 R3), with the 28BYJ-48 Stepper Motor (w/ ULN2003 driver). There are two limit switches in the standard “Common” to GRND and “Normally Open” to interrupt pin configuration, using an internal pull-up input mode. If the door is closed then pushing the joystick right opens the door opens at a steady speed until the joystick is released or it triggers the limit switch (and vise versa when pushing the joystick to the left). The limit switches work great and I’m almost done I just have one problem…

Problem: I need a homing function that will guarantee the door is in the closed position on start-up or after power outage or reset.

I can’t believe this is a tough problem to solve and I’m bummed I couldn’t figure it out. I also cannot believe I haven’t be able to find this answer for my specific stepper motor/driver anywhere on the interwebs. I’ve looked for months and am finally asking for help, ego be damned. Thanks in advance for your help.

``````//Model Door Diagnostics - 11/28/2020 - NOTES:Includes interrupts, working on homing

#include<LiquidCrystal.h>     //LCD Library
#include <Stepper.h>

#define STEPS 32
#define IN1 8
#define IN2 9
#define IN3 10
#define IN4 11

Stepper templeStepper(STEPS, IN1, IN3, IN2, IN4);

//LCD display
const int rs = 40, en = 41, d4 = 42, d5 = 43, d6 = 44, d7 = 45;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
const int lcdBacklight = 13;

String sketchName = "Sketch: Diagnostics-Joystick Stepper v3";

//joystick pins
const int joyXPin = A0;
const int joyYPin = A1;
const int joyButtonPin = 1;

//stepper limit switches
const int limitSwitch0Pin = 18; // closed limit
const int limitSwitch1Pin = 19; // open limit

//utilites
const int buzzerPin = 29;

// Variables...
int doorState;    //Is door open or closed
int joyXVal;
int joyYVal;
int joyButtonVal;

int dt = 5;                        // delay time
int mdt = 50;                      // delay time-medium

//long initial_homing = -1; // Used to Home Stepper at startup

void setup() {

Serial.begin(9600);
Serial.println(sketchName);          //Report Sketch version
lcd.begin(16, 2);                    //LCD screen has 16 columns, 2 rows
lcd.clear();

pinMode(lcdBacklight, OUTPUT);
pinMode(limitSwitch0Pin, INPUT_PULLUP); //home pin (door closed)
pinMode(limitSwitch1Pin, INPUT_PULLUP);

pinMode(joyXPin, INPUT);
pinMode(joyYPin, INPUT);
pinMode(joyButtonPin, INPUT_PULLUP);

pinMode(buzzerPin, OUTPUT);

attachInterrupt(digitalPinToInterrupt(limitSwitch0Pin), stopMotor0, FALLING);
attachInterrupt(digitalPinToInterrupt(limitSwitch1Pin), stopMotor1, FALLING);

//Program start-up beep
digitalWrite(buzzerPin, HIGH);
delay(dt);
digitalWrite(buzzerPin, LOW);
delay(mdt);
digitalWrite(buzzerPin, HIGH);
delay(dt);
digitalWrite(buzzerPin, LOW);

// TEMPLE DOOR HOMING

digitalWrite(lcdBacklight, HIGH);
lcd.display();                              //turn on display
lcd.clear();                                //clear screen
lcd.setCursor(0, 0);
lcd.print("!Resetting Door!");
lcd.setCursor(0, 1);
lcd.print("*****HOMING*****");

delay(2000);

//totally just making stuff up here... help please
templeStepper.setSpeed(100);
templeStepper.step(initial);                //Set rotation direction to CW
initial_homing--;
templeStepper.step(5000);
delay(dt);
}
initial_homing=1;

templeStepper.setSpeed(initial_homing);            //speed
templeStepper.step(5000);
initial_homing++;
delay(dt);
}
lcd.display();
lcd.clear();
lcd.setCursor(0,0);
lcd.print("   Door Reset   ");
lcd.setCursor(0,1);
lcd.print("HOMING COMPLETE!");

Serial.println("Homing Completed");
Serial.println("");
*/
doorState = 0; //door is closed, homing complete

}//End Setup

//VOID LOOP****************************************************************************************************************************

void loop() {

joyXVal = analogRead(joyXPin);  //If joystick is neutral turn off stepper motor
if ((joyXVal > 500) && (joyXVal < 523))
{
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
}
else
{
while ((joyXVal >= 523) && (doorState == 0)) //While joystick is pushed RIGHT
{
int speed_ = map(joyXVal, 523, 1023, 5, 700);
templeStepper.setSpeed(speed_);
templeStepper.step(-1);               //Set rotation direction to CCW
}
}
while ((joyXVal <= 500) && (doorState == 1)) //While joystick is pushed LEFT
{
int speed_ = map(joyXVal, 500, 0, 5, 1700);
templeStepper.setSpeed(speed_);
templeStepper.step(1);                //Set rotation direction to CW
}

Serial.print("Joystick x,y = (");
Serial.print(joyXVal);
Serial.print(" , ");
Serial.print(joyYVal);
Serial.println(")");

} //End Void Loop

void stopMotor0() {
doorState = 0; //door is closed
delay(dt);
Serial.println("Door is closed");
}

void stopMotor1() {
doorState = 1; //door is open
delay(dt);
Serial.println("Door is open");
}
``````

The usual way to home a stepper motor is to do it from setup() with code like this pseudo code

``````limitSwitchState = digitalRead(limitSwitchPin);
while (limitSwitchState == HIGH) { // assumes LOW when pressed
// code to move one step towards the switch