- Thu May 13, 2010 12:15 pm
#100880
how can I wire 3 EasyDriver Stepper Motor Driver and 3 Stepper Motor with Cable to an arduino mega ?
Where electronics enthusiasts find answers.
int DIRX = 3; // PIN 3 = DIRX
int STEPX = 2; // PIN 2 = STEPX
int MS1X = 13; // PIN 13 = MS1X
int MS2X = 9; // PIN 9 = MS2X
int SLEEPX = 12; // PIN 12 = SLPX
int DIRY = 4; // PIN 4 = DIRY
int STEPY = 5; // PIN 5 = STEPY
int MS1Y= 6; // PIN 6 = MS1Y
int MS2Y = 8; // PIN 8 = MS2X
int SLEEPY = 7; // PIN 7 = SLPY
void setup() {
Serial.begin(9600); // open the serial connection at 9600bps
pinMode(DIRX, OUTPUT); // set pin 3 to output
pinMode(STEPX, OUTPUT); // set pin 2 to output
pinMode(MS1X, OUTPUT); // set pin 13 to output
pinMode(MS2X, OUTPUT); // set pin 9 to output
pinMode(SLEEPX, OUTPUT); // set pin 12 to output
pinMode(DIRY, OUTPUT);
pinMode(STEPY, OUTPUT);
pinMode(MS1Y, OUTPUT);
pinMode(MS2Y, OUTPUT);
pinMode(SLEEPY, OUTPUT);
}
void loop()
{
int modeType = 1; // This number increases by multiple of 2 each through the while loop..
// ..to identify our STEPX mode type.
while (modeType<=8){ // loops the following block of code 4 times before repeating .
digitalWrite(DIRX, LOW); // Set the DIRXection change LOW to HIGH to go in opposite DIRXection
digitalWrite(MS1X, MS1X_MODE(modeType)); // Set state of MS1X based on the returned value from the MS1X_MODE() switch statement.
digitalWrite(MS2X, MS2X_MODE(modeType)); // Set state of MS2X based on the returned value from the MS2X_MODE() switch statement.
digitalWrite(SLEEPX, HIGH); // Set the SLEEPX mode to AWAKE.
digitalWrite(DIRY, LOW);
digitalWrite(MS1Y, MS1Y_MODE(modeType));
digitalWrite(MS2Y, MS2Y_MODE(modeType));
digitalWrite(SLEEPY, HIGH);
int i = 0; // Set the counter variable.
while(i<(modeType*200)) // Iterate for 200, then 400, then 800, then 1600 STEPXs.
// Then reset to 200 and start again.
{
digitalWrite(STEPX, LOW); // This LOW to HIGH change is what creates the..
digitalWrite(STEPX, HIGH); // .."Rising Edge" so the easydriver knows to when to STEPX.
digitalWrite(STEPY, LOW);
digitalWrite(STEPY, HIGH);
delayMicroseconds(1600/modeType); // This delay time determines the speed of the STEPXper motor.
// Delay shortens from 1600 to 800 to 400 to 200 then resets
i++;
}
modeType = modeType * 2; // Multiply the current modeType value by 2 and make the result the new value for modeType.
// This will make the modeType variable count 1,2,4,8 each time we pass though the while loop.
delay(500);
}
digitalWrite(SLEEPX, LOW); // switch off the power to STEPXper
Serial.print("SLEEPXING..");
delay(1000);
Serial.print("z");
delay(1000);
Serial.print("z");
delay(1000);
Serial.print("z");
delay(1000);
Serial.println("");
digitalWrite(SLEEPX, HIGH);
Serial.println("X AWAKE!!!"); // Switch on the power to STEPXper
delay(1000);
digitalWrite(SLEEPY, LOW);
Serial.print("SLEEPYING..");
delay(1000);
Serial.print("z");
delay(1000);
Serial.print("z");
delay(1000);
Serial.print("z");
delay(1000);
Serial.println("");
digitalWrite(SLEEPY, HIGH);
Serial.println(" Y AWAKE!!!");
delay(1000);
}
int MS1X_MODE(int MS1X_STEPXMode){ // A function that returns a High or Low state number for MS1X pin
switch(MS1X_STEPXMode){ // Switch statement for changing the MS1X pin state
// Different input states allowed are 1,2,4 or 8
case 1:
MS1X_STEPXMode = 0;
Serial.println("STEPX Mode is Full...");
break;
case 2:
MS1X_STEPXMode = 1;
Serial.println("STEPX Mode is Half...");
break;
case 4:
MS1X_STEPXMode = 0;
Serial.println("STEPX Mode is Quarter...");
break;
case 8:
MS1X_STEPXMode = 1;
Serial.println("STEPX Mode is Eighth...");
break;
}
return MS1X_STEPXMode;
}
int MS1Y_MODE(int MS1Y_STEPYMode){
switch(MS1X_STEPYMode){
// Different input states allowed are 1,2,4 or 8
case 1:
MS1Y_STEPYMode = 0;
Serial.println("STEPY Mode is Full...");
break;
case 2:
MS1X_STEPYMode = 1;
Serial.println("STEPY Mode is Half...");
break;
case 4:
MS1X_STEPYMode = 0;
Serial.println("STEPY Mode is Quarter...");
break;
case 8:
MS1X_STEPYMode = 1;
Serial.println("STEPY Mode is Eighth...");
break;
}
return MS1Y_STEPYMode;
}
int MS2X_MODE(int MS2X_STEPXMode){ // A function that returns a High or Low state number for MS2X pin
switch(MS2X_STEPXMode){ // Switch statement for changing the MS2X pin state
// Different input states allowed are 1,2,4 or 8
case 1:
MS2X_STEPXMode = 0;
break;
case 2:
MS2X_STEPXMode = 0;
break;
case 4:
MS2X_STEPXMode = 1;
break;
case 8:
MS2X_STEPXMode = 1;
break;
}
return MS2X_STEPXMode;
}
int MS2X_MODE(int MS2Y_STEPYMode){
switch(MS2Y_STEPYMode){
// Different input states allowed are 1,2,4 or 8
case 1:
MS2Y_STEPYMode = 0;
break;
case 2:
MS2Y_STEPYMode = 0;
break;
case 4:
MS2Y_STEPYMode = 1;
break;
case 8:
MS2Y_STEPYMode = 1;
break;
}
return MS2Y_STEPYMode;
}
///////////////////////////////////////////////////////////
// Stepper Motor skecth for use with the EasyDriver v4.2 //
///////////////////////////////////////////////////////////
// Dan Thompson 2010
//
// Use this code at your own risk.
//
// For all the product details visit http://www.schmalzhaus.com/EasyDriver/
// For the full tutorial visit http://danthompsonsblog.blogspot.com/
////// ED_v4 Step Mode Chart //////
// //
// MS1X MS2X Resolution //
// L L Full step (2 phase) //
// H L Half step //
// L H Quarter step //
// H H Eighth step //
// //
////////////////////////////////////
//X axis
int DIRX = 22; // PIN 3 = DIRX
int STEPX = 24; // PIN 2 = STEPX
int MS1X = 26; // PIN 13 = MS1X
int MS2X = 28; // PIN 9 = MS2X
int SLEEPX = 30; // PIN 12 = SLPX
//Y axis
int DIRY = 32; // PIN 5 = DIRY
int STEPY = 34; // PIN 4 = STEPY
int MS1Y = 36; // PIN 6 = MS1Y
int MS2Y = 38; // PIN 7 = MS2Y
int SLEEPY = 40; // PIN 8 = SLPY
//Z axis
int DIRZ = 42; // PIN 5 = DIRY
int STEPZ = 44; // PIN 4 = STEPY
int MS1Z = 46; // PIN 6 = MS1Y
int MS2Z = 48; // PIN 7 = MS2Y
int SLEEPZ = 50; // PIN 8 = SLPY
void setup() {
Serial.begin(9600); // open the serial connection at 9600bps
pinMode(DIRX, OUTPUT); // set pin 3 to output
pinMode(STEPX, OUTPUT); // set pin 2 to output
pinMode(MS1X, OUTPUT); // set pin 13 to output
pinMode(MS2X, OUTPUT); // set pin 9 to output
pinMode(SLEEPX, OUTPUT); // set pin 12 to output
pinMode(DIRY, OUTPUT); // set pin 5 to output
pinMode(STEPY, OUTPUT); // set pin 4 to output
pinMode(MS1Y, OUTPUT); // set pin 6 to output
pinMode(MS2Y, OUTPUT); // set pin 7 to output
pinMode(SLEEPY, OUTPUT); // set pin 8 to output
pinMode(DIRZ, OUTPUT); // set pin 5 to output
pinMode(STEPZ, OUTPUT); // set pin 4 to output
pinMode(MS1Z, OUTPUT); // set pin 6 to output
pinMode(MS2Z, OUTPUT); // set pin 7 to output
pinMode(SLEEPZ, OUTPUT); // set pin 8 to output
}
void loop()
{
int modeType = 1; // This number increases by multiple of 2 each through the while loop..
// ..to identify our step mode type.
while (modeType<=8){ // loops the following block of code 4 times before repeating .
digitalWrite(DIRX, HIGH); // Set the direction change LOW to HIGH to go in opposite direction
digitalWrite(MS1X, MS1X_MODE(modeType)); // Set state of MS1X based on the returned value from the MS1X_MODE() switch statement.
digitalWrite(MS2X, MS2X_MODE(modeType)); // Set state of MS2X based on the returned value from the MS2X_MODE() switch statement.
digitalWrite(SLEEPX, LOW); // Set the Sleep mode to AWAKE.
digitalWrite(DIRY, HIGH);
digitalWrite(MS1Y, MS1Y_MODE(modeType));
digitalWrite(MS2Y, MS2Y_MODE(modeType));
digitalWrite(SLEEPX, LOW);
digitalWrite(DIRZ, HIGH);
digitalWrite(MS1Z, MS1Z_MODE(modeType));
digitalWrite(MS2Z, MS2Z_MODE(modeType));
digitalWrite(SLEEPZ, LOW);
int i = 0; // Set the counter variable.
while(i<(modeType*200)) // Iterate for 200, then 400, then 800, then 1600 steps.
// Then reset to 200 and start again.
{
digitalWrite(STEPX, LOW); // This LOW to HIGH change is what creates the..
digitalWrite(STEPX, HIGH); // .."Rising Edge" so the easydriver knows to when to step.
digitalWrite(STEPY, LOW);
digitalWrite(STEPY, HIGH);
digitalWrite(STEPZ, LOW);
digitalWrite(STEPZ, HIGH);
delayMicroseconds(1600/modeType); // This delay time determines the speed of the stepper motor.
// Delay shortens from 1600 to 800 to 400 to 200 then resets
i++;
}
modeType = modeType * 2; // Multiply the current modeType value by 2 and make the result the new value for modeType.
// This will make the modeType variable count 1,2,4,8 each time we pass though the while loop.
delay(500);
}
digitalWrite(SLEEPX, LOW); // switch off the power to stepper
Serial.print("X SLEEPING..");
delay(1000);
Serial.print("X z");
delay(1000);
Serial.print("X z");
delay(1000);
Serial.print("X z");
delay(1000);
Serial.println("");
digitalWrite(SLEEPX, HIGH);
Serial.println("X AWAKE!!!"); // Switch on the power to stepper
delay(1000);
digitalWrite(SLEEPY, LOW); // switch off the power to stepper
Serial.print("Y SLEEPING..");
delay(1000);
Serial.print("Y z");
delay(1000);
Serial.print("Y z");
delay(1000);
Serial.print("Y z");
delay(1000);
Serial.println("");
digitalWrite(SLEEPY, HIGH);
Serial.println("Y AWAKE!!!"); // Switch on the power to stepper
delay(1000);
digitalWrite(SLEEPZ, LOW); // switch off the power to stepper
Serial.print("Z SLEEPING..");
delay(1000);
Serial.print("Z z");
delay(1000);
Serial.print("Z z");
delay(1000);
Serial.print("Z z");
delay(1000);
Serial.println("");
digitalWrite(SLEEPZ, HIGH);
Serial.println("Z AWAKE!!!"); // Switch on the power to stepper
delay(1000);
}
int MS1X_MODE(int MS1X_StepMode){ // A function that returns a High or Low state number for MS1X pin
switch(MS1X_StepMode){ // Switch statement for changing the MS1X pin state
// Different input states allowed are 1,2,4 or 8
case 1:
MS1X_StepMode = 0;
Serial.println("X Step Mode is Full...");
break;
case 2:
MS1X_StepMode = 1;
Serial.println("X Step Mode is Half...");
break;
case 4:
MS1X_StepMode = 0;
Serial.println("X Step Mode is Quarter...");
break;
case 8:
MS1X_StepMode = 1;
Serial.println("X Step Mode is Eighth...");
break;
}
return MS1X_StepMode;
}
int MS1Y_MODE(int MS1Y_StepMode){ // A function that returns a High or Low state number for MS1X pin
switch(MS1Y_StepMode){ // Switch statement for changing the MS1X pin state
// Different input states allowed are 1,2,4 or 8
case 1:
MS1Y_StepMode = 0;
Serial.println("Y Step Mode is Full...");
break;
case 2:
MS1Y_StepMode = 1;
Serial.println("Y Step Mode is Half...");
break;
case 4:
MS1Y_StepMode = 0;
Serial.println("Y Step Mode is Quarter...");
break;
case 8:
MS1Y_StepMode = 1;
Serial.println("Y Step Mode is Eighth...");
break;
}
return MS1Y_StepMode;
}
int MS1Z_MODE(int MS1Z_StepMode){ // A function that returns a High or Low state number for MS1X pin
switch(MS1Z_StepMode){ // Switch statement for changing the MS1X pin state
// Different input states allowed are 1,2,4 or 8
case 1:
MS1Z_StepMode = 0;
Serial.println("Z Step Mode is Full...");
break;
case 2:
MS1Z_StepMode = 1;
Serial.println("Z Step Mode is Half...");
break;
case 4:
MS1Z_StepMode = 0;
Serial.println("Z Step Mode is Quarter...");
break;
case 8:
MS1Z_StepMode = 1;
Serial.println("Z Step Mode is Eighth...");
break;
}
return MS1Z_StepMode;
}
int MS2X_MODE(int MS2X_StepMode){ // A function that returns a High or Low state number for MS2X pin
switch(MS2X_StepMode){ // Switch statement for changing the MS2X pin state
// Different input states allowed are 1,2,4 or 8
case 1:
MS2X_StepMode = 0;
break;
case 2:
MS2X_StepMode = 0;
break;
case 4:
MS2X_StepMode = 1;
break;
case 8:
MS2X_StepMode = 1;
break;
}
return MS2X_StepMode;
}
int MS2Y_MODE(int MS2Y_StepMode){ // A function that returns a High or Low state number for MS2X pin
switch(MS2Y_StepMode){ // Switch statement for changing the MS2X pin state
// Different input states allowed are 1,2,4 or 8
case 1:
MS2Y_StepMode = 0;
break;
case 2:
MS2Y_StepMode = 0;
break;
case 4:
MS2Y_StepMode = 1;
break;
case 8:
MS2Y_StepMode = 1;
break;
}
return MS2Y_StepMode;
}
int MS2Z_MODE(int MS2Z_StepMode){ // A function that returns a High or Low state number for MS2X pin
switch(MS2Z_StepMode){ // Switch statement for changing the MS2X pin state
// Different input states allowed are 1,2,4 or 8
case 1:
MS2Z_StepMode = 0;
break;
case 2:
MS2Z_StepMode = 0;
break;
case 4:
MS2Z_StepMode = 1;
break;
case 8:
MS2Z_StepMode = 1;
break;
}
return MS2Z_StepMode;
}
// for duemilanove atmega328 arduino board + easydriver stepper controller
// dan@marginallyclever.com 2010-06-15
#define SENSOR_X_PIN 1
#define SENSOR_Y_PIN 2
#define SENSOR_Z_PIN 3
#define DIR1_PIN 4
#define STEP1_PIN 5
#define DIR2_PIN 6
#define STEP2_PIN 7
#define DIR3_PIN 8
#define STEP3_PIN 9
#define DELAY 150
void setup() {
Serial.begin(9600);
pinMode(SENSOR_X_PIN,INPUT);
pinMode(SENSOR_Y_PIN,INPUT);
pinMode(SENSOR_Z_PIN,INPUT);
pinMode(DIR1_PIN,OUTPUT);
pinMode(STEP1_PIN,OUTPUT);
pinMode(DIR2_PIN,OUTPUT);
pinMode(STEP2_PIN,OUTPUT);
pinMode(DIR3_PIN,OUTPUT);
pinMode(STEP3_PIN,OUTPUT);
}
void loop() {
int i;
digitalWrite(DIR1_PIN, LOW); // Set the direction.
digitalWrite(DIR2_PIN, LOW); // Set the direction.
digitalWrite(DIR3_PIN, LOW); // Set the direction.
delay(DELAY);
Serial.println(">>");
for (i = 0; i<6800; i++) // Iterate for 4000 microsteps.
{
digitalWrite(STEP1_PIN, LOW); // This LOW to HIGH change is what creates the
digitalWrite(STEP1_PIN, HIGH); // "Rising Edge" so the easydriver knows to when to step.
if((i%2)==0) {
digitalWrite(STEP2_PIN, LOW); // This LOW to HIGH change is what creates the
digitalWrite(STEP2_PIN, HIGH); // "Rising Edge" so the easydriver knows to when to step.
}
if((i%4)==0) {
digitalWrite(STEP3_PIN, LOW); // This LOW to HIGH change is what creates the
digitalWrite(STEP3_PIN, HIGH); // "Rising Edge" so the easydriver knows to when to step.
}
delayMicroseconds(DELAY); // This delay time is close to top speed for this
} // particular motor. Any faster the motor stalls.
digitalWrite(DIR1_PIN, HIGH); // Change direction.
digitalWrite(DIR2_PIN, HIGH); // Change direction.
digitalWrite(DIR3_PIN, HIGH); // Change direction.
delay(DELAY);
Serial.println("<<");
for (i = 0; i<6800; i++) // Iterate for 4000 microsteps
{
digitalWrite(STEP1_PIN, LOW); // This LOW to HIGH change is what creates the
digitalWrite(STEP1_PIN, HIGH); // "Rising Edge" so the easydriver knows to when to step.
if((i%2)==0) {
digitalWrite(STEP2_PIN, LOW); // This LOW to HIGH change is what creates the
digitalWrite(STEP2_PIN, HIGH); // "Rising Edge" so the easydriver knows to when to step.
}
if((i%4)==0) {
digitalWrite(STEP3_PIN, LOW); // This LOW to HIGH change is what creates the
digitalWrite(STEP3_PIN, HIGH); // "Rising Edge" so the easydriver knows to when to step.
}
delayMicroseconds(DELAY); // This delay time is close to top speed for this
} // particular motor. Any faster the motor stalls.
}