Arduino and Two Stepper Motors

 

Two_Steppers

This project consist in moving two stepper motors “simultaneously”.
Really there is a loop where both motors are moved “one step at a time” to reach their final position.
5 Volts steppers with external power source are used.

 

Here is the video in YouTube:

Here is the Sketch:

—————-

/*------------------------------------------------------
Two_Steppers.ino     Arduining.com  30 MAY 2015

Driving two steppers concurrently with the Arduino Nano.
stepperX follows potentiometer on analog input 0.
stepperY follows potentiometer on analog input 1.
A timeout is used to turn off coils and save energy in battery
operated applications.

Hardware:
Arduino NANO.
Drivers: ULN2003A
Stepper Motors: 28BYJ48, 5VDC, step angle 5.625 °
                Gear reduction 64:1
                No-load starting frequency:> = 500PPS (4 rpm)
                Coil resistance 60 Ohms.               
 -----------------------------------------------------*/

#include <Stepper.h>

#define  STEPSREV    4096    // 64(fullsteps) * 64 (reduction ratio)
#define  SPEED       4       // motor speed (RPM)

#define  COIL_1X     11
#define  COIL_2X     9
#define  COIL_3X     10
#define  COIL_4X     8

#define  COIL_1Y     7
#define  COIL_2Y     5
#define  COIL_3Y     6
#define  COIL_4Y     4

#define  POT_X       0
#define  POT_Y       1                
#define  TIMEOUT     1000    //Turns off after 1 sec of inactivity.
#define  NOISE       8       //inmunity in steps because analog noisy readings.

// create the instances of the stepper class.
Stepper stepperX(STEPSREV, COIL_1X, COIL_2X, COIL_3X, COIL_4X);
Stepper stepperY(STEPSREV, COIL_1Y, COIL_2Y, COIL_3Y, COIL_4Y);

int potValX,potValY;           // potentiometers analog readings
int Xpos,Ypos;                 // Actual steppers positions(0-4096)->(0-360°)
int newXpos, newYpos;          // New steppers positions
unsigned long stampX, stampY;  // last move time stamped.

//============== functions ====================================================

//Read the potentiometers and map the reading to mach 360 degrees.
void readPots(){
  potValX = analogRead(POT_X);          // read POT_X value (0-1023).
  potValY = analogRead(POT_Y);          // read POT_Y value (0-1023).
  newXpos= map(potValX,0,1023,0,2047);  // Map pot X range to one stepper turn.
  newYpos= map(potValY,0,1023,0,2047);  // Map pot Y range to the stepper turn.
}
 
//-----------------------------------------------------------------------------
// Aproach stepperX to the newX position.
void aproachX(int newX){
  int Xdir = Xpos<newX ? 1 : -1;
  stepperX.step(Xdir);        // move one step in the Xdir direction.
  Xpos += Xdir;               // update Xpos.
  stampX = millis();          // stamp actual time.
  }

//-----------------------------------------------------------------------------
// Aproach stepperY to the newY position.
void aproachY(int newY){
  int Ydir = Ypos<newY ? 1 : -1;
  stepperY.step(Ydir);        // move one step in the Ydir direction.
  Ypos += Ydir;               // update Ypos.
  stampY = millis();          // stamp actual time.
  }

//-----------------------------------------------------------------------------
//Check for inactivity and turn off the steppers coils to save battery.
void CheckTimeout(){
  if((millis() - stampX) > TIMEOUT){   //Turn Off StepperX coils.
    digitalWrite(COIL_1X, LOW);
    digitalWrite(COIL_2X, LOW);
    digitalWrite(COIL_3X, LOW);
    digitalWrite(COIL_4X, LOW);
  }
  if((millis() - stampY) > TIMEOUT){   //Turn Off StepperY coils.
    digitalWrite(COIL_1Y, LOW);
    digitalWrite(COIL_2Y, LOW);
    digitalWrite(COIL_3Y, LOW);
    digitalWrite(COIL_4Y, LOW);
  }    
}


//=================== setup ===================================================
void setup(){
  stepperX.setSpeed(SPEED);  // set the X motor speed.
  stepperY.setSpeed(SPEED);  // set the Y motor speed.
  readPots();
  Xpos = newXpos;
  Ypos = newYpos;
}

//================= main loop =================================================
void loop(){
  readPots();
//if diference is greater than NOISE move steppers.
  if(abs(newXpos - Xpos)> NOISE) aproachX(newXpos);
  if(abs(newYpos - Ypos)> NOISE) aproachY(newYpos);

  CheckTimeout();   //check for inactivity.

}

 

—————-

 

 

 

 

 

