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;
}