Components and supplies
Arduino Mega 2560
NEMA 17 Stepper Motor
TB6600 Stepper Motor Driver
Project description
Code
TB6600
arduino
test code for CNC Single Axis 4A TB6600 Stepper Motor Driver Controller
1// test code for 2// CNC Single Axis 4A TB6600 Stepper Motor Driver Controller 3// use Serial Monitor to control 115200 baud 4 5 6word X; 7word NX; 8int MoveSpeed=600; //step in Microseconds 9String inputString = "help\ 10"; // a string to hold incoming data 11boolean stringComplete = true; // whether the string is completet 12boolean ComData = false; // whether com data is on when motors are moving will slow them down 13 14# define X_ENgnd 2 //ENA-(ENA) stepper motor enable , active low Gray 15# define X_EN_5v 3 //ENA+(+5V) stepper motor enable , active low Orange 16# define X_DIRgnd 4 //DIR-(DIR) axis stepper motor direction control Blue 17# define X_DIR_5v 5 //DIR+(+5v) axis stepper motor direction control Brown 18# define X_STPgnd 6 //PUL-(PUL) axis stepper motor step control Black 19# define X_STP_5v 7 //PUL+(+5v) axis stepper motor step control RED 20 21 22void setup() {// ************************************************************* setup 23pinMode (X_ENgnd ,OUTPUT); //ENA-(ENA) 24pinMode (X_EN_5v ,OUTPUT); //ENA+(+5V) 25pinMode (X_DIRgnd,OUTPUT); //DIR-(DIR) 26pinMode (X_DIR_5v,OUTPUT); //DIR+(+5v) 27pinMode (X_STPgnd,OUTPUT); //PUL-(PUL) 28pinMode (X_STP_5v,OUTPUT); //PUL+(+5v) 29pinMode (13,OUTPUT); 30digitalWrite (X_ENgnd, LOW); //ENA-(ENA) 31digitalWrite (X_EN_5v, HIGH); //ENA+(+5V) low=enabled 32digitalWrite (X_DIRgnd, LOW); //DIR-(DIR) 33digitalWrite (X_DIR_5v, LOW); //DIR+(+5v) 34digitalWrite (X_STPgnd, LOW); //PUL-(PUL) 35digitalWrite (X_STP_5v, LOW); //PUL+(+5v) 36 37Serial.begin(115200); 38} 39 40void serialEvent()// ******************************************************** Serial in 41{ while (Serial.available()) 42 { 43 char inChar = (char)Serial.read(); // get the new byte: 44 if (inChar > 0) {inputString += inChar;} // add it to the inputString: 45 if (inChar == '\n') { stringComplete = true;} // if the incoming character is a newline, set a flag so the main loop can do something about it: 46 } 47} 48void Help(){ // ************************************************************** Help 49 Serial.println("Commands step by step guide"); 50 Serial.println("Type hello -sends TB6600 Tester Ready "); 51 Serial.println("Type xon -turns TB6600 on"); 52 Serial.println("Type x+Number(0-60000) eg x1904 -to set next move steps"); 53 Serial.println("Type mx -to make motor move to next postion"); 54 Serial.println("Type cdon -turns on postion data when moving will increase time of step"); 55 Serial.println("Type x0"); 56 Serial.println("Type mx"); 57 Serial.println("Type s+Number(0-2000) eg s500 -to set Microseconds betwean steps"); 58 Serial.println("Type s2000"); 59 Serial.println("Type x300"); 60 Serial.println("Type mx"); 61 Serial.println("Type xoff -turns TB6600 off"); 62 Serial.println("Type cdoff -turns off postion data when moving"); 63 64 65 66 inputString=""; 67} 68void Hello(){ // ************************************************************** Hello 69 Serial.println("TB6600 Tester Ready"); 70 inputString=""; 71} 72void ENAXON(){ // ************************************************************* ENAXON 73 Serial.println("ENAXON"); 74 digitalWrite (X_EN_5v, LOW);//ENA+(+5V) low=enabled 75 inputString=""; 76} 77void ENAXOFF(){ // *********************************************************** ENAXOFF 78 Serial.println("ENAXOFF"); 79 digitalWrite (X_EN_5v, HIGH);//ENA+(+5V) low=enabled 80 inputString=""; 81} 82void MSpeed(){ // ************************************************************ MoveSpeed 83 inputString.setCharAt(0,' '); 84 MoveSpeed=inputString.toInt(); 85 Serial.print("Speed="); 86 Serial.println(MoveSpeed); 87 inputString=""; 88} 89void ComDataON(){ // ********************************************************* ComDataON 90 ComData=true; 91 Serial.println("ComDataOn"); 92 inputString=""; 93} 94void ComDataOFF(){ // ******************************************************** ComDataOFF 95 ComData=false; 96 Serial.println("ComDataOFF"); 97 inputString=""; 98} 99void NextX(){ // ************************************************************** NextX 100 inputString.setCharAt(0,' '); 101 NX=inputString.toInt(); 102 Serial.print("NX="); 103 Serial.println(NX); 104 inputString=""; 105} 106void MoveX(){ // ************************************************************** Move 107int xt; 108if (NX>X) 109{xt=NX-X; digitalWrite (X_DIR_5v,LOW);xt=1;} 110else 111{xt=X-NX; digitalWrite (X_DIR_5v,HIGH);xt=-1;} 112if (ComData==true) 113{for (; X !=NX; X=X+xt) 114{ digitalWrite (X_STP_5v, HIGH); 115 Serial.print("X="); 116 delayMicroseconds (MoveSpeed); 117 digitalWrite (X_STP_5v, LOW); 118 delayMicroseconds (MoveSpeed); 119 Serial.println(X+100000); 120}} 121else 122{for (; X !=NX; X=X+xt) 123{ digitalWrite (X_STP_5v, HIGH); 124 delayMicroseconds (MoveSpeed); 125 digitalWrite (X_STP_5v, LOW); 126 delayMicroseconds (MoveSpeed); 127}} 128Serial.print("X="); 129Serial.println(X); 130//X=NX; 131 132inputString=""; 133} 134void loop() // ************************************************************** loop 135{ 136 serialEvent(); 137 if (stringComplete) 138 { 139 if (inputString=="help\ 140") {Help();} 141 if (inputString=="hello\ 142") {Hello();} 143 if (inputString=="xon\ 144") {ENAXON();} 145 if (inputString=="xoff\ 146") {ENAXOFF();} 147 if (inputString=="cdon\ 148") {ComDataON();} 149 if (inputString=="cdoff\ 150") {ComDataOFF();} 151 if (inputString=="mx\ 152") {MoveX();} 153 if (inputString.charAt(0)=='s') {MSpeed();} 154 if (inputString.charAt(0)=='x') {NextX();} 155 156 if (inputString !="") {Serial.println("BAD COMMAND="+inputString);}// Serial.print("\ 157"); }// "\ " tab 158 inputString = ""; stringComplete = false; // clear the string: 159 } 160 161}
TB6600
arduino
test code for CNC Single Axis 4A TB6600 Stepper Motor Driver Controller
1// test code for 2// CNC Single Axis 4A TB6600 Stepper Motor Driver 3 Controller 4// use Serial Monitor to control 115200 baud 5 6 7word X; 8word 9 NX; 10int MoveSpeed=600; //step in Microseconds 11String inputString = "help\ 12"; 13 // a string to hold incoming data 14boolean stringComplete = true; // whether 15 the string is completet 16boolean ComData = false; // whether com data 17 is on when motors are moving will slow them down 18 19# define X_ENgnd 2 //ENA-(ENA) 20 stepper motor enable , active low Gray 21# define X_EN_5v 3 //ENA+(+5V) stepper 22 motor enable , active low Orange 23# define X_DIRgnd 4 //DIR-(DIR) axis stepper 24 motor direction control Blue 25# define X_DIR_5v 5 //DIR+(+5v) axis stepper motor 26 direction control Brown 27# define X_STPgnd 6 //PUL-(PUL) axis stepper motor step 28 control Black 29# define X_STP_5v 7 //PUL+(+5v) axis stepper motor step control 30 RED 31 32 33void setup() {// ************************************************************* 34 setup 35pinMode (X_ENgnd ,OUTPUT); //ENA-(ENA) 36pinMode (X_EN_5v ,OUTPUT); 37 //ENA+(+5V) 38pinMode (X_DIRgnd,OUTPUT); //DIR-(DIR) 39pinMode (X_DIR_5v,OUTPUT); 40 //DIR+(+5v) 41pinMode (X_STPgnd,OUTPUT); //PUL-(PUL) 42pinMode (X_STP_5v,OUTPUT); 43 //PUL+(+5v) 44pinMode (13,OUTPUT); 45digitalWrite (X_ENgnd, LOW); //ENA-(ENA) 46digitalWrite 47 (X_EN_5v, HIGH); //ENA+(+5V) low=enabled 48digitalWrite (X_DIRgnd, LOW); //DIR-(DIR) 49digitalWrite 50 (X_DIR_5v, LOW); //DIR+(+5v) 51digitalWrite (X_STPgnd, LOW); //PUL-(PUL) 52digitalWrite 53 (X_STP_5v, LOW); //PUL+(+5v) 54 55Serial.begin(115200); 56} 57 58void serialEvent()// 59 ******************************************************** Serial in 60{ while 61 (Serial.available()) 62 { 63 char inChar = (char)Serial.read(); // 64 get the new byte: 65 if (inChar > 0) {inputString += inChar;} // add it 66 to the inputString: 67 if (inChar == '\ 68') { stringComplete = true;} // if 69 the incoming character is a newline, set a flag so the main loop can do something 70 about it: 71 } 72} 73void Help(){ // ************************************************************** 74 Help 75 Serial.println("Commands step by step guide"); 76 Serial.println("Type 77 hello -sends TB6600 Tester Ready "); 78 Serial.println("Type xon -turns TB6600 79 on"); 80 Serial.println("Type x+Number(0-60000) eg x1904 -to set next move steps"); 81 82 Serial.println("Type mx -to make motor move to next postion"); 83 Serial.println("Type 84 cdon -turns on postion data when moving will increase time of step"); 85 Serial.println("Type 86 x0"); 87 Serial.println("Type mx"); 88 Serial.println("Type s+Number(0-2000) 89 eg s500 -to set Microseconds betwean steps"); 90 Serial.println("Type s2000"); 91 92 Serial.println("Type x300"); 93 Serial.println("Type mx"); 94 Serial.println("Type 95 xoff -turns TB6600 off"); 96 Serial.println("Type cdoff -turns off postion data 97 when moving"); 98 99 100 101 inputString=""; 102} 103void Hello(){ // ************************************************************** 104 Hello 105 Serial.println("TB6600 Tester Ready"); 106 inputString=""; 107} 108void 109 ENAXON(){ // ************************************************************* ENAXON 110 111 Serial.println("ENAXON"); 112 digitalWrite (X_EN_5v, LOW);//ENA+(+5V) low=enabled 113 114 inputString=""; 115} 116void ENAXOFF(){ // *********************************************************** 117 ENAXOFF 118 Serial.println("ENAXOFF"); 119 digitalWrite (X_EN_5v, HIGH);//ENA+(+5V) 120 low=enabled 121 inputString=""; 122} 123void MSpeed(){ // ************************************************************ 124 MoveSpeed 125 inputString.setCharAt(0,' '); 126 MoveSpeed=inputString.toInt(); 127 128 Serial.print("Speed="); 129 Serial.println(MoveSpeed); 130 inputString=""; 131} 132void 133 ComDataON(){ // ********************************************************* ComDataON 134 135 ComData=true; 136 Serial.println("ComDataOn"); 137 inputString=""; 138} 139void 140 ComDataOFF(){ // ******************************************************** ComDataOFF 141 142 ComData=false; 143 Serial.println("ComDataOFF"); 144 inputString=""; 145} 146void 147 NextX(){ // ************************************************************** NextX 148 149 inputString.setCharAt(0,' '); 150 NX=inputString.toInt(); 151 Serial.print("NX="); 152 153 Serial.println(NX); 154 inputString=""; 155} 156void MoveX(){ // ************************************************************** 157 Move 158int xt; 159if (NX>X) 160{xt=NX-X; digitalWrite (X_DIR_5v,LOW);xt=1;} 161else 162{xt=X-NX; 163 digitalWrite (X_DIR_5v,HIGH);xt=-1;} 164if (ComData==true) 165{for (; X !=NX; X=X+xt) 166{ 167 digitalWrite (X_STP_5v, HIGH); 168 Serial.print("X="); 169 delayMicroseconds 170 (MoveSpeed); 171 digitalWrite (X_STP_5v, LOW); 172 delayMicroseconds (MoveSpeed); 173 174 Serial.println(X+100000); 175}} 176else 177{for (; X !=NX; X=X+xt) 178{ digitalWrite 179 (X_STP_5v, HIGH); 180 delayMicroseconds (MoveSpeed); 181 digitalWrite (X_STP_5v, 182 LOW); 183 delayMicroseconds (MoveSpeed); 184}} 185Serial.print("X="); 186Serial.println(X); 187//X=NX; 188 189inputString=""; 190} 191void 192 loop() // ************************************************************** loop 193{ 194 195 serialEvent(); 196 if (stringComplete) 197 { 198 if (inputString=="help\ 199") 200 {Help();} 201 if (inputString=="hello\ 202") {Hello();} 203 if (inputString=="xon\ 204") 205 {ENAXON();} 206 if (inputString=="xoff\ 207") {ENAXOFF();} 208 if 209 (inputString=="cdon\ 210") {ComDataON();} 211 if (inputString=="cdoff\ 212") 213 {ComDataOFF();} 214 if (inputString=="mx\ 215") {MoveX();} 216 if 217 (inputString.charAt(0)=='s') {MSpeed();} 218 if (inputString.charAt(0)=='x') 219 {NextX();} 220 221 if (inputString !="") {Serial.println("BAD COMMAND="+inputString);}// 222 Serial.print("\ 223"); }// "\ " tab 224 inputString = ""; stringComplete 225 = false; // clear the string: 226 } 227 228}
Downloadable files
wire up like this
wire up like this
Comments
Only logged in users can leave comments
ashleyblack
0 Followers
•0 Projects
50
0
TB6600 Stepper Motor driver Tester | Arduino Project Hub