Motor Knob Trouble

Initial Position
When it starts up the Arduino has no means of knowing where the stepper motor is positioned - for example somebody might have moved it manually when the power was off.

The usual way to establish a datum for counting steps is with a limit switch. At startup the Arduino will move the motor until it triggers the switch. The Arduino will then regard that step position as step zero for the purpose of future position keeping.

I am not sure how to implement this. I am using a basic motor knob program. Trying to move the motor 45 deg fixed position. I not failing to determine its initial position. It should be 0 deg for 0 V and 45 deg for 5 V.

Can you tell me where am i going wrong.

#include <AccelStepper.h>
AccelStepper stepper; // Defaults to AccelStepper::FULL4WIRE (4 pins) on 2, 3, 4, 5
#define ANALOG_IN A1

void setup()

void loop()
  // Read new position
  int analog_in = analogRead(ANALOG_IN);

Please do not hijack. Thread split.

I asked the Moderator to move your Question as it is not relevant to my Stepper Motor Basics Tutorial.

Stepper motors do not have any way to report their position. The usual way to deal with that is to have a function that causes them to move one step at a time until they press a limit switch. The position the motor is at then is taken to be Zero.

Something like this pseudo code

void (moveToHome() {
    limitSwitchState = digitalRead(limitSwitchPin);
    while (limitSwitchState == HIGH) { // this assumes it will be LOW when pressed
       // move one step towards switch
       limitSwitchState = digitalRead(limitSwitchPin);
    stepperPosition = 0;