To recreate it the same way we did, the following things are needed:
- A CNC Shield V4 β for Arduino Nano
- TMC2209 Motor Driver Module V4.0
- Arduino Nano
- A stepper motor
- A stepper motor extension cable
- An AC-DC adapter 12V
The first step is to connect everything. Place the arduino and the motor driver on the CNC shield. Connect the extension cable to the stepper motor and the CNC shield. Now you can connect the arduino to your laptop and plug the adapter in. Make sure when placing the motor driver, the adapter is not plugged in.
When everything is connected, see picture, the following code can be run.
#include <AccelStepper.h>
// Define the pins for your stepper motor
#define X_STEP_PIN 5
#define X_DIR_PIN 2
#define X_ENABLE_PIN 8
String fp_num = "focus_9";
uint64_t last_message_time = 0;
int PinIn = 13;
// Create an instance of AccelStepper with DRIVER mode (for step/direction control)
AccelStepper stepper(AccelStepper::DRIVER, X_STEP_PIN, X_DIR_PIN);
void setup() {
Serial.begin(115200);
Serial.setTimeout(10);
delay(1000);
Serial.println(fp_num);
stepper.setMaxSpeed(500); // Set the maximum speed (steps per second)
stepper.setAcceleration(5000); // Set the acceleration (steps per second^2)
pinMode(PinIn, INPUT);
}
void loop() {
if (Serial.available() > 0) {
// Read the number of steps from the serial input
long steps = Serial.parseInt();
last_message_time = millis();
// Check if steps value is valid (not 0)
if (steps != 0) {
// Move the stepper by the given number of steps
stepper.move(steps);
stepper.runToPosition(); // Block until the move is completed
// Send a "Done" message to the PC after the movement is completed
delay(1);
Serial.println("Done");
if (steps == 0){Serial.println("Done");}
}
}
if (millis() - last_message_time > 10 * 1000) {
// Serial.println("off");
digitalWrite(X_ENABLE_PIN, HIGH);
}
}
After the code is uploaded tot he arduino, in the serial monitor the amount of steps can be given. Positive input letβs the stepper motor rotate to the right and a negative input rotates it the other way around.