Arduino Code
#include <TMCStepper.h>
#include <SoftwareSerial.h> // Use software serial for the UART to TMC2209
# include <Streaming.h> // For debugging
#define EN_PIN 2 // Enable - PURPLE
#define DIR_PIN 3 // Direction - WHITE
#define STEP_PIN 4 // Step - ORANGE
#define SW_SCK 5 // Software Slave Clock (SCK) - BLUE
#define SW_TX 6 // SoftwareSerial receive pin - BROWN
#define SW_RX 7 // SoftwareSerial transmit pin - YELLOW
#define DRIVER_ADDRESS 0b00 // TMC2209 Driver address according to MS1 and MS2
#define R_SENSE 0.11f // SilentStepStick series use 0.11 ...and so does my fysetc TMC2209 (?)
#define POT1 A0 // Potentiometers to adjust speed and travel
#define POT2 A1
SoftwareSerial SoftSerial(SW_RX, SW_TX);
TMC2209Stepper TMC_Driver(&SoftSerial, R_SENSE, DRIVER_ADDRESS);
//== Setup ======================================================================================
void setup() {
Serial.begin(115200); // initialize hardware serial for debugging
SoftSerial.begin(115200); // initialize software serial for UART motor control
pinMode(EN_PIN, OUTPUT);
digitalWrite(EN_PIN, LOW); // Enable driver in hardware
pinMode(STEP_PIN, OUTPUT);
pinMode(DIR_PIN, OUTPUT);
TMC_Driver.beginSerial(115200); // Initialize UART
TMC_Driver.begin(); // UART: Init SW UART (if selected) with default 115200 baudrate
TMC_Driver.toff(5); // Enables driver in software
TMC_Driver.rms_current(1500); // Set motor RMS current
TMC_Driver.microsteps(64); // Set microsteps
TMC_Driver.en_spreadCycle(false); // Toggle spreadCycl
TMC_Driver.pwm_autoscale(true); // Needed for stealthChop
}
//== Loop ========================================================================================
void loop() {
int potVal = analogRead(A0); // Read potentiometer (0-1023)
long stepperSpeed;
if (potVal <= 500) // In lower half of range turn counter clockwise
{ // (direction depends on motor wiring?)
stepperSpeed = map(potVal, 0, 500, -50000, 0);
}
else if (potVal >= 530) // In high half of range turn clockwise
{
stepperSpeed = map(potVal, 520, 1023, 0, 50000);
}else // Create a "dead zone" between CW and CCW
{ // if 500 < potVal <520
stepperSpeed = 0;
}
Serial << potVal << " " << stepperSpeed << endl;
TMC_Driver.VACTUAL(stepperSpeed);
TMC_Driver.step();
} // end loop
Comments