UART in ESP32 With TMC2209

Hello,
I was trying to make a stepper motor run using on ESP32 board with TMC2209 driver and with UART Communication. Here's The Code That I'm Uploading :

#include <TMCStepper.h>

#define DIR_PIN_2          2          // Direction
#define STEP_PIN_2         4          // Step
#define SERIAL_PORT_2      Serial2    // TMC2208/TMC2224 HardwareSerial port
#define DRIVER_ADDRESS_2   0b00       // TMC2209 Driver address according to MS1 and MS2
#define R_SENSE_2          0.11f      // E_SENSE for current calc.  

hw_timer_t * timer1 = NULL;
TMC2209Stepper driver2(&SERIAL_PORT_2, R_SENSE_2 , DRIVER_ADDRESS_2 );

void IRAM_ATTR onTimer() 
{
  digitalWrite(STEP_PIN_2, !digitalRead(STEP_PIN_2));
} 

void setup() 
{
  Serial.begin(250000);         // Init serial port and set baudrate
  while(!Serial);               // Wait for serial port to connect
  Serial.println("\nStart...");
  SERIAL_PORT_2.begin(115200);

  pinMode(STEP_PIN_2 ,OUTPUT);
  pinMode(DIR_PIN_2 ,OUTPUT);

  digitalWrite(DIR_PIN_2 ,LOW);

  driver2.begin();
  driver2.toff(4);
  driver2.blank_time(24);
  driver2.rms_current(10); 
  driver2.microsteps(16);
  driver2.en_spreadCycle(false);
  driver2.intpol(true);

  activate_interrupt();
}

void loop() {
 static uint32_t last_time=0;
 uint32_t ms = millis();
 if((ms-last_time) > 100) { //run every 0.1s
    last_time = ms;

    Serial.print("0 ");
    Serial.print(driver2.SG_RESULT(), DEC);
    Serial.print(" ");
    Serial.println(driver2.cs2rms(driver2.cs_actual()), DEC);
  }
}

void activate_interrupt(){
  {
    cli();//stop interrupts
    timer1 = timerBegin(3, 8,true); // Initialize timer 4. Se configura el timer,  ESP(0,1,2,3)
                                 // prescaler of 8, y true es una bandera que indica si la interrupcion se realiza en borde o en nivel
    timerAttachInterrupt(timer1, &onTimer, true); //link interrupt with function onTimer
    timerAlarmWrite(timer1, 8000, true); //En esta funcion se define el valor del contador en el cual se genera la interrupción del timer
    timerAlarmEnable(timer1);    //Enable timer        
    sei();//allow interrupts
  }
}

This Code Complies and Gets Uploaded but the Current is not setting at all. Is there any problem with the sketch ? If Anyone knows than please reply. I appreciate even a little bit of help.

The Serial2 TX is sending data and the step pin is sending pulses (tested in Wokwi simulation).
Perhaps something is wrong with the wiring or with the commands.

Is there a command to get the status via the Serial2 port that you can call in setup() ?

Hello,
I was using TMC2209 driver on ESP32 with UART. Here's The Code That I'm Compiling.

#include <TMCStepper.h>

// #define EN_PIN           38 // Enable
#define DIR_PIN          2 // Direction
#define STEP_PIN         4 // Step
// #define CS_PIN           42 // Chip select
// #define SW_MOSI          66 // Software Master Out Slave In (MOSI)
// #define SW_MISO          44 // Software Master In Slave Out (MISO)
// #define SW_SCK           64 // Software Slave Clock (SCK)
#define SW_RX            16 // TMC2208/TMC2224 SoftwareSerial receive pin
#define SW_TX            17 // TMC2208/TMC2224 SoftwareSerial transmit pin
#define SERIAL_PORT Serial2 // TMC2208/TMC2224 HardwareSerial port
#define DRIVER_ADDRESS 0b00 // TMC2209 Driver address according to MS1 and MS2

#define R_SENSE 0.11f 

// TMC2208Stepper driver(&SERIAL_PORT, R_SENSE);                     // Hardware Serial
//TMC2208Stepper driver(SW_RX, SW_TX, R_SENSE);                     // Software serial
TMC2209Stepper driver(&SERIAL_PORT, R_SENSE, DRIVER_ADDRESS);
// TMC2209Stepper driver(SW_RX, SW_TX, R_SENSE, DRIVER_ADDRESS);

void setup() {
  // pinMode(EN_PIN, OUTPUT);
  pinMode(STEP_PIN, OUTPUT);
  pinMode(DIR_PIN, OUTPUT);
  // digitalWrite(EN_PIN, LOW);      // Enable driver in hardware
                                  // Enable one according to your setup
//SPI.begin();                    // SPI drivers
//SERIAL_PORT.begin(115200);      // HW UART drivers
  // driver.beginSerial(115200);     // SW UART drivers

  driver.begin();                 //  SPI: Init CS pins and possible SW SPI pins
                                  // UART: Init SW UART (if selected) with default 115200 baudrate
  driver.toff(5);                 // Enables driver in software
  driver.rms_current(10);        // Set motor RMS current
  driver.microsteps(16);          // Set microsteps to 1/16th
  // driver.blank_time(24);

//driver.en_pwm_mode(true);       // Toggle stealthChop on TMC2130/2160/5130/5160
  driver.en_spreadCycle(false);   // Toggle spreadCycle on TMC2208/2209/2224
  driver.intpol(true);
  driver.pwm_autoscale(true);     // Needed for stealthChop
}

// bool shaft = false;

void loop() {
  // Run 5000 steps and switch direction in software
  // for (uint16_t i = 5000; i>0; i--) {
    digitalWrite(DIR_PIN, HIGH);
    digitalWrite(STEP_PIN, HIGH);
    delayMicroseconds(1000);
    digitalWrite(STEP_PIN, LOW);
    delayMicroseconds(1000);
  // }
  // shaft = !shaft;
  // driver.shaft(shaft);
}

The Sketch Is Uploading Fine But The Stepper Motor Is Making Too Much Of Vibrations And It's Also Causing Much Noise Because Of That.
Does Anyone Know What's Wrong In My Sketch ? Or What Things i Should Add To Make The Stepper Motor Run Smoothly. I Appreciate Any Kind Of Help. THANKS

I have merged your cross-posts @kirito2908.

Cross-posting is against the Arduino forum rules. The reason is that duplicate posts can waste the time of the people trying to help. Someone might spend a lot of time investigating and writing a detailed answer on one topic, without knowing that someone else already did the same in the other topic.

Repeated cross-posting can result in a suspension from the forum.

In the future, please only create one topic for each distinct subject matter.

I also had to move your topics to an appropriate forum category.

In the future, please take some time to pick the forum category that best suits the subject of your topic. There is an "About the _____ category" topic at the top of each category that explains its purpose.


These things are important parts of responsible forum usage, as explained in the "How to get the best out of this forum" guide. The guide contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

First Of All, Thanks For Your Reply And Trying To Help Me.
I've Checked The Wiring 2 Times Just Now To Make It Confirm That There's No Problem There.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.