Code for Returning two Stepper Motors to position 0.

Hi guys, I have very little coding knowledge so any help would be greatly appreciated. I’m using two 1.8 degree stepper motors (200 steps), with the Adafruit Motorshield V2 and the AccelStepper Library. I currently have one stepper completing a single revolution in an hour, and the other completing a single revolution in a minute.

My question is: How do I have each motor move back to their starting position once completing the revolution, but at a much higher speed, then start again with the hour/revolution and minute/revolution?

I’m stumped after looking through references and examples. I don’t understand the AccelStepper Library examples and jargon due to my lack of code understanding, so really not sure.

This is my current code, any suggestions or help would be greatly appreciated.

// Shows how to run two Steppers at once with thefirst completing one revolution in an hour, and the second one in a minute.
// Requires the Adafruit_Motorshield v2 library 
// And AccelStepper with AFMotor support 

// This is for Adafruit Motorshield v2 only!
// Will not work with v1 shields

#include <AccelStepper.h>
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"

Adafruit_MotorShield AFMStop(0x60); // Default address, no jumpers

// Connect two steppers with 200 steps per revolution (1.8 degree)
// to the top shield
Adafruit_StepperMotor *myStepper1 = AFMStop.getStepper(200, 1);
Adafruit_StepperMotor *myStepper2 = AFMStop.getStepper(200, 2);

// you can change these to DOUBLE or INTERLEAVE or MICROSTEP!
// wrappers for the first motor!
void forwardstep1() {  
  myStepper1->onestep(FORWARD, SINGLE);
void backwardstep1() {  
  myStepper1->onestep(BACKWARD, SINGLE);
// wrappers for the second motor!
void forwardstep2() {  
  myStepper2->onestep(FORWARD, DOUBLE);
void backwardstep2() {  
  myStepper2->onestep(BACKWARD, DOUBLE);

// Now we'll wrap the 3 steppers in an AccelStepper object
AccelStepper stepper1(forwardstep1, backwardstep1);
AccelStepper stepper2(forwardstep2, backwardstep2);

void setup()
  AFMStop.begin(); // Start the top shield


void loop()

Stepper motors don't have a mechanical zero. You can switch them on and start stepping without ever knowing what position they started in.

If you need a real zero position and you have continuous rotation, then you need a sensor to tell you when the output is in a defined position. This can be a simple switch or it can be an optical sensor. Then you just drive your motor in one direction or the other and stop when the switch s activated. Check the switch with digitalRead() on every step or maybe every 10 steps.

If this sensor is some distance away from the position you actually wanted, then you need to know how many steps (and what direction) to drive away from the sensor to get to the "real zero." It might be 1 or 2 steps, it might be 126 steps.

Ok thanks for that, makes sense. So if there is no mechanical zero, couldn't reversing the steps I just programmed it to take essentially take it back to the starting position? So if I had the stepper move 200 steps, how would I program it to then go back 200 steps, then restart the process? Seems the most logical way to approach this without a sensor. It doesn't need to be exact either, so a sensor wouldn't be necessary.

Yes, but what if the motors are bumped when you transport the thing while it's powered off? Do you manually set the shafts back to zero before powering on?

If you have a variable that keeps track of the accumulated number of steps (positive and negative) and IF the motor never misses a step you can always use the accumulated value to get back to where you started.

But, as @MorganS has said, there is no way to know the absolute position of the motor without an external switch to detect the Home or Zero position.

Stepper Motor Basics