SkillAgentSearch skills...

TMC2130Stepper

Arduino library for Trinamic TMC2130 Stepper driver

Install / Use

/learn @teemuatlut/TMC2130Stepper
About this skill

Quality Score

0/100

Supported Platforms

Universal

README

This library has been superseded by the newer TMCStepper library which supports many more of the Trinamic stepper drivers and more MCU architectures.

TMC2130Stepper

Arduino library for Trinamic TMC2130 Stepper driver

Installation:

Use the Arduino IDE library manager (Sketch -> Include library -> Manage libraries...)

Search for TMC2130Stepper and then install.

Or download the zip file from Github and extract it to<br> your-scetchbook-location/libraries<br> and restart the IDE.

or goto to you arduino libraries folder and in command line git clone https://github.com/teemuatlut/TMC2130Stepper.git

What works:

Nearly all the features in the registeries are configurable through get/set functions. See below for a list of functions. Datasheet (link) from Trinamic also provides further detail into the settings available.

Simple example

/*
Initializes the library and turns the motor in alternating directions.
*/
#define EN_PIN    38  // Nano v3:	16 Mega:	38	//enable (CFG6)
#define DIR_PIN   55  //			19			55	//direction
#define STEP_PIN  54  //			18			54	//step
#define CS_PIN    40  //			17			64	//chip select

bool dir = true;


#include <TMC2130Stepper.h>
TMC2130Stepper TMC2130 = TMC2130Stepper(EN_PIN, DIR_PIN, STEP_PIN, CS_PIN);

void setup() {
	Serial.begin(9600);
	TMC2130.begin(); // Initiate pins and registeries
	TMC2130.SilentStepStick2130(600); // Set stepper current to 600mA
	TMC2130.stealthChop(1); // Enable extremely quiet stepping
	
	digitalWrite(EN_PIN, LOW);
}

void loop() {
	digitalWrite(STEP_PIN, HIGH);
	delayMicroseconds(10);
	digitalWrite(STEP_PIN, LOW);
	delayMicroseconds(10);
	uint32_t ms = millis();
	static uint32_t last_time = 0;
	if ((ms - last_time) > 2000) {
		if (dir) {
			Serial.println("Dir -> 0");
			TMC2130.shaft_dir(0);
		} else {
			Serial.println("Dir -> 1");
			TMC2130.shaft_dir(1);
		}
		dir = !dir;
		Serial.println(TMC2130.GCONF(), BIN);
		last_time = ms;
	}
}

Current calculations

A simple way to set the current is to use the setCurrent() method.

I_rms = (CS+1)/32 * V_fs/(R_sense+0.02mOhm) * 1/sqrt(2)
I_motor = I_sine/248 * (CS+1/32) * V_fs/(R_sense+0.02mOhm)

Where:<br> I_rms is the rms current<br> I_motor is the motor current<br> CS is the Current Scale value<br> V_fs is the voltage determined by v_sense<br> R_sense is the chosen sense resistor<br> I_sine is the current position in the sine table. I_motor reaches peak value at I_sine = 248<p>

If external_ref is enabled, V_fs is scaled by V_ain/2.5V

Functions

Function | Argument range | Returns | Description --------------------|-----|---|----------------------------- begin | - | - | Initialized pins Enable, Direction, Step and Chip Select.<br>Initialized the SPI pins MOSI, MISO and SCK.<br>Calls spi.begin()<br>Sets off_time = 2 and blank_time = 24 setCurrent | 0..2000<br>0.1 .. 1<br>0..1 | - | Helper function to set the motor RMS current.<br>Arguments:<br><b>uint16_t</b> Desired current in milliamps<br><b>float</b> Sense resistor value<br><b>float</b> Multiplier for holding current<br>Example for SilentStepStick2130: setCurrent(1200, 0.11, 0.5)<p>Makes use of the run_current() and hold_current() funtions. SilentStepStick2130 | 0..2000 | - | Calls the begin() functions and according to the argument sets the current with sense resistor being 0.11 and multiplier being 0.5

Register functions:

Note: You can read the saved value by calling a function without providing an argument.

GCONF register

Function | Argument range | Returns | Description --------------------|-----|---------|----------------------------- GCONF | - | uint32_t| Read actual bits from the register external_ref | 0/1 | bool | Use external voltage reference for coil currents internal_sense_R | 0/1 | bool | Use internal sense resistors stealthChop | 0/1 | bool | Enable stealthChop (dependant on velocity thresholds) commutation | 0/1 | bool | Enable commutation by full step encoder shaft_dir | 0/1 | bool | Inverse motor direction diag0_errors | 0/1 | bool | Enable DIAG0 active on driver errors: Over temperature (ot), short to GND (s2g), undervoltage chargepump (uv_cp) diag0_temp_prewarn | 0/1 | bool | Enable DIAG0 active on driver over temperature prewarning diag0_stall | 0/1 | bool | Enable DIAG0 active on motor stall (set TCOOLTHRS before using this feature) diag1_stall | 0/1 | bool | Enable DIAG1 active on motor stall (set TCOOLTHRS before using this feature) diag1_index | 0/1 | bool | Enable DIAG1 active on index position (microstep look up table position 0) diag1_chopper_on | 0/1 | bool | Enable DIAG1 active when chopper is on diag1_steps_skipped | 0/1 | bool | Enable output toggle when steps are skipped in dcStep mode (increment of LOST_STEPS). Do not enable in conjunction with other DIAG1 options. diag0_active_high | 0/1 | bool | Set DIAG0 to active high diag1_active_high | 0/1 | bool | Set DIAG1 to active high small_hysteresis | 0/1 | bool | <b>0:</b> Hysteresis for step frequency comparison is 1/16 <br><b>1:</b> Hysteresis for step frequency comparison is 1/32 stop_enable | 0/1 | bool | Emergency stop: DCIN stops the sequencer when tied high (no steps become executed by the sequencer, motor goes to standstill state). direct_mode | 0/1 | bool | Motor coil currents and polarity are directly controlled by the SPI interface.

