Adafruit is open, safely ... and shipping all orders at this time! Read more!

Stepper synchronisation
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Stepper synchronisation

by Dr_Frankenstein on Sun Nov 10, 2019 5:25 pm

Hi I have just built a two link robotic arm for making x,y movements. At the shoulder there is a nema 17 stepper ( ... =nema%2017), and at the elbow there is a 28BYJ-48 stepper ( ... e-512-step). I am controlling them using an Adafruit V2 motor-shield kit, attached to an Arduino Mega 2560.
I am trying to get both motors to run simultaneously so that they both start and finish at the same time so that the arm can draw a straight line. I don't know if my code is wrong or given the difference in steps per revolution if its impossible. Using the following code they seem to start together, but finish independantly. Where am I going wrong?

Code: Select all | TOGGLE FULL SIZE
#include <Wire.h>
#include <AccelStepper.h>
#include <Adafruit_MotorShield.h>

Adafruit_MotorShield AFMS; // Default address, no jumpers

Adafruit_StepperMotor *myStepper1 = AFMS.getStepper(2038, 1);// 28BYJ-48
Adafruit_StepperMotor *myStepper2 = AFMS.getStepper(200, 2);// Nema 17

void forwardstep1() { 
  myStepper1->step(FORWARD, SINGLE);
void backwardstep1() { 
  myStepper1->step(BACKWARD, SINGLE);
// wrappers for the second motor!
void forwardstep2() { 
  myStepper2->step(FORWARD, MICROSTEP);
void backwardstep2() { 
  myStepper2->step(BACKWARD, MICROSTEP);

//  wrap the steppers in an AccelStepper object
AccelStepper stepper1(forwardstep1, backwardstep1);
AccelStepper stepper2(forwardstep2, backwardstep2);

// Used to establish starting position
long Travel1; // for stepper1
long Travel2; //for stepper2

void setup()

  AFMS.begin(); // Start the shield

  stepper1.moveTo(200); // number of steps for stepper1
  stepper2.moveTo(10); // number of steps for stepper2


void loop()
    Serial.print("stepper1.distanceToGo ");
    Serial.println(stepper1.distanceToGo());// Used to monitor stepper1 progress
    Serial.print("stepper2.distanceToGo ");
    Serial.println(stepper2.distanceToGo()); // Used to monitor stepper2 progress
    //delay(1000); // delay for monitoring purposes
    // return to starting position
    if (stepper1.distanceToGo() == 0)
    if (stepper2.distanceToGo() == 0)



Posts: 9
Joined: Sun Nov 10, 2019 5:07 pm

Re: Stepper synchronisation

by adafruit_support_bill on Sun Nov 10, 2019 5:48 pm

In AccelStepper, speed is expressed in steps per second. ... 6ec929c18f
Your code is telling both motors to move at 500 steps/second. But one of them has 20 times as many steps to go, so it will take longer.

Also note that your serial output will prevent the steppers from achieving a rate of 500 steps/second. At 9600 baud, it takes about 1 ms per character. It looks like you have about 50 ms worth of serial output there, so at most you will see about 20 steps/second.

You can reduce the verbosity of your output, or increase the baud-rate to reduce that delay.

Posts: 77363
Joined: Sat Feb 07, 2009 10:11 am

Please be positive and constructive with your questions and comments.