import machine
import utime
# Define GPIO pins for TMC2209
STEP_PIN = 22
DIR_PIN = 23
UART_TX_PIN = 18
UART_RX_PIN = 19
# Define pins for TMC2209 driver
step_pin = machine.Pin(STEP_PIN, machine.Pin.OUT)
dir_pin = machine.Pin(DIR_PIN, machine.Pin.OUT)
# Define UART parameters
uart = machine.UART(1, baudrate=115200, tx=UART_TX_PIN, rx=UART_RX_PIN)
# Set initial pin states
step_pin.value(0)
dir_pin.value(0)
# Define stepper motor parameters
steps_per_rev = 400 # Number of steps per revolution
microsteps = 1 # Microstepping mode (1, 2, 4, 8, 16)
rpm = 1 # Desired revolutions per minute
delay = 60 / (steps_per_rev * microsteps * rpm) # Delay between steps
# Set current level (A) to 1.68A
uart.write(b'\x01\x00\x0F\xA0') # Send IHOLD_IRUN register write command via UART
# Set stealthChop mode
uart.write(b'\x05\x00\x00\x03') # Send GCONF register write command via UART
uart.write(b'\x01\x00\x01\x10') # Send IHOLD_IRUN register write command via UART
uart.write(b'\x05\x00\x00\x01') # Send GCONF register write command via UART
# Define step direction function
def step():
step_pin.value(1)
utime.sleep_us(1)
step_pin.value(0)
utime.sleep_us(1)
# Main loop
while True:
dir_pin.value(0)
for i in range(steps_per_rev * microsteps):
step() # Step in one direction
utime.sleep_us(int(delay))
dir_pin.value(1)
for i in range(steps_per_rev * microsteps):
step() # Step in the opposite direction
utime.sleep_us(int(delay))
Paste Hosted With By Wklejamy.pl