IHOLD_IRUN register

Function | Argument | Returns | Description ----------------|-----------|---------|----------------------------- hold_current | 0..31 | uint8_t | Standstill current (0=1/32…31=32/32) run_current | 0..31 | uint8_t | Motor run current (0=1/32…31=32/32) hold_delay | 0..15 | uint8_t | Controls the number of clock cycles for motor power down after a motion as soon as standstill is detected (stst=1) and TPOWERDOWN has expired.

REG_TPOWERDOWN register

Function | Argument | Returns | Description -----------------|----------|-----------|----------------------------- power_down_delay | 0..255 | uint8_t | power_down_delay sets the delay time after stand still (stst) of the motor to motor current power down. Time range is about 0 to 4 seconds. <br>0…((2^8)-1) * 2^18 tCLK

REG_TSTEP register

Function | Argument | Returns | Description ----------------|-----------|----------|----------------------------- microstep_time | - | uint32_t | Read the actual measured time between two 1/256 microsteps derived from the step input frequency in units of 1/fCLK.

REG_TPWMTHRS register

Function | Argument | Returns | Description --------------------|---------------|-----------|----------------------------- stealth_max_speed | 0..1,048,575 | uint32_t | This is the upper velocity for stealthChop voltage PWM mode. TSTEP ≥ TPWMTHRS - stealthChop PWM mode is enabled, if configured - dcStep is disabled

REG_TCOOLTHRS register

Function | Argument | Returns | Description --------------------|---------------|-----------|----------------------------- coolstep_min_speed | 0..1,048,575 | uint32_t | This is the lower threshold velocity for switching on smart energy coolStep and stallGuard feature.

REG_THIGH register

Function | Argument | Returns | Description ----------------|---------------|-----------|----------------------------- mode_sw_speed | 0..1,048,575 | uint32_t | This velocity setting allows velocity dependent switching into a different chopper mode and fullstepping to maximize torque.

REG_XDRIRECT register

Function | Argument | Returns | Description ----------------|---------------|-----------|----------------------------- coil_A_current | -255..+255 | int16_t | Specifies Motor coil currents and polarity directly programmed via the serial interface. In this mode, the current is scaled by IHOLD setting. coil_B_current | -255..+255 | int16_t | As above.

REG_VDCMIN register

Function | Argument | Returns | Description --------------------|---------------|-----------|----------------------------- DCstep_min_speed | 0..8,388,607 | uint32_t | The automatic commutation dcStep becomes enabled by the external signal DCEN. VDCMIN is used as the minimum step velocity when the motor is heavily loaded. <br>Hint: Also set DCCTRL parameters in order to operate dcStep.

REG_CHOPCONF register

Function | Argument | Returns | Description ------------------------|-----------|-----------|----------------------------- CHOPCONF | - | uint32_t | Read actual bits from the register off_time | 0..15 | uint8_t | Off time setting controls duration of slow decay phase<br>NCLK= 12 + 32TOFF<br>Initialized to value 2 (NCLK = 76) by begin() hysteresis_start | 1..8 | uint8_t | Add 1, 2, …, 8 to hysteresis low value HEND (1/512 of this setting adds to current setting) Attention: Effective HEND+HSTRT ≤ 16. Hint: Hysteresis decrement is done each 16 clocks fast_decay_time | 0..15 | uint8_t | Fast decay time setting TFD with NCLK= 32HSTRT hysteresis_end | -3..12 | int8_t | This is the hysteresis value which becomes used for the hysteresis chopper. sine_offset | -3..12 | int8_t | This is the sine wave offset and 1/512 of the value becomes added to the absolute value of each sine wave entry. disable_I_comparator | 0/1 | bool | <b>1:</b> Disables current comparator usage for termination of the fast decay cycle.<br>chopper_mode needs to be 1. random_off_time | 0/1 | bool | <b>0:</b> Chopper off time is fixed as set by TOFF <br><b>1:</b> Random mode, TOFF is random modulated by dNCLK= -12 … +3 clocks. chopper_mode | 0/1 | uint8_t | <b>0:</b> Standard mode (spreadCycle) <br><b>1:</b> Constant off time with fast decay time. <br>Fast decay time is also terminated when the negative nominal current is reached. Fast decay is after on time. blank_time | 16, 24,<br>36, 54| uint8_t | Set co

View on GitHub
GitHub Stars162
CategoryDevelopment
Updated24d ago
Forks50

Languages

C++

Security Score

100/100

Audited on Mar 15, 2026

No findings