Advertisements
  1. May 30, 2015 at 6:26 PM

    great job! tell me how to make motor run faster and without delay? which means #define SPEED 4 // motor speed (RPM) – is the maximum speed, or what? I’ve got motor for 200 steps, no gearbox – that it is necessary to change in the sketch that he moved one revolution, as in this video https://www.youtube.com/watch?v=Owuc7JKkh68? Thanks!!!

    • May 30, 2015 at 6:55 PM

      There is a compromise with this stepper, is strong but slow…

  2. May 31, 2015 at 12:58 AM

    how to connect to the board two-phase stepper motor?

  3. May de Leoz
    June 11, 2015 at 3:04 AM

    Is anyone selling this board as a finished product?

  4. sayf
    October 12, 2015 at 5:52 AM

    int Ydir = Ypos<newY ? 1 : -1; can u explane this step ?

  5. wrongway is what everyone has called me 40+ years
    January 17, 2016 at 8:07 PM

    this is fantastic just what I have bin looking for for days I am kind of new to all this and making a robot head to act like it is tell story’s while I read out loud to sick kids in the hospital and would like to know if you can help me add some more stepper motors to the sketch and maybe set different speeds to each stepper

    • January 17, 2016 at 11:28 PM

      The best approach to make multiple movements is using servos, you can move many of them simultaneously (will need a good power supply). Drivers are not needed. The position can be controlled without sensors or encoders.

  6. wrongway is what everyone has called me 40+ years
    January 17, 2016 at 8:32 PM

    sorry to be such a pest but I am so happy to find this code you wrote it has help me SO much but I can only get one stepper to go forward/revers with the pot one is a small stepper you get with the aruino kits you can also get them on ebay cheap and they come with there own driver that one wont change speed or direction but my nema 17 and a L298N driver works perfect and fast reaction to input and I am using 10k pots on a breadboard for now till all works good Thank you again for any help

  7. fernando soares
    March 11, 2016 at 5:47 PM

    Hi ! what the value of the pot?

  8. David Long
    June 18, 2016 at 5:07 PM

    The noise reduction part of this script is particularly amazing for the DSLR follow focus I am designing. Many thanks!

  9. andrebiedermann
    July 24, 2016 at 11:07 AM

    Great Code Example. I love that map function wich i didn’t know yet.
    I adapted the code to drive 4 steppers.
    In case somebody is happy with that.

    /*——————————————————
    Conrols 4 steppers simultaniously.
    It handles different numbers of steps for each stepper.
    The speed is adpated depending of the individual numbers of steps,
    so the speppers reach their goal position the same time.
    The code was testet on Teensy 3.1.
    But there is nothing extraordinary in the code so it should run on any arduino.
    —————————————————–*/

    #include
    #define STEPSREV 4096 // 64(fullsteps) * 64 (reduction ratio)
    #define SPEED 4 // motor speed (RPM)

    // create the instances of the stepper class.
    Stepper stepper1(STEPSREV, 8, 10, 9, 11);
    Stepper stepper2(STEPSREV, 7, 5, 6, 4); //originally 4 6 5 7 for other rotational direction
    Stepper stepper3(STEPSREV, 0, 2, 1, 3);
    Stepper stepper4(STEPSREV, 27, 25, 26, 24); //originally 24 26 25 27 for other rotational direction

    int SchritteZuFahren1; //Steps to go for stepper 1
    int SchritteZuFahren2; //Steps to go for stepper 2
    int SchritteZuFahren3; //Steps to go for stepper 3
    int SchritteZuFahren4; //Steps to go for stepper 4

    //*********************************************************************************************************

    void MotorAus() { //Turns off al motors to save energy
    digitalWrite( 4, 0); //Motor aus
    digitalWrite( 5, 0); //Motor aus
    digitalWrite( 6, 0); //Motor aus
    digitalWrite( 7, 0); //Motor aus
    digitalWrite( 8, 0); //Motor aus
    digitalWrite( 9, 0); //Motor aus
    digitalWrite(10, 0); //Motor aus
    digitalWrite(11, 0); //Motor aus
    digitalWrite( 0, 0); //Motor aus
    digitalWrite( 1, 0); //Motor aus
    digitalWrite( 2, 0); //Motor aus
    digitalWrite( 3, 0); //Motor aus
    digitalWrite(24, 0); //Motor aus
    digitalWrite(25, 0); //Motor aus
    digitalWrite(26, 0); //Motor aus
    digitalWrite(27, 0); //Motor aus
    }

    //*********************************************************************************************************

    void SchritteFahren(int SollSteps1, int SollSteps2, int SollSteps3, int SollSteps4) {
    int Pos1 = 0; //Current position for stepper 1
    int Pos2 = 0; //Current position for stepper 2
    int Pos3 = 0; //Current position for stepper 3
    int Pos4 = 0; //Current position for stepper 4
    int NewPos1 = 0; //Next position for stepper 1
    int NewPos2 = 0; //Next position for stepper 2
    int NewPos3 = 0; //Next position for stepper 3
    int NewPos4 = 0; //Next position for stepper 4
    int MaxSteps; //largest absolute number of steps to do

    MaxSteps = max(abs(SollSteps1), abs(SollSteps2)); //max-function takes 2 arguments only, so it takes three commands for 4 numbers
    MaxSteps = max(MaxSteps, abs(SollSteps3));
    MaxSteps = max(MaxSteps, abs(SollSteps4));

    for (int i=0; i <= MaxSteps; i++){ //for each step of MaxSteps
    NewPos1= map(i,0,MaxSteps,0,SollSteps1); //Next position for stepper 1
    NewPos2= map(i,0,MaxSteps,0,SollSteps2); //Next position for stepper 2
    NewPos3= map(i,0,MaxSteps,0,SollSteps3); //Next position for stepper 3
    NewPos4= map(i,0,MaxSteps,0,SollSteps4); //Next position for stepper 4
    stepper1.step(NewPos1 – Pos1); //Approach next position for stepper 1
    stepper2.step(NewPos2 – Pos2); //Approach next position for stepper 2
    stepper3.step(NewPos3 – Pos3); //Approach next position for stepper 3
    stepper4.step(NewPos4 – Pos4); //Approach next position for stepper 4
    Pos1 = NewPos1; //update position for stepper 1
    Pos2 = NewPos2; //update position for stepper 2
    Pos3 = NewPos3; //update position for stepper 3
    Pos4 = NewPos4; //update position for stepper 4
    }
    MotorAus();
    }

    //=================== setup ===================================================
    void setup(){
    stepper3.setSpeed(SPEED); // set the motor3 speed.
    stepper4.setSpeed(SPEED); // set the motor4 speed.
    }

    //================= main loop =================================================
    void loop(){

    //Test routine for different combinations of directions
    SchritteZuFahren1 = 1500;
    SchritteZuFahren2 = 1500;
    SchritteZuFahren3 = 2048;
    SchritteZuFahren4 = 1536;
    SchritteFahren (SchritteZuFahren1, SchritteZuFahren2, SchritteZuFahren3, SchritteZuFahren4);
    delay(1000);

    SchritteZuFahren1 =-1500;
    SchritteZuFahren2 =-1500;
    SchritteZuFahren3 =-2048;
    SchritteZuFahren4 = 1536;
    SchritteFahren (SchritteZuFahren1, SchritteZuFahren2, SchritteZuFahren3, SchritteZuFahren4);
    delay(1000);

    SchritteZuFahren1 = 1500;
    SchritteZuFahren2 = 1500;
    SchritteZuFahren3 = 2048;
    SchritteZuFahren4 =-1536;
    SchritteFahren (SchritteZuFahren1, SchritteZuFahren2, SchritteZuFahren3, SchritteZuFahren4);
    delay(1000);

    SchritteZuFahren1 =-1500;
    SchritteZuFahren2 =-1500;
    SchritteZuFahren3 =-2048;
    SchritteZuFahren4 =-1536;
    SchritteFahren (SchritteZuFahren1, SchritteZuFahren2, SchritteZuFahren3, SchritteZuFahren4);
    delay(1000);
    }

  10. andrebiedermann
    July 26, 2016 at 2:57 PM

    Correction:
    All four Steppers need to be set for the SPEED of course in the previous code.

    void setup(){
    stepper1.setSpeed(SPEED); // set the motor3 speed.
    stepper2.setSpeed(SPEED); // set the motor3 speed.
    stepper3.setSpeed(SPEED); // set the motor3 speed.
    stepper4.setSpeed(SPEED); // set the motor4 speed.
    }

  11. steve
    August 1, 2016 at 6:11 PM

    please explain how you have the pots wired up to input 0 and 1.
    thank you

  12. Matt
    September 10, 2016 at 1:30 PM

    Hello! Your sketch is perfect for the project I’m doing although it I run the stepper one revolution sketch from the library, it is able to spin much faster. With your sketch there seems to be a maximum SPEED value. How can that value increase?

    • September 10, 2016 at 7:59 PM

      If you try to go faster the steppers don’t move. With This steppers you have strength and resolution instead of speed

  13. Adam
    September 12, 2016 at 1:18 AM

    Hi there, awesome project, this has really help me with my project. Can you advise which parts of code to change to adjust the needle travel and the ohms input value?
    Thanks,

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: