Need help with programming 3 stepper motors

Hi there,

For our school project, we’re trying to program 3 stepper motors (NEMA17 0.9 degrees type: 42BYGHM809) with Arduino. To manage the stepper motors, we use Easydrivers 4.4.
The motors will be integrated into a home build linear actuator with the use of lead-screw (see picture below).

We’re trying to create a loop (kind of a scene), in which all of the 3 motors are powered at the same time. For example, motor 1 will first rotate 200 rounds, stop and then turn 100 rounds in the opposite direction. At the same time, motor 2 will rotate 100 rounds and then 50 backwards. Motor 3 waits for motors 1 and 2 to finish and then will rotate 300 rounds.

We have searched the internet for several codes to get the motors rotating. However, we’re not clear on which code the best option is to adjust the speed, number of rotations, delays, et cetera. The code we have written right now is the following:

int dirpin1 = 6;
int steppin1 = 7;
int dirpin2 = 4;
int steppin2 = 5;
int dirpin3 = 2;
int steppin3 = 3;
int rounds = 3200;

void setup() 
{
    pinMode(dirpin1, OUTPUT);
    pinMode(steppin2, OUTPUT);
    pinMode(dirpin2, OUTPUT);
    pinMode(steppin2, OUTPUT);
    pinMode(dirpin3, OUTPUT);
    pinMode(steppin3, OUTPUT);
    
}
void loop()
{
    // change direction
    int i;
          digitalWrite(dirpin1, HIGH);
          digitalWrite(dirpin2, HIGH);
          digitalWrite(dirpin3, HIGH);

   // Motor 1: make 6 rounds
      for (i = 0; i < (rounds*6); i++) {
          digitalWrite(steppin1, HIGH);
          digitalWrite(steppin1, LOW);
          delayMicroseconds(90);
      }
        
   // Motor 2: make 3 rounds   
      for (i = 0; i < (rounds*3); i++) {
           digitalWrite(steppin2, HIGH);
           digitalWrite(steppin2, LOW);
           delayMicroseconds(210);
       }
       
	// Motor 3: make 1 round
       for (i = 0; i < (rounds*1); i++) {
           digitalWrite(steppin3, HIGH);
           digitalWrite(steppin3, LOW);
           delayMicroseconds(260);
       }
       
 // change direction 
          digitalWrite(dirpin1, LOW);
          digitalWrite(dirpin2, LOW);
          digitalWrite(dirpin3, LOW);
    
    // Motor 1: make 6 rounds
      for (i = 0; i < (rounds*6); i++) {
          
          digitalWrite(steppin1, LOW);
          digitalWrite(steppin1, HIGH);
          delayMicroseconds(90);
      }
        
   // motor 2: make 3 rounds  
      for (i = 0; i < (rounds*3); i++) {
           digitalWrite(steppin2, LOW);
           digitalWrite(steppin2, HIGH);
           delayMicroseconds(210);
       }
    // motor 3: make 1 round
       for (i = 0; i < (rounds*1); i++) {
           digitalWrite(steppin3, LOW);
           digitalWrite(steppin3, HIGH);
           delayMicroseconds(260);
       }
}

It would be great if you could have a thorough look at the code and also point us into the right direction concerning the right codes to make the adjustments.

Many thanks.

Kind regards

Hi, welcome to the forum.
That seems like a nice project.

Where does the power for the stepper motors come from ? You need to know how much power is used, because I think it will not work with only the usb cable to power it.

When you do this :

digitalWrite(steppin1, HIGH);
digitalWrite(steppin1, LOW);

Then it is not known how short the pulse is. I think a delay should be added in between.

Sparkfun points towards this example code : http://lusorobotica.com/index.php/topic,106.0.html
That shows there is a delay in between.

Doing many things at once can be done with millis().
A delay will halt the sketch, but with millis() it is possible to have a number of software-timers or sequences running at the same time. It won't be easy to do that for 3 stepper motors.
Start to learn using millis() with this: http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay

You could use a library. That requires extra time to understand the use of the library. But after that, the library does all the hard work for you. For example the AccelStepper : http://www.airspayce.com/mikem/arduino/AccelStepper/

Things to do:

  • You must know how much power is needed, and how to gonna supply that.
  • Make the steppers move one by one to test everything.
  • Use millis() to do many things at the same time or use a library.

However, we're not clear on which code the best option is to adjust the speed, number of rotations, delays, et cetera.

Speed is a function of the interval between steps and the amount of current supplied to the motor. The Arduino can only control the interval between steps. Typically, you either use a library that lets you set the speed, or you experiment to determine the interval that lets you accomplish the speed that you need, assuming that you are providing sufficient current to be able to reach that speed.

Only you know how many steps you need to take. Only you know how long you want to sit still after doing all the stepping.

This simple stepper code illustrates how to control a motor without using a library - so you can see how it works.

The second example (using millis() for timing) is the only one that is practical if you want to extend it to work 3 motors at the same time. The demo several things at a time is an extended example of the BWoD technique.

You may also find something useful in stepper motor basics.

If you need precise coordinated movement of the motors (for example motorA move 85 steps in the same time that motorB moves 833 steps) you will find that the normal stepper and accelstepper libraries are not really helpful - they are not designed for that.

...R