Running multiple stepper motors at once using Uno

Hey, I am currently working on a small 3 axis robot arm with 28BYJ_48 stepper motors and it runs but the motors run one by one rather than simultaneously as I wanted too.

So, I was wondering if it is possible to run 3 28BYJ_48 stepper motors off an Arduino Uno board and have them move simultaneously?

Here is my code, I can bet its not the cleanest or most efficient code (This is only my second year of teaching myself arduino). Please be kind :slight_smile:

#include <TinyStepper_28BYJ_48.h>
//Stepper One Pins 
const int Stepper1_IN1 = 2;
const int Stepper1_IN2 = 3;
const int Stepper1_IN3 = 4;
const int Stepper1_IN4 = 5;

//Stepper Two Pins 
const int Stepper2_IN1 = 6;
const int Stepper2_IN2 = 7;
const int Stepper2_IN3 = 8; 
const int Stepper2_IN4 = 9;

//Stepper Three Pins
const int Stepper3_IN1 = 10;
const int Stepper3_IN2 = 11;
const int Stepper3_IN3 = 12;
const int Stepper3_IN4 = 13;

TinyStepper_28BYJ_48 stepper1;
TinyStepper_28BYJ_48 stepper2;
TinyStepper_28BYJ_48 stepper3;


void setup() {
  // put your setup code here, to run once:
  stepper1.connectToPins(Stepper1_IN1, Stepper1_IN2, Stepper1_IN3, Stepper1_IN4);
  stepper2.connectToPins(Stepper2_IN1, Stepper2_IN2, Stepper2_IN3, Stepper2_IN4);
  stepper3.connectToPins(Stepper3_IN1, Stepper3_IN2, Stepper3_IN3, Stepper3_IN4);
  
  Serial.begin(9600); 
}

//Top motor max distance is 800
// middle motor max distance is >600

void loop() {
  prog_start();
  //
  //
  //
  //
  // Program to run goes here:
  // Distance is mesured in degrees
  s1(90);
  s2(90);
  s3(90);
  delay(500);
  s1(-90);
  s2(-90);
  s3(-90);
  // 
  //
  //
  //
  prog_end();
  
}

void dec1(){
  //Stepper One || Wrist
  //Max Rotation = 360
  stepper1.setSpeedInStepsPerSecond(256);
  stepper1.setAccelerationInStepsPerSecondPerSecond(512);
}

void dec2(){
  //Stepper Two || Elbow
  //Max Rotation = 270
  stepper2.setSpeedInStepsPerSecond(256);
  stepper2.setAccelerationInStepsPerSecondPerSecond(512);
}

void dec3(){
  //Stepper Three || Base
  //Max Rotation = 270
  stepper3.setSpeedInStepsPerSecond(256);
  stepper3.setAccelerationInStepsPerSecondPerSecond(512);
}

int s1(int pos1){
  if (pos1 > 360 || pos1 < -360){
    Serial.println("Value to large");
  }
  else{
    stepper1.moveToPositionInSteps(pos1 *5.5555555); 
    Serial.println(pos1);
  }
}


int s2(int pos2){
  if (pos2 > 150 || pos2 < -150){
    Serial.println("Value to large");
  }
  else{ 
    stepper2.moveToPositionInSteps(pos2 *5.5555555);
    Serial.println(pos2);
  }
}

int s3(int pos3){
  if (pos3 > 90 || pos3 < -90){
    Serial.println("Value to large");
  }
  else{
    stepper3.moveToPositionInSteps(pos3 *5.5555555);
    Serial.println(pos3);
  }
}

int prog_start(){
  dec1();
  dec2();
  dec3();
  stepper1.moveToPositionInSteps(0);
  stepper1.moveToPositionInSteps(0);  
  stepper3.moveToPositionInSteps(0);
  delay(1000);
  Serial.println("Start Programm: ");
  
  
}

int prog_end(){
  Serial.print("Proggram end");
  s3(0);
  s2(0);
  s1(0);
  delay(5000);
}

I am still working on a proper way to control it, any suggestions would be appreciated.

I’m not familiar with the TinyStepper library but I know the
AccelStepper library can be used to control multiple motors.

You have not said whether the movement needs to be closely co-ordinated like with a CNC machine, and if so you should use the MultiStepper version of the AccelStepper library. As you are using 28BYJ motors I suspect that they probably don’t need close coordination.

…R

Yeah, they don't need close coordination. I tried using the AccelStepper library but I ran into troubles so I decided to find a dedicated library for these motors.

If you can give me any suggestions about how to use the AccelStepper library it would help me a lot.

A_Nerd

A_Nerd:
If you can give me any suggestions about how to use the AccelStepper library it would help me a lot.

I don't have any suggestions better than the examples that come with the library.

The essence of getting 3 motors to run is to have these 3 lines as the last thing in loop()

myMotorA.run();
myMotorB.run();
myMotorC.run();

No doubt you will think of more meaningful names for your variables.

...R

It's open source. Look inside the library to see what it's doing...

//
// move to the given absolute position in steps, this function does not return until
// the move is complete
//  Enter:  absolutePositionToMoveToInSteps = signed absolute position to move to in  
//          units of steps
//
void TinyStepper_28BYJ_48::moveToPositionInSteps(long absolutePositionToMoveToInSteps)
{
  setupMoveInSteps(absolutePositionToMoveToInSteps);
  
  while(!processMovement())
    ;
}

See that last part? Obviously this function waits until the movement is complete before releasing control back to your program. The comments also say that, but read the code first.

The setupMoveInSteps() method is public, meaning you are allowed to call this yourself from your code. So do the setup on all the motors, then call the processMovement() method on each one in turn until they all arrive at their destinations.