Using Raspberry Pi Pico to run TMC2209 in UART

Hello!

I’m looking to run multiple stepper motor drivers (up to 4) from a Raspberry Pi Pico.
I’m starting out using a BigTreeTech TMC2209 v1.3 module and a 5v stepper motor I purchased from Micro Center. I want to eventually control a 24v nema 17 motor, but I don’t have a power supply, so for testing, I’m using the 5v stepper. It came with its own driver, which used the GPIO pins of the pico to activate the coils of the motor. I could reverse the motor by reversing the order the pins activated in, so I know that the motor can reverse.

My problem is that I can’t get the motor to change direction when using the TMC2209. In all of the documentation I’ve read, this should be as simple as switching the state of the DIR pin on the TMC2209, “HIGH” or “1” for clockwise and “LOW” or “0” for counterclockwise. I tried without UART control first, and that didn’t work, and am currently trying to figure out how to connect the driver via UART. I have 2 major questions, 1. Why won’t the stepper change directions, and 2. How do I connect the TMC2209 to the Pi Pico for UART control?

My current wiring is this:
From the Pico to the TMC2209: DIR pin is connected to GP18, STEP pin is connected to GP19, ENABLE (EN) pin is connected to GP20. UART TX is connected to GP0, UART TX is connected to GP1. The VIO pin is connected to VSYS (physical pin 39) and GND is connected to GND (physical pin 38) on the Raspberry pi.
From the TMC2209 to the Motor:
A1, A2, B1 and B2 are connected to 1, 2, 3, and 4 respectively. The 5v input for the motor is also connected to VSYS on the pico.

this is the best pinout I could find for the driver

this is the tutorial I referenced when controlling the motor with its own driver

And this is the code I tried using to control the stepper via uart, if you remove the uart sections, it’s the same code I used to control it without uart. Line 53 is where I set the step direction, by setting it to “LOW” (0) I expect it to spin counter-clockwise, and by setting it to “HIGH” (1) it should spin clockwise, but it only spins clockwise.

#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/uart.h"

#define DIR_PIN 18
#define STEP_PIN 19
#define ENABLE_PIN 20
#define UART_ID uart0
#define BAUD_RATE 115200
#define UART_TX_PIN 0
#define UART_RX_PIN 1
#define STEPS_PER_ROTATION 16384

void setup() {
    // Initialize GPIO
    gpio_init(DIR_PIN);
    gpio_set_dir(DIR_PIN, GPIO_OUT);
    
    gpio_init(STEP_PIN);
    gpio_set_dir(STEP_PIN, GPIO_OUT);
    
    gpio_init(ENABLE_PIN);
    gpio_set_dir(ENABLE_PIN, GPIO_OUT);
    
    // Enable the driver
    gpio_put(ENABLE_PIN, 0);
    
    // Initialize UART
    uart_init(UART_ID, BAUD_RATE);
    gpio_set_function(UART_TX_PIN, GPIO_FUNC_UART);
    gpio_set_function(UART_RX_PIN, GPIO_FUNC_UART);
    
    // Initialize stdio for USB serial communication
    stdio_init_all();
}

void step_motor(int steps, int delay_ms) {
    for (int i = 0; i < steps; i++) {
        gpio_put(STEP_PIN, 1);
        sleep_us(delay_ms);
        gpio_put(STEP_PIN, 0);
        sleep_us(delay_ms);
    }
}

void send_uart_command(const char* command) {
    uart_puts(UART_ID, command);
}

int main() {
    setup();

    gpio_put(DIR_PIN, 0);

    step_motor(STEPS_PER_ROTATION, 500);
    
    send_uart_command("DIS");

    return 0;
}

28BYJ-48 is a unipolar stepper motor - you can’t drive it with a bipolar driver such as a TMC2209 module without modification. There is a description of the procedure here.

Not that there is a comment that you cannot run the modified motor in microstepping mode with this change, but I don’t see any reason why that would be - it’s worth a try. (The TMC2209 will only do single stepping in UART mode).

Your 24V NEMA would likely run at 12V without a load for testing purposes, and that would be a much simpler setup for testing.
/edit
(The TMC2209 will not do single stepping unless it is in UART mode).
edit/

2 Likes

You were right! I didn’t think to check that. I got a breadboard 12v power supply and hooked the motor up to it, and it works forwards and back. Thank you for the swift response!

I don’t really need microstepping for this project, so I am interested in using uart mode to potentially run up to 4 motors from the pico, would you know how to connect the tmc2209 to the pico?

1 Like

The TMC2209 UART is a very odd configuration and I haven’t used it. Then is some useful discussion here:
Chr157i4n/TMC2209_Raspberry_Pi: this is a Python library to drive a stepper motor with a Trinamic TMC2209 stepper driver and a Raspberry Pi (github.com)

It would seem that you could use any example of Pi UART configuration if you are able to ignore (or not even connect) the data received and only try to do OTP. For full bi-directional communication it gets more complex.

1 Like

I was afraid of that,

Thank you for your help!