Section 5: Setting Trinamic Drivers

You are viewing Section 5 of 13

Let’s open up our Arduino sketch again.

We are now going to discuss setting up the TMCStepper library which will allow us change the settings on the driver.

On the Firmware tab we first include TMCStepper.h on line 5

But what is the TMCStepper library?

Remember when I said that we need to set up the motor before controlling it? This is the library used to set up the motor.

It allows us to easily set different settings into the 2209 via UART or SPI (depending on your driver).


NOTE: The Trinamic datasheet is VERY IMPORTANT. It contains so much information and is critical to understand.

Let's take a look at it to see what kinds of settings there are for us to use.

CLICK HERE to open the datasheet.

Let’s go to page 4, at the very bottom section titled THREE MODES OF OPERATION:

The 2209 was designed to replace older 3D printer drivers, so they created 3 types of modes of operating this driver:

OPTION 1: Standalone STEP/DIR Driver (Legacy Mode): Remember the 3D printer board we looked at in the beginning of this? This mode is designed so that board fits perfectly in place of older 3D printer drivers. This is called legacy mode and is very crude. We will not use it.

OPTION 2: Standalone STEP/DIR Driver with OTP pre-configuration: I am not really sure what this mode is for. I think it may be for newer 3D printers, but I am not sure.

OPTION 3: STEP/DIR Driver with Full Diagnostics and Control: This gives us full control of all settings via UART. This is what we will use.

So what kind of settings do we need to set? One of the most important settings is current. We can set the current of our motor to use very little which allows it to run cooler and is safer, or we can use a lot of current to move very heavy objects. 

Setting Registers

Trinamic drivers operate by setting registers. There are a number of registers on the IC and when we write different values to the registers, they set different settings on the IC. It's actually very simple as I will show you.

Think of the driver as an airplane cockpit. There are a million switches everywhere. All we need to do it flip the correct switch, which the datasheet tells up how to do.

This next section might be a little dense, but it's important to understand. We will not actually program the driver like this because the TMCStepper library abstracts this step and does it all for you. But it's very important to understand how things work in the background. Without this knowledge you will not understand how the datasheet works and will struggle a lot.

What registers do we have and what settings can we change?

Go to page 19 of the datasheet titled “Register Map”

This is the beginning of the section that shows us which registers we can set.

Let’s take a look around as this is the most important and interesting section in the datasheet.

Notice that when you reset power, all settings are wiped out. We will address this problem later.

  • R means you can read the register
  • W means you can write to the register
  • R/W means you can read and write to the register

Go down to page 20, this is the beginning of the registers.

On page 20 we see one of the most important registers called GCONF which are our GENERAL CONFIGURATION REGISTERS .

Notice all of the columns above.

On the left is column R/W, this tells us if we can R, W, or R/W this register.

To the right of that is Addr.

This is the address of the register.

When we want to write to a register, we select its address first. To the right of the is the number of bits in this register.

In this case it is 10 bits (0-9).

If you look on the right side you can see all 10 bits (0-9) and what they all control. The Register name is just a name, we don’t use it for anything except to make it easier to us to read.

Now let's look at the bits. Here we have bits 0-9 allowing us to set various settings. These bits are all very random and do not have anything to do with each other. These are just different settings we can set. For example, if we want to inverse the motor direction, which we will do in our sketch, we set bit 3 to 1.

Let’s say we want to only change this setting and nothing else in this register How do we set it? Well it’s easy. We send the integer “8” to address 0x00.

Do you see why the number 8? Think of all these 10 bits as binary like the red numbers I placed in the datasheet.

If we send the value 8, it will flip bit 3 to 1. This will inverse the motor shaft, which means the motor will spin the other way.

What if we also want to turn on test_mode, which is bit 9, along with inverse shaft. We send the value 520. That is because bit 9 is value 512, and bit 3 is value 8. If we send 520, it will turn bit 3 and 9 to a 1.

See how simple this actually is. Just find the setting you want to change, find the address, and find the bit to set.

Now that we know what address to send to (0x00) and what value (520), how do we send it?

Sending Bits

Go to page 15 of the datasheet. In section 4.1.1 we see what needs to be done to send the value of 520 to address 0x00.

We need to send a 64 bit value using UART in Arduino. If we send the 64 bit number according to these instructions, the TMC2209 values will get set. 
But that can be complicated. Trinamic does offer an API that will help you out quite a bit on their GitHub page here.
Luckily there's an even easier way!
The TMCStepper Library!

TMCStepper Library

Go back to your Arduino sketch.

In the MotorControl.h tab, line 41, we create an object and input the parameters required by the library. In this case, we will call it “driver”
TMC2209Stepper driver(&Serial2, 0.10f , 0b00);

Let’s open up the documentation for this library on the Github page.
As we can see here, the object needs 3 parameters.
  1. Serial port
  2. RS value
  3. Address

While we are here let’s explore this documentation a bit more as you will need to reference it to change the values in the future and it's very good to understand how it works.

On the left side we can explore different classes, depending on the driver you have

You can even look at the 2208, 2130, and more.

Let’s say we want to change the current that the driver supplies to the motor. If we look at the Public Member Function list in the TMC2209, can you find something that says rms_current? (This stands for root mean square current, more on that later).

It doesn't exist here, so where is it?

The TMC2209 is very similar to the TMC2208 as well as nearly every other Trinamic driver, so it inherits many of the functions from the 2208 as well as the universal Trinamic functions.

Click on the arrows that are in red below, notice that they reveal more functions that are inhereted from TMC2208Stepper and TMC Stepper.

And there is it rms_current under the TMC Stepper section

In this example, we can see that in order to change the current to 500 mA, we need to write the following code in Arduino:


This is because the library expect an integer for the milliamps of current.

Recommended Workflow: First go to the TMC2209 datasheet and figure out what setting you want to change. Then come here and try to find its name. Then click on it to figure out what values need to be passed into it

Let's go back to the code. Let's get back to line 41, creating the driver object

TMC2209Stepper driver(&Serial2, 0.10f , 0b00);

First type in TMC2209Stepper to access the 2209 class, then create a name, I call is driver.


Then we place the serial port to use. The ESP32 has 3 hardware serial ports, 1, 2, and 3. But only 1 and 2 can be used.

Serial 1 is used by the USB port for the serial monitor, so we can't use it, which leaves us to use Serial2.

Then we place 0.10, this is the Ohms of the current sense resistors. Then we place the slave address, which is 0b00.

What are the current sense resistors? This is a very important to understand because placing the wrong value can lead to destruction!! Follow along to the next section.


Course Sections:

Leave a comment