How to reset a Stepper Motor to a Start Position?

I'm currently programming an Arduino program for my final school project, and I need help.

The task is to control two stepper motors. One of the two motors has two buttons, when the button is pressed, the motor goes one step to the right, when the other one goes to the left. The second motor has three buttons, the first two for the same thing, but when the third is pressed, the motor goes to an already fixed start position (reset button).
I use two bipolars with four connections as stepping motors (description as photo in German). I use Az-Delivery's A4988 driver module (one driver for each motor). As for the code itself, I already have the controls to turn the motor left and right, but I need help resetting the motor.

It would be a really big help if someone could help me with the programming.
P.S. I write everything in German and then translate it into English, so I'm sorry if the spelling isn't correct

//there is no Libary needed

int buttonResetpin = 10; //Taster Reset
int buttonLeftpin = 9; //Taster nach Links
int buttonRightpin = 8; //Taster nach Rechts

#define dirPin 2
#define stepPin 3
#define stepsPerRevolution 50 //wie viele Schritte Der Motor macht

void setup() {
  pinMode(buttonResetpin, INPUT_PULLUP);  
  pinMode(buttonLeftpin, INPUT_PULLUP);
  pinMode(buttonRightpin, INPUT_PULLUP);  
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
  // initialize the serial port:
  Serial.begin(9600);
}

void loop() {

if (digitalRead(buttonLeftpin) == LOW)//Taster für Linkssdreh
  {
    //Stell die Drehrichtung im Uhrzeigersinn
    digitalWrite(dirPin, HIGH);

    // Spin the stepper motor 1 revolution slowly:
    for (int i = 0; i < stepsPerRevolution; i++) {
     // These four lines result in 1 step:
      digitalWrite(stepPin, HIGH);
      delayMicroseconds(5000);
      digitalWrite(stepPin, LOW);
      delayMicroseconds(5000);
    }
  }

if (digitalRead(buttonRightpin) == LOW)//Taster für Rechtssdreh
  {
    // Stell die Drehrichtung gegen dem Uhrzeigersinn
    digitalWrite(dirPin, LOW);

    // Spin the stepper motor 1 revolution quickly:
    for (int i = 0; i < stepsPerRevolution; i++) {
     // These four lines result in 1 step:
      digitalWrite(stepPin, HIGH);
      delayMicroseconds(2000);
      digitalWrite(stepPin, LOW);
      delayMicroseconds(2000);
    }
  }
  
if (digitalRead(buttonResetpin) == LOW)//Taster für Reset
  {
    Serial.println("test");
  }
}

The usual way to set a stepper to the zero position is to use a switch (optical or electrical) that is activated by a pin attached to the motor shaft.

To zero, have the motor take a step, use a digitalRead() command to read whether the switch is open or closed, and repeat or stop.

Example:
Capture

can i also makit only digital. the stepper count his steps and then when i press reset button he goes back the steps

Yes, but when the power is turned off, the MCU forgets the motor shaft position, and the motor shaft can be turned easily.

Most people use a method for setting the zero position upon power up.

thats no problem, but please can you show me how to code this?

To keep track of position, add 1 to position for each step taken in the positive direction, and subtract 1 from position for each step taken in the negative direction.

For example:

   long position = 0;

    for (int i = 0; i < stepsPerRevolution; i++) {
     // These four lines result in 1 step:
      digitalWrite(stepPin, HIGH);
      delayMicroseconds(2000);
      digitalWrite(stepPin, LOW);
      delayMicroseconds(2000);
      position++;
    }

and wt code i have to type in thet the stepper goes the ammount back?

digitalWrite(stepPin, position--);

is this the solution

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