Post Go back to editing

TMC2209 and Stallguard with ESP32

Category: Hardware
Product Number: tmc2209

I am trying to have my TMC2209 use its StallGuard2 technology to detect a stall (to be used for sensor-less homing later). I am using the TMC2209 on Adafruits breakout board: https://www.adafruit.com/product/6121
I am using an ESP32 as my microcontroller. It's the 'ESP-WROOM-32'

Despite trying both extremes (and values between) for 'SGTHRS' I constantly just receive the following on my serial monitor: 

Stall detected!
Stall Sensor Value: 0

What might I be missing please? I have spent two days troawling the internet, forums and friends for a solution and struggle to find one. Once this is resolved I intend to produce a Youtube video (similar to this one I made: https://youtu.be/1E4TDZC-xaY?si=vd3TwDqG9MbG7Nwa)  documenting its use for everyone else who want's to use this feature to simplify wiring in their project designs and make the most of this brilliant driver IC.

Any questions, please ask.

They are wired up as follows:

ESP32 TMC2209 (Breakout)
---------- ----------------------
3.3V ----------------> VDD
GND ----------------> GND

GPIO 27 ----------------> EN (Enable)

GPIO 25 ----------------> DIR (Direction)

GPIO 26 ----------------> STEP (Step pulse)

GPIO 16 (RX) ---------> Via 1k restore to sam wire as TX
GPIO 17 (TX) ---------> PDN_UART (UART communication)

GPIO 14 ---------------> DIAG (Stall/Diagnostic output) - connected with an additional 4.7kohm resistor connected to the 3.3v power supply of the ESP32

This is my code:

#include <TMCStepper.h>

// -----------------------------
// Define ESP32 Pin Connections
// -----------------------------
#define EN_PIN 27 // Enable pin (active LOW)
#define DIR_PIN 25 // Direction pin
#define STEP_PIN 26 // Step pulse pin
#define DIAG_PIN 14 // DIAG pin (for stall detection)

// -----------------------------
// Define UART Pins for TMC2209
// -----------------------------
#define UART_RX_PIN 16 // ESP32 RX for TMC2209
#define UART_TX_PIN 17 // ESP32 TX for TMC2209

// -----------------------------
// TMC2209 Settings
// -----------------------------
#define R_SENSE 0.11f // Sense resistor value (ohms), adjust per your circuit

// Create a TMC2209Stepper instance with an extra address parameter (often 0)
TMC2209Stepper driver(&Serial2, R_SENSE, 0);

void setup() {
// Initialize serial for debugging
Serial.begin(115200);
while (!Serial) { /* Wait for Serial monitor */ }

// Configure motor control pins
pinMode(EN_PIN, OUTPUT);
digitalWrite(EN_PIN, LOW); // Enable the driver (active LOW)

pinMode(DIR_PIN, OUTPUT);
pinMode(STEP_PIN, OUTPUT);

// Set DIAG pin with an internal pull-up so that a LOW indicates a stall
pinMode(DIAG_PIN, INPUT_PULLUP);
// Initialize Serial2 for TMC2209 communication (UART 115200, 8N1)
Serial2.begin(115200, SERIAL_8N1, UART_RX_PIN, UART_TX_PIN);
// -----------------------------
// Initialize the TMC2209 Driver
// -----------------------------
driver.begin(); // Start communication with TMC2209
driver.toff(5); // Set toff value (controls chopper timing)
driver.rms_current(600); // Set RMS current to 600 mA (adjust as needed)
driver.microsteps(16); // Configure microstepping (1/16 steps)
// Configure STALLGUARD sensitivity threshold
// Lower SGTHRS values increase sensitivity; adjust according to your load
driver.SGTHRS(200);

Serial.println("TMC2209 initialized with STALLGUARD enabled.");
}

void loop() {
// Generate a simple step pulse
digitalWrite(DIR_PIN, HIGH); // Set direction (adjust as needed)
digitalWrite(STEP_PIN, HIGH);
delayMicroseconds(1000);
digitalWrite(STEP_PIN, LOW);
delayMicroseconds(1000);
// Monitor the DIAG pin for a stall condition
if (digitalRead(DIAG_PIN) == LOW) {
Serial.println("Stall detected!");
// Add additional actions here if a stall is detected (e.g., stop or reverse the motor)
}
// Read DRV_STATUS and extract the stall sensor value (bits 16-23)
uint32_t drvStatus = driver.DRV_STATUS();
int stallSensorValue = (drvStatus >> 16) & 0xFF;
Serial.print("Stall Sensor Value: ");
Serial.println(stallSensorValue);

// Small delay for stabilization
delay(10);
}