Jump to content

Running TMC2240 Stepper Motor Driver with Arduino?


Runsva

Recommended Posts

I am attempting to run a Nema 17 stepper motor with the BigTreeTech TMC2240 stepper motor driver. I am connecting to the driver module with an Arduino UNO R3 board, and I'm using Arduino IDE v2.3.0.

Datasheets:
- TMC2240: https://www.analog.com/media/en/technical-documentation/data-sheets/tmc2240_datasheet.pdf
- Arduino UNO R3: https://docs.arduino.cc/resources/datasheets/A000066-datasheet.pdf

------------------------------------------------------------------------------------------------------------------------

At first I tried to connect the driver in the same fashion as it's predescessor, the TMC2209 which I used before and had no issues with. I want to run this driver as simply as possible, with the "STEP" and "DIR" pins like most of the other simillar drivers out there.
I can see that the TMC2240 has a simillar pinout to the TMC2209, with the microstep "MS" pins replaced with the SPI & UART connection pins:

image.jpeg.51b70370e0144b98783da8d34ec46d54.jpeg image.jpeg.c3cc7ccbb2c9c7a87dadf088c98936aa.jpeg

The following setup (with the `STEP` pin connected to pin 6, `DIR` pin connected to pin 7, `EN` pin set to `LOW` and 12V supplied to `VM`):

image.thumb.jpeg.285d5a4bfc8fb1f0252c41f74bbc6940.jpeg

With the following code:

const int PIN_STEP = 6;
const int PIN_DIR = 7;
const int PIN_EN = 0;

void setup() 
{
  Serial.begin(9600);

  pinMode(PIN_STEP, OUTPUT);
  pinMode(PIN_DIR, OUTPUT);
  pinMode(PIN_EN, OUTPUT);

  digitalWrite(PIN_EN, LOW);
}

void loop() 
{
  digitalWrite(PIN_DIR, HIGH);
  for (int i = 0; i < 200; i++)
  {
    digitalWrite(PIN_STEP, HIGH);
    delayMicroseconds(500);
    digitalWrite(PIN_STEP, LOW);
    delayMicroseconds(500);
  }
  digitalWrite(PIN_DIR, LOW);
  for (int i = 0; i < 200; i++)
  {
    digitalWrite(PIN_STEP, HIGH);
    delayMicroseconds(500);
    digitalWrite(PIN_STEP, LOW);
    delayMicroseconds(500);
  }

  delay(3000);
}

Resulted in no activity or energiziation of the motor. Since there was virtually no current being drawn by "VMot", and since when I used the TMC2209 even when disabled the driver would still draw very little but some current, this setup clearly didn't work.

---

I then took a good look at the datasheet, to see what exactly I was missing. Since I want to run the driver using the "STEP" and "DIR" pins, I went to the section that covers that communication mode:

image.jpeg.10d9f37c963c93c57a287df0e7255ec5.jpeg

image.thumb.jpeg.654564820ed784635e9d25c223c47cd2.jpeg
(pages 27, 28)

The only mention of internal registers altering the operation mode using the "STEP" and "DIR" pins is the "CHOPCONF" register. Since the TMC2240 replaced the microstep "MS" pins with the SPI & UART pins, I had initially assumed that the SPI & UART pins would only be used for 3D printer boards that these drivers are made for. However, while this section doesn't directly state that SPI needs to be used in order to enable the "STEP" and "DIR" interface, given that the previous setup didn't work at all made me want to try and use SPI to change the value of that register, and see whether that improves the situation.

I then went to the SPI section in the datasheet, to try and figure out exactly how I could change internal registers of the driver.


(page 22)

The above section on the SPI interface details the structure of the messages sent, with each message constisting of 5 bytes; the first byte being the register address, and the rest being the data. It supports both reading & writing of most registers.

In every reply message recieved from the driver, one of the bytes apparently contains the "SPI_STATUS" flags, which are defined as follows:

image.jpeg.360ad8b64d1b6c7c77f655bca2c99088.jpeg
(page 23)

The driver is also apparently configured to communicate via SPI by default, with UART being disabled as per the "UART_EN" pin and a solder joint on the back of the board:

image.jpeg.917b4f3b972c8ad1032fe54c60e9f93e.jpeg
(page 15)

The document as includes some example SPI commands:

image.jpeg.ff551690b760a84fa3902af61f610e76.jpeg
(page 23)

------------------------------------------------------------------------------------------------------------------------

I then setup my driver board with my Arduino as follows, with all the SPI pins connected:

image.thumb.jpeg.c5801d00e10239ccc56e8063aa392ee8.jpeg image.thumb.jpeg.59a0fad16349d2fff291436c4a5a4dd7.jpeg

I wanted to then try and send some sample read commands to the driver, to try and read some of the registers.

All of the registers and their purposes are defined at the bottom of the document, and are apparently separated into a few broad categories, defined as such:

image.jpeg.43834cb7eb192241904da4269026d711.jpeg
(page 74)

The "CHOPCONF" register is located at address "0x6C", and one of it's bytes named "MRES" contains the current microstep configuration defined as follows:

image.jpeg.89b3e9960ae1ceae05dec9319334d75a.jpeg
(page 110)
image.jpeg.ef2ba5523f1da5c3c86378bca440211b.jpeg
(page 111)

I first wanted to try and read the "CHOPCONF" register, using the following code which polls the register every 3 seconds:

#include "SPI.h"

const int PIN_EN = 0;
const int PIN_CS = 3;

void setup() 
{
  Serial.begin(9600);

  pinMode(PIN_EN, OUTPUT);
  pinMode(PIN_CS, OUTPUT);
  SPI.begin();

  digitalWrite(PIN_EN, LOW);
}

void loop() 
{
  uint8_t data[5] = {0x6C, 0x00, 0x00, 0x00, 0x00};
  SPIExchange(data);

  Serial.print(data[0], BIN); Serial.print(", ");
  Serial.print(data[1], BIN); Serial.print(", ");
  Serial.print(data[2], BIN); Serial.print(", ");
  Serial.print(data[3], BIN); Serial.print(", ");
  Serial.println(data[4], BIN);

  delay(3000);
}

void SPIExchange(uint8_t* data)
{
  digitalWrite(PIN_CS, LOW);
  SPI.transfer(data, 5);
  digitalWrite(PIN_CS, HIGH);
}

Here I'm using the address "0x6C" defined in the datasheet, followed by all zeroes like in the example read command shown previously.
The values I got were very inconsistent. It would alternate between the first byte showing some value, and all zeroes:

image.jpeg.e010acb5fb991f3cdded8748f5f05224.jpeg

I tried polling several other registers to see whether the behaviour would change, but to no avail.

------------------------------------------------------------------------------------------------------------------------

At this point I'm quite a bit confused as to how exactly one is supposed to operate this driver. Given that the TMC2240's predescessor, the TMC2209, was easily operated using the "STEP" and "DIR" pins, it's strange to me that this driver doesn't.

I wanted to ask for feedback and suggestions before trying other things, especially from others who have also tried using the TMC2240 driver.

Thanks for reading my post, any guidance is appreciated!

image.jpeg

Edited by Runsva
fixed image
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...