So I've been making progress trying to learn how to connect the tmc2209 and run the motors.
I wrote a series of sketches which ran perfectly but didn't test any Stall Guard issues.
So now I'm looking to get the Stallguard working. So far, zero results.
I'm running a Mega using UART and tx2 and rx2.
I have a 10k pullup resistor on the diag pin.
the sketch just increases stall guard settings, but nothing works, diag just reads 1 no matter what the load is (I'm stalling it with my fingers).
Here's the ino code:
#include <TMCStepper.h>
#define EN_PIN 9 // Enable
#define DIR_PIN 7 // Direction
#define STEP_PIN 8 // Step
#define SERIAL_PORT Serial2 // Use hardware serial port 2
#define DRIVER_ADDRESS 0b00 // TMC2209 Driver address according to MS1 and MS2
#define DIAG_PIN 13 // DIAG pin on Mega
#define R_SENSE 0.11f
TMC2209Stepper driver(&SERIAL_PORT, R_SENSE, DRIVER_ADDRESS);
void setup() {
pinMode(EN_PIN, OUTPUT);
pinMode(DIR_PIN, OUTPUT);
pinMode(STEP_PIN, OUTPUT);
pinMode(DIAG_PIN, INPUT); // Set DIAG pin as input
digitalWrite(EN_PIN, LOW); // Enable driver in hardware
SERIAL_PORT.begin(9600); // Initialize hardware serial port at 9600 baud rate
driver.begin(); // Initialize the driver
driver.toff(5); // Enables driver in software
driver.rms_current(1200); // Set motor current to 1200mA
driver.microsteps(16); // Set microsteps to 1/16th
driver.en_spreadCycle(false); // Disable SpreadCycle
driver.TCOOLTHRS(0xFFFF); // Set TCOOLTHRS for StallGuard
driver.SGTHRS(0); // Initialize StallGuard threshold to 0
// Start Serial communication for debug
Serial.begin(9600); // Set Serial monitor to 9600 baud rate for debugging
Serial.println("Setup complete.");
Serial.print("Driver version: ");
Serial.println(driver.version(), HEX);
// Verify configuration
Serial.print("toff: ");
Serial.println(driver.toff(), DEC);
Serial.print("rms_current: ");
Serial.println(driver.rms_current(), DEC);
Serial.print("microsteps: ");
Serial.println(driver.microsteps(), DEC);
Serial.print("SGTHRS: ");
Serial.println(driver.SGTHRS(), DEC);
}
void loop() {
// Move the motor in one direction
digitalWrite(DIR_PIN, LOW);
for (uint16_t i = 0; i < 1000; i++) {
digitalWrite(STEP_PIN, HIGH);
delayMicroseconds(160);
digitalWrite(STEP_PIN, LOW);
delayMicroseconds(160);
}
// Read and print StallGuard status and other debug information
Serial.print("SG_RESULT: ");
Serial.println(driver.SG_RESULT(), DEC);
Serial.print("CS_ACTUAL: ");
Serial.println(driver.cs_actual(), DEC);
Serial.print("TSTEP: ");
Serial.println(driver.TSTEP(), DEC);
Serial.print("DIAG pin status: ");
Serial.println(digitalRead(DIAG_PIN));
// Increment SGTHRS value incrementally for tuning
static int sgt = 0;
if (sgt <= 255) {
driver.SGTHRS(sgt);
Serial.print("SGTHRS set to: ");
Serial.println(sgt);
sgt += 10; // Increment SGTHRS by 10 for quicker testing
}
delay(2000); // Add delay between loops
Serial.println("Loop iteration complete.");
}
Pin 13 had the built-in LED attached to it. I would use a different pin.
Have to tried the stallguard example that comes with the library?
That sketch uses interrupts to continuously rotate the motor while your sketch just rotates the motor in your for() loop and then reads that status. The status may reflect that the motor is currently stopped, not that it stalled out previously - I don't know.
I changed the pin, same story. You could be right about the motor, I'll have to think about it.
As far as their example; I ran it but got errors.
One was:
'driver' was not declared in this scope.
As I'm using their library and their example code, seems like something is wrong with their file...
So I got their example working, and I got a screen that read 0 0 75
Adjusting the stall guard value seemed to have no effect although the last number changed. But activating stall guard means that the motor will stop turning, and that never happened.
But just to be clear, Stallguard does not stop the motor, it just means that the driver indicates it detected a "stall condition". It is up to whatever is toggling the STEP pin to stop toggling the STEP pin, which stops the motor. Unless I seriously misunderstood the datasheet.
hi @geoffreybaker, were you successful?
I am in the same situation you were. I can control the motor but stall detection does not work.
Various scenarios but none detected. The one inspired from the library could show a 55mA but when I was trying to put more load or stall the motor that value never changed.
Tried with both variant:
TMC2209Stepper driver(&SERIAL_PORT, R_SENSE, DRIVER_ADDRESS); <connected to Arduino's RX,TX
TMC2209Stepper driver(SW_RX, SW_TX, R_SENSE, DRIVER_ADDRESS);
or with the DIAG pin, but that always outputs 0.
Yes, I was, for a couple of days. Then I made some changes to the hardware and it all stopped communicating again.
I'm working on a different project but will get back to this shortly and I'll be in touch once I get it working again...
Not yet unfortunately. Using the code or variations of it, didn’t rotate the motor.
I am away for few days, but once I get it working I will show the code.