SkillAgentSearch skills...

PyTmcStepper

PyTmcStepper is a pure Python library for controlling Trinamic stepper drivers via UART or SPI on platforms like Raspberry Pi and MicroPython devices.

Install / Use

/learn @Chr157i4n/PyTmcStepper

README

PyTmcStepper

PyPI python version PyTmcStepper PyPI version PyTmcStepper PyPI downloads PyTmcStepper GitHub issues

CI Pipeline Coverage badge



This is a Python library to drive a stepper motor with a Trinamic stepper driver and a single board computer like a Raspberry Pi.

This code is still experimental, so use it at your own risk.

This library is programmed in pure Python. The performance of Python is not good enough to drive the motor with high speed. So if you move the motor with high speed using this library the motor will lose steps.

Tested on a Bigtreetech TMC2209 V1.2 and Bigtreetech TMC2209 V1.3.

It has a rSense of 110 mOhm and it uses one Pin (PDN_UART) for UART RX and TX. So the PDN_UART-Pin needs to be connected to the Raspberry Pis RX-Pin directly and to the TX-Pin with an 1kOhm resistor. You can read more about this in the datasheet from Trinamic.

Because the TMC2209 uses one shared pin for transmit and receive in the UART communication line, the Raspberry Pi also receives what it sends. Well, the Pi receives 4 bytes from itself and 8 bytes from the driver. So the Pi receives a total of 12 bytes and only the last 8 are the reply, of which only 4 are data bytes.

The documentation of the TMC2209 can be found here: TMC2209 - Datasheet

The code is also available on PyPI.

Installation (on Raspberry Pi)

Installation with PIP

pip3 install PyTmcStepper

Installation with GIT

  • clone this repo to your Raspberry Pi using

    git clone https://github.com/Chr157i4n/PyTmcStepper
    cd PyTmcStepper
    
  • (optional) install the lib to get proper syntax highlighting and start the demo scripts from every path

    pip3 install -e .
    
  • install the required modules

    pip3 install RPi.GPIO pyserial
    
  • enable the serial port in

    sudo raspi-config
    

Board Support

The following table shows the supported boards and which libraries for GPIO access is being used for that board.

Library | Installation Parameter | Boards ----------- | ------------------------- | ----------- RPi.GPIO | raspberry_pi | Pi4, Pi3 etc. gpiozero | raspberry_pi5 | Pi5 Jetson.GPIO | nvidia_jetson | Nvidia Jetson periphery | luckfox_pico | Luckfox Pico OPi.GPIO | orange_pi | Orange Pi pyftdi | ftdi | Windows/Linux machine | - | Pico, Pico2, ESP32, etc.

Those libraries are needed for this library to work. You can either install the correct library yourself. You can also install the needed GPIO library by specifying the Installation Parameter while installing this library:

pip3 install PyTmcStepper[raspberry_pi, uart]

Driver Support

The currently supported drivers are:

Driver | Interface -- | -- TMC2208 | UART TMC2209 | UART TMC2240 | SPI/UART TMC5160 | SPI/UART

Submodules

with V0.7 the code for enabling the motor current output and the code for controlling the motion of the drivers is split into their own classes to be able to support the diverse methods.

EnableControl

The EnableControl module controls the motor current output. It has a function set_motor_enabled with one boolean parameter. This function is used to enable or disable the Motor current output.

EnableControl | Class | Driver | Notes -- | -- | -- | -- Pin | TmcEnableControlPin | all | the EN Pin of the Driver needs to be connected to a GPIO of the Pi TOff | TmcEnableControlToff | all | the EN Pin needs to be connected to GND.<br />On the TMC2209 this enables current Output on Startup!<br />On the TMC2240 this works fine, because TOff is per default 0 (off).

MotionControl

The MotionControl module controls the motion of the motor. Before a movement the motor current output needs to be enabled by the EnableControl module.

MotionControl | Class | Driver | Notes -- | -- | -- | -- STEP/DIR | TmcMotionControlStepDir | all | the STEP and DIR pin of the driver must each be connected to a GPIO of the Pi STEP/REG | TmcMotionControlStepReg | all | only the STEP pin needs to be connected to a GPIO of the Pi.<br />The direction is controlled via the Register. VACTUAL | TmcMotionControlVActual | TMC220x | the Direction and Speed is controlled via Register. But VActual does only allow setting a speed and therefore cannot control positioning of the Motor. STEP_PWM/DIR | TmcMotionControlStepPwmDir | all | In contrast to STEP/DIR, the step pin is controlled by PWM. This reduces the load on the CPU, but does not allow precise positioning (similar to the VACTUAL).<br />STEP must be connected to a PWM-capable pin for this purpose RAMP_GENERATOR | TmcMotionControlIntRampGenerator | TMC5160 | the SD_Mode Pin needs to be LOW for this to work. This is not possible on all TMC5160-Boards easily.

Further methods of controlling the motion of a motor could be:

  • via a µC which controlls the Motor
  • highperformance Step/Dir Library written in a compiled language (C/C++)

Com

Com | Class | Driver | Notes -- | -- | -- | -- UART | TmcComUart | all | Communication via UART (RX, TX). See Wiring<br />pyserial needs to be installed SPI | TmcComSpi | TMC2240 | Communication via SPI (MOSI, MISO, CLK, CS). See Wiring<br />spidev needs to be installed SPI over FT232H | TmcComSpiFtdi | TMC2240 | Communication via SPI using a FT232H. This can be used on any OS.<br />pyftdi needs to be installed

Wiring

wiring diagram

Pin TMC | Color | connect to | Function -- | -- | -- | -- VDD | | 3,3V of Raspberry Pi | recommended, for forcing the TMC to use 3,3V logic level GND | BLACK | GND of Raspberry Pi | GND for VDD and Signals VM | RED | 12V or 24V of power supply | power for the motor GND | BLACK | GND of power supply | power for the motor EN | RED | GPIO21 of Raspberry Pi | enable the motor output STEP | GREEN | GPIO16 of Raspberry Pi | moves the motor one step per pulse DIR | WHITE | GPIO20 of Raspberry Pi | set the direction of the motor DIAG | ORANGE | GPIO26 of Raspberry Pi | optional, for StallGuard

The GPIO pins can be specific when initiating the class. If you test this on a breadboard, make sure to cut off the bottomside of the pins (Vref and DIAG) next to the EN pin, so that they are not shorted trough the breadboard.

UART

Pin TMC | Color | connect to | Function -- | -- | -- | -- TX or PDN_UART with 1kOhm | YELLOW | TX of Raspberry Pi | send data to TMC via UART RX or PDN_UART directly | YELLOW | RX of Raspberry Pi | receive data from TMC via UART

SPI

Pin TMC | Color | connect to | Function -- | -- | -- | -- MOSI | | MOSI | Data from Pi to TMC MISO | | MOSI | Data from TMC to Pi SPI-CLK | | CLK | Clock for SPI CS | | SPI CE0 (for the demo scripts) | Chipselect (2nd parameter of TmcComSpi "spi_dev")

Demo scripts

You can run the demo scripts files from the main directory with

python3 -m demo.demo_scri

Related Skills

View on GitHub
GitHub Stars97
CategoryDevelopment
Updated9d ago
Forks30

Languages

Python

Security Score

100/100

Audited on Mar 27, 2026

No findings