Section 6: TMCStepper Library
The advanced features such as StallGuard can be purchased in our PDF guide here. |
The TMCStepper library is an Arduino library written by Github user teemuatlut. It allows you to easily change the values of almost any Trinamic driver in a very easy way. I recommend using it for all Trinamic projects.
Let's go back to our Arduino sketch.
We’ll create an object for the TMCStepper class and pass the parameters required by the class. In this case, we will call it “driver”.
TMCStepper driver(&Serial1, 0.12f, 0);
Multiple Motors
We can control up to four TMC2209 motors/boards. All we have to do is address them correctly. To assign an address to each board, simply pull the MS1/MS2 pins high according to the chart below. We will only use one motor, which is how each TMC2209 is already set to do. Just leave the MS1/MS2 pins floating (they have a built-in pulldown resistor), for single motor
To control additional motors, just create additional objects and change the address of the motor from 0-3 like below.
TMCStepper driver1(&Serial1, 0.12f, 1);
Single Motor Operation
We are going to run one motor only, so let’s go back to the code to continue.
Let’s open the documentation for the TMCStepper library. A link can be found at the top of the Arduino sketch.
As we can see in the documentation, the object constructor needs 3 parameters when creating it:
TMC2209Stepper (Stream *SerialPort, float RS, uint8_t addr)
- Serial port
- RS value
- Address
To begin, we type in "TMC2209Stepper" like you see below in purple to access the TMC2209 Stepper class. Then we need to create an object name, in this case let's call it "driver" like you see below in red.
TMC2209Stepper driver(&Serial1, 0.12f, 0);
Then we select the serial port to use. The ESP32 has 3 hardware serial ports, 0, 1, and 2. But only 0 and 1 can be used. Serial0 is used by the USB port for the serial monitor, so we can't use it, which leaves us to use Serial1.
Then we place 0.12 (above in blue), this is the Ohms of the current sense resistors that are on your module. Refer to the Current Sense Resistor section below to learn more. This is critical to get right!
Then we place the slave address, which is 0. This is necessary because we can link many TMC2209s together if needed. In this case we are only using one.
Documentation
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 Functions list for the TMC2209Stepper class, 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 TMC2208 as well as the universal Trinamic functions.
Click on the arrows that are in red below, notice that they reveal more functions that are inherited from TMC2208Stepper and TMC Stepper.
And here is the rms_current under the TMC Stepper section
In this example, we can see that to change the current of the motor to 500 mA, we need to write the following code in Arduino:
driver.rms_current(500);
This is because the library expects an integer for the milliamps of current.
Recommended Workflow for Setting Values: First, go to the TMC2209 datasheet and figure out what setting you want to change. Then go to the TMCStepper docs and try to find the name of the function that will control that value. Then click on it to figure out what values need to be passed into it.
NEXT SECTION: 7
Course Sections:
- Section 0: Background
- Section 1: Hardware Setup
- Section 2: Stepper Motor Basics
- Section 3: Power Requirements
- Section 4: Arduino Setup
- Section 5: Understanding Trinamic Drivers
- Section 6: TMCStepper Library
- Section 7: FastAccelStepper Library
- Section 8: ESP32 Dual Core Setup
- Section 9: Motor Setup
- Section 10: Understanding Positioning
Advanced Sections: