TMCStepper
TMCStepper copied to clipboard
TMC 5161 StallGuard on ESP32
I am running a 5161 on an ESP32, I am getting '0 1 292' and the motor is not moving, this is what i have wired
EN_PIN -> 14 MOSI ->MOSI SCK -> SCK CS -> SS (i have tryied Salve Select and a non-SS pin) MISO ->MISO (I assume CLK is not needed)
`#include <TMCStepper.h>
#define STALL_VALUE 50// [0... 255] #define TOFF_VALUE 4 // [1... 15]
#define EN_PIN 14 // Enable #define DIR_PIN 13 // Direction #define STEP_PIN 12 // Step #define CS_PIN SS // Chip select #define SW_MOSI 23 // Software Master Out Slave In (MOSI) #define SW_MISO 19 // Software Master In Slave Out (MISO) #define SW_SCK 18 // Software Slave Clock (SCK)
#define R_SENSE 0.075f // Match to your driver // SilentStepStick series use 0.11 // UltiMachine Einsy and Archim2 boards use 0.2 // Panucatt BSD2660 uses 0.1 // Watterott TMC5160 uses 0.075
TMC5161Stepper driver(CS_PIN, R_SENSE); //TMC5161Stepper driver(CS_PIN, R_SENSE, SW_MOSI, SW_MISO, SW_SCK);
int32_t speed = 1000;
void setup() {
Serial.begin(115200); // Init serial port and set baudrate
while(!Serial); // Wait for serial port to connect Serial.println("\nStart..."); SPI.begin(); // SPI drivers
pinMode(STEP_PIN, OUTPUT); pinMode(CS_PIN, OUTPUT); pinMode(DIR_PIN, OUTPUT); pinMode(MISO, INPUT_PULLUP);
driver.begin(); driver.toff(4); driver.setSPISpeed(speed); driver.blank_time(24); driver.rms_current(600); // mA driver.microsteps(16); driver.TCOOLTHRS(0xFFFFF); // 20bit max driver.THIGH(0); driver.semin(5); driver.semax(2); driver.sedn(0b01); driver.sgt(STALL_VALUE);
driver.en_pwm_mode(true); // Toggle stealthChop on TMC2130/2160/5130/5160 driver.pwm_autoscale(true); // Needed for stealthChop
Serial.print("\nTesting connection..."); uint8_t result = driver.test_connection();
if (result) { Serial.println("failed!"); Serial.print("Likely cause: ");
switch(result) {
case 1: Serial.println("loose connection"); break;
case 2: Serial.println("no power"); break;
}
Serial.println("Fix the problem and reset board.");
// We need this delay or messages above don't get fully printed out
delay(100);
abort();
} pinMode(EN_PIN, OUTPUT); digitalWrite(EN_PIN, LOW); }
bool shaft = false;
void loop() {
static uint32_t last_time = 0;
uint32_t ms = millis();
while (Serial.available() > 0) { int8_t read_byte = Serial.read();
if (read_byte == '0') {
Serial.print("Motor ");
if (driver.toff() == 0) {
Serial.print("already ");
}
Serial.println("disabled.");
driver.toff(0);
} else if (read_byte == '1') {
Serial.print("Motor ");
if (driver.toff() != 0) {
Serial.print("already ");
}
Serial.println("enabled.");
driver.toff(TOFF_VALUE);
} else if (read_byte == '+') {
speed += 100;
if (speed == 0) {
Serial.println("Hold motor.");
} else {
Serial.println("Increase speed.");
}
driver.setSPISpeed(speed);
} else if (read_byte == '-') {
speed -= 100;
if (speed == 0) {
Serial.println("Hold motor.");
} else {
Serial.println("Decrease speed.");
}
driver.setSPISpeed(speed);
}
}
if((ms-last_time) > 100 && driver.toff() != 0 && speed != 0) { // run every 0.1s last_time = ms;
Serial.print("Status: ");
Serial.print(driver.sg_result(), DEC);
Serial.print(" ");
Serial.print(driver.sg_result() < STALL_VALUE, DEC);
Serial.print(" ");
Serial.println(driver.cs2rms(driver.cs_actual()), DEC);
} }`
I am also trying to get stall guard to work with esp32 and tmc5160. Does anyone have a working code example?
same here, aqbot-oliver...