The use of CNC Shield is optional, it is used just for easy extension to y-axis and z-axis.
Connection
1. Stack CNC Shield on Arduino Uno or Mega2560
2. Connect CNC Shield to power supply with voltage 8-35V
3. Connect CNC X axis to 42BYGH4417 stepper motor
Photo
Connections
Dolly In Motion
Sketch// simple stepper motor control
// only x axis is used for dolly
#define EN 8 / / stepper motor enable
#define X_DIR 5/ / x axis direction control
#define Y_DIR 6/ / y axis direction control
#define Z_DIR 7/ / z axis direction control
#define X_STP 2/ / x axis step control
#define Y_STP 3/ / y axis step control
#define Z_STP 4/ / z axis step control
/*
// step(): to control direction and steps of stepper motor
// parameter: dir for direction control,
// dirPin maps to DIR pin of stepper motor,
// stepperPin maps to STEP pin of stepper motor
// return value: none
*/
void step(boolean dir, byte dirPin, byte stepperPin, int steps)
{
digitalWrite(dirPin, dir);
delay(50);
for (int i = 0; i < steps; i++) {
digitalWrite(stepperPin, HIGH);
delayMicroseconds(800);
digitalWrite(stepperPin, LOW);
delayMicroseconds(800);
}
}
void setup (){
// setup stepper motor I/O pin to output
pinMode(X_DIR, OUTPUT); pinMode(X_STP, OUTPUT);
pinMode(Y_DIR, OUTPUT); pinMode(Y_STP, OUTPUT);
pinMode(Z_DIR, OUTPUT); pinMode(Z_STP, OUTPUT);
pinMode(EN, OUTPUT);
digitalWrite(EN, LOW);
}
void loop (){
// 200 steps per turn
step(false, X_DIR, X_STP, 1800); // run 360 mm
step(false, Y_DIR, Y_STP, 200);
step(false, Z_DIR, Z_STP, 200);
delay(1000);
step(true, X_DIR, X_STP, 1800); // run 360 mm in reverse direction
step(true, Y_DIR, Y_STP, 200);
step(true, Z_DIR, Z_STP, 200);
delay(1000);
}
Thankyou for posting this code. I was trying to work out why my CNC-Shield v3.0 wasn't passing commands from GrblController to my motors (A4988 running NEMA11's with 12V Supply. This code works fine, therefore the problem I was having was the GRBL 0.9j running on the MEGA2560 Arduino.
ReplyDeleteSaved me many hours of hair pulling !!!
That's why posting the code.
ReplyDeletewhat is code for stopping stepperM
ReplyDeletedigitalWrite(stepperPin, LOW);
ReplyDeletethe code doesnt work by me. It shows the error: expected primary-expression befor / token. The error is in line 9.
ReplyDeleteThx
delete space character between "/" and "/", may be caused during copy and paste
ReplyDeletezhx ;D
Deleteand another question: my step motor is moving 3 rounds then stops. I think its the A4988 Driver is overheated because when i wait for a few seconds and i give agaun voltage then it moves again 3 rounds. How many Volts have you putted in the CNC shield?
ReplyDelete12 Volts.
ReplyDeleteHi, thank you !
ReplyDeleteI'm Rob, unfortunately I'm good with meccanic hardware but I do not know anything about arduino. do you think it is possible to reverse the direction by using two limit switches? I would like to make a game for my son, some targets that move right and then left.
Thx !!
Yes, it is possible. Reprap 3D printers using limit switches are built with Arduino Mega2560.
ReplyDeletethank you! please, have you an example of code? I unfortunately do not know anything about the programming of arduino.... sorry..
ReplyDeleteYou have to download Arduino IDE, compile the code above and upload to Arduino Board (eg. UNO) to run. It's simple and there are so many tutorials about Arduino all over the internet. Arduino boards and shields are cheap.
ReplyDeletenice, thanks you for the code
ReplyDeletehttps://papanbungabandung.net