Section 5: Understanding Trinamic Drivers

The advanced features such as StallGuard can be purchased in our PDF guide here.

 

Trinamic drivers need to be thought of in two separate parts: Setup and Control.

Using SPI or UART (depending on the model of driver you have), we can set dozens of different settings. Then we control the driver using the Step and Dir pin to make out motor turn.

Always try to think of Trinamic drivers as these two very different parts, Setup and Control.

Let me give you an example of setup and control. Let’s pretend you want to drive your car to the restaurant. What do you?

You get in your car and turn the engine on. Then you change the heat settings to make it hotter or colder. Then you adjust the seat to make sure it feels comfortable and maybe adjust the radio and prepare for the drive. Then you place your car into the drive gear and press the gas pedal to drive forward.

In this scenario you first setup the car by changing all the setting prior to driving. Then you placed it into gear and pressed the gas pedal to control it.

Trinamic drivers work in a similar way. We use UART or SPI (depending on the driver you use) to set different settings in the driver before we move it. We can set the current, stall value, and many more things using the TMCStepper library.

Then we pulse the STEP and DIR pin to get the motor to begin turning by using the FastAccelStepper Library.

 

The STEP and DIR Pins

Making the motor turn requires just two pins on almost any stepper driver, even primitive ones like the A4988. These are the Step pin and the Dir pin. The Dir pin is the Direction pin. When it gets a high signal, it spins one way, and when it gets a low signal, it spins the other way.

The Step pin as also very simple. When it receives a high signal from the ESP32, it moves one step. It’s that simple. If we want it to accelerate or move at a high speed, we just change the rate of high signals that we send from the ESP32. Think of it like flipping a light switch, each time you flip the switch on and off, the motor moved one step. If you want it to mover very fast, you just flip it on and off very very fast.

 

Modes of Operation

The TMC2209 was designed to replace older stepper motor drivers in 3D printers. To make it compatible with a wide range of 3D printers as well as advanced users like us, they created three different modes of operation. This is important to understand because reading the datasheet will get confusing without understanding this.

Let's see what kinds of settings there are for us to use. Open the TMC2209 datasheet by downloading it from the resources page or the Trinamic website.

Let’s go to page 4, at the very bottom section titled Three Modes of Operation:

NOTE: The Trinamic datasheet is VERY IMPORTANT. It contains so much information and is critical to understand. Take your time to read it carefully when required.

OPTION 1: Standalone STEP/DIR Driver (Legacy Mode): This mode is designed so that boards like the BIGTREETECH 2209 V1.2 fit 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 sure what this mode is for. I think it may be for newer 3D printers, but we will not use this either.

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 can set via UART? One of the most important settings is the current setting. We can set the current of our motor to use very little current which will allow it to run cooler and safer, or we can use a lot of current to move very heavy objects. This is an example of just one of dozens of settings we will control via UART, something that is not possible to do in Legacy Mode (Options 1).

 

Examples

The best way to learn is to dive right in. We’ll open the example and learn it as we go. If you only want to learn to use the Single Core code, please read this section as well because it will cover many important things that will need to be considered for the single core version, because single core operation is more complicated.

Example 1: Dual Core

First, we include the two libraries that we’ll need.

#include <TMCStepper.h>
#include <FastAccelStepper.h>

It’s completely possible to write your code without the aid of these two libraries. The TMCStepper library helps us set up the TMC2209 settings. And the FastAccelStepper library pulses the STEP/DIR pins and keeps tracks of steps for us, so that we don’t have to.

First, let’s talk about the settings that are possible to set on the TMC2209 and why using a library is extremely helpful in setting them.


Changing Settings


How do we change the stall, current, or any of the other 50+ settings? To change the settings on the driver, we just need to change the value of specific registers. Think of the driver as an airplane cockpit. There are a million switches everywhere. All we need to do is flip the correct switch, which the datasheet tells us how to do.

What is a register?

A register a small computer circuit that can hold a small piece of information. Think of it like a small box that contains a group of on/off switches, like a light switch. Each of these light switches controls a different setting depending on its state of on or off. It’s our job to find which box to open, and which switch to flip to change a setting.

This next section might be a little dense, but it's important to understand. Normally we will not program the driver in this way because the TMCStepper library abstracts this step and does it all for us. But it's very important to understand how things work under the hood. Without this knowledge, you will not understand how the datasheet works and will struggle because of it.

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 look around as this is the most important and interesting section in the datasheet.

Here we have a quick overview of the Register Map section. There are typically three types of registers. Ones that you can only read data from, ones you can only write data to, and ones that allow you to do both.

  1. R means you can read the register.
  2. W means you can write to the register.
  3. R/W means you can read and write to the register.

 

 Let’s explore our first register. On page 20 we see one of the most important registers called GCONF which is the first of our General Configuration Registers.

Let's begin by reviewing the different columns of these registers.
On the left in green is the column R/W, which tells us if we can Read, Write, or Read & Write this register.

To the right in red is the "Addr." column. This is the address of the register. Think of this as the name of the register box. When we want to write to a register, this is the address that we write to.

To the right in orange is the "n" column, this is the number of bits in the register. This is the number of switches that it has. In this case, the GCONF register is 10 bits (0-9) which means we can flip 10 switches on or off.

If you look at the purple "Bit" column, you can see all 10 bits (0-9) and what each bit controls. The Register name is just a name, we don’t use it for anything except to make it easier for us to read.

Now let's look at the bits in the GCONF register. 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. If, for example, we want to inverse the motor direction, we set bit 3 to the binary value of 1, which is like flipping the switch on.

Let’s say we want to inverse the shaft direction. In that case, we need to set this entire GCONF register to a certain value. How do we set it? Well, it’s easy. We send the integer value “8” to address 0x00. Doing so will inverse the motor direction.

Do you see why it's the number 8? Think of all these 10 bits as binary like the red numbers I placed on the datasheet below.

If we send the value 8, it will turn the shaft bit 3 into a value "1". This will inverse the motor shaft, which means the motor will spin in the other direction. This can also be accomplished by sending the binary value of "1000", which is also the integer "8".

What if we also want to turn on test_mode, which is bit 9, along with inverse the 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 the value of bits 3 and 9 into a 1.

See how simple this 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 in our code?

Sending Datagrams

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 ‘datagram’ value using UART in Arduino. If we send the 64-bit number according to these instructions, the TMC2209 registers will get set. But this process can be complicated. Trinamic does offer an API that will help you out quite a bit on their GitHub page.

Luckily there's a very easy way. The TMCStepper Library!

 

NEXT SECTION: 6

Course Sections:

Advanced Sections:

Back to blog

Leave a comment