Skip to content
Snippets Groups Projects
README.md 3.2 KiB
Newer Older
  • Learn to ignore specific revisions
  • Quentin Bolsee's avatar
    Quentin Bolsee committed
    # TMC2209 Xiao RP2040 stepper
    
    Quentin Bolsee's avatar
    Quentin Bolsee committed
    
    
    Quentin Bolsee's avatar
    Quentin Bolsee committed
    This is a stepper controller based on a xiao RP2040 communicating with a TMC2209 driver. One interesting application is using Stallguard for sensorless homing:
    
    Quentin Bolsee's avatar
    Quentin Bolsee committed
    
    
    Quentin Bolsee's avatar
    Quentin Bolsee committed
    ![](video/homing.mp4)
    
    Quentin Bolsee's avatar
    Quentin Bolsee committed
    
    
    Quentin Bolsee's avatar
    Quentin Bolsee committed
    ## Design
    
    Quentin Bolsee's avatar
    Quentin Bolsee committed
    
    
    Quentin Bolsee's avatar
    Quentin Bolsee committed
    Jake designed this board in EAGLE with the following in mind:
    
    Quentin Bolsee's avatar
    Quentin Bolsee committed
    
    
    Quentin Bolsee's avatar
    Quentin Bolsee committed
    - The TMC2209 has an internal regulator that needs to be bypassed when feeding it only 5V. A resistor of 10R must be added for this.
    - TX and RX pins are connected with a 10k resistor, with RX going to the PDN_UART pin of the TMC
    
    Quentin Bolsee's avatar
    Quentin Bolsee committed
    
    
    Quentin Bolsee's avatar
    Quentin Bolsee committed
    The following schematic is designed for a SAMD21 Xiao board, the GPIO numbers are slightly different for a RP2040 variant:
    
    Quentin Bolsee's avatar
    Quentin Bolsee committed
    
    
    Quentin Bolsee's avatar
    Quentin Bolsee committed
    ![](images/schematic-d21.png)
    
    Quentin Bolsee's avatar
    Quentin Bolsee committed
    
    
    Quentin Bolsee's avatar
    Quentin Bolsee committed
    Here is the completed board. This early prototype uses 4 layers to have a ground plane, VCC plane and signals in between:
    
    ![](images/routed.png)
    
    Quentin Bolsee's avatar
    Quentin Bolsee committed
    
    
    Quentin Bolsee's avatar
    Quentin Bolsee committed
    The correct pinout for a RP2040 Xiao is:
    
    Quentin Bolsee's avatar
    Quentin Bolsee committed
    
    
    Quentin Bolsee's avatar
    Quentin Bolsee committed
    | **Signal** | **GPIO** |
    |------------|----------|
    | DIAG       | P07      |
    | ENN        | P06      |
    | DIR        | P27      |
    | STEP       | P28      |
    | TX         | P0       |
    | RX         | P29      |
    
    Quentin Bolsee's avatar
    Quentin Bolsee committed
    
    
    Quentin Bolsee's avatar
    Quentin Bolsee committed
    Note that P29 is not the [standard RX pin](https://wiki.seeedstudio.com/XIAO-RP2040/) for `Serial1` on the Xiao. This can be changed with `Serial1.setRX(29)` in `init()`.
    
    Quentin Bolsee's avatar
    Quentin Bolsee committed
    
    
    Quentin Bolsee's avatar
    Quentin Bolsee committed
    ## Soldering
    
    Quentin Bolsee's avatar
    Quentin Bolsee committed
    
    
    Quentin Bolsee's avatar
    Quentin Bolsee committed
    We soldered the prototypes using stencils + solder paste and reflow, but I had inconsistent communication to the TMC. This might have happened after incorrect settings on the VCC jumpers.
    
    Quentin Bolsee's avatar
    Quentin Bolsee committed
    
    
    Quentin Bolsee's avatar
    Quentin Bolsee committed
    I decided to remove the driver with a hot air gun so I could inspect the solder underneath it:
    
    Quentin Bolsee's avatar
    Quentin Bolsee committed
    
    
    Quentin Bolsee's avatar
    Quentin Bolsee committed
    ![](images/solder.jpg)
    
    Quentin Bolsee's avatar
    Quentin Bolsee committed
    
    
    Quentin Bolsee's avatar
    Quentin Bolsee committed
    After aligning a new chip, I manually reflow it with the hot air gun.
    
    Quentin Bolsee's avatar
    Quentin Bolsee committed
    
    
    Quentin Bolsee's avatar
    Quentin Bolsee committed
    ![](images/solder_done.jpg)
    
    Quentin Bolsee's avatar
    Quentin Bolsee committed
    
    
    Quentin Bolsee's avatar
    Quentin Bolsee committed
    The final PCB is mounted on the back of a NEMA17 using long M3 screws. Note the VCC jumpers at the bottom.
    
    Quentin Bolsee's avatar
    Quentin Bolsee committed
    
    
    Quentin Bolsee's avatar
    Quentin Bolsee committed
    ![](images/pcb.jpg)
    
    Quentin Bolsee's avatar
    Quentin Bolsee committed
    
    
    Quentin Bolsee's avatar
    Quentin Bolsee committed
    ## Code
    
    Quentin Bolsee's avatar
    Quentin Bolsee committed
    
    
    Quentin Bolsee's avatar
    Quentin Bolsee committed
    The library [TMCStepper](https://github.com/teemuatlut/TMCStepper) provides a good interface for reading/writing the registers of the TMC. Here's a code snippet for setting up and reading the Stallguard values:
    
    Quentin Bolsee's avatar
    Quentin Bolsee committed
    
    
    Quentin Bolsee's avatar
    Quentin Bolsee committed
    ```c++
    #include <TMCStepper.h>
    
    Quentin Bolsee's avatar
    Quentin Bolsee committed
    
    
    Quentin Bolsee's avatar
    Quentin Bolsee committed
    #define PIN_DIAG           7 // diagnostic
    #define PIN_EN             6 // Enable (inverted)
    #define PIN_DIR           27 // direction
    #define PIN_STEP          28 // Step
    #define PIN_RX            29 // RX
    #define PIN_TX             0 // TX
    #define SERIAL_PORT Serial1  // Serial1
    #define DRIVER_ADDRESS 0b00  // MS1 and MS2 = address
    #define STALL_VALUE      50  // [0...255]
    
    Quentin Bolsee's avatar
    Quentin Bolsee committed
    
    
    Quentin Bolsee's avatar
    Quentin Bolsee committed
    #define MICROSTEPS       16
    
    Quentin Bolsee's avatar
    Quentin Bolsee committed
    
    
    Quentin Bolsee's avatar
    Quentin Bolsee committed
    #define R_SENSE 0.1f // 100 mOhm
    
    Quentin Bolsee's avatar
    Quentin Bolsee committed
    
    
    Quentin Bolsee's avatar
    Quentin Bolsee committed
    TMC2209Stepper driver(&SERIAL_PORT, R_SENSE, DRIVER_ADDRESS);
    
    Quentin Bolsee's avatar
    Quentin Bolsee committed
    void setup() {
      SERIAL_PORT.setTX(PIN_TX);
      SERIAL_PORT.setRX(PIN_RX);
      SERIAL_PORT.begin(115200);
    
    Quentin Bolsee's avatar
    Quentin Bolsee committed
    
    
    Quentin Bolsee's avatar
    Quentin Bolsee committed
      pinMode(PIN_DIAG, INPUT);
      pinMode(PIN_EN, OUTPUT);
      pinMode(PIN_STEP, OUTPUT);
      pinMode(PIN_DIR, OUTPUT);
    
    Quentin Bolsee's avatar
    Quentin Bolsee committed
    
    
    Quentin Bolsee's avatar
    Quentin Bolsee committed
      driver.begin();
      driver.toff(4);
      driver.blank_time(24);
      driver.rms_current(250); // in mA
      driver.microsteps(MICROSTEPS);
      driver.TCOOLTHRS(0xFFFFF); // 20bit max
      driver.semin(5);
      driver.semax(2);
      driver.sedn(0b01);
      driver.SGTHRS(STALL_VALUE); // Stallguard < threshold -> DIAG pin goes up
    }
    
    Quentin Bolsee's avatar
    Quentin Bolsee committed
    
    
    Quentin Bolsee's avatar
    Quentin Bolsee committed
    void loop() {
        // make motor turn, needed for valid Stallguard readings
        [...]
    
        // 9-bit Stallguard reading
        int value = driver.SG_RESULT();
    }
    ```
    
    Quentin Bolsee's avatar
    Quentin Bolsee committed
    
    
    Quentin Bolsee's avatar
    Quentin Bolsee committed
    ## Result
    
    Quentin Bolsee's avatar
    Quentin Bolsee committed
    
    
    Quentin Bolsee's avatar
    Quentin Bolsee committed
    ![](video/homing.mp4)