Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 8082

General • Pico2 and TMC2209 (c/c++)

$
0
0
Hi, i would really appreciate any help with regards to controlling a nematodes 17 stepper motor using tmc2209 stepper driver in the normal step/dir mode.

So i have MS1, MS2, EN, Dir pins on the tmc2209 put on low in the pico2, so 0.
And i am generating pio squarewave signals on to the step_pin. now when i run the following code with pio_freq = 5000 the stepper motor does one full step and then stops turning and vibrates, however if i pulse the EN pin at about 2hz or less the stepper motor continues to rotate without any issues. But if i increase the pio_freq to 400000 then the stepper motor turns without the need to pulse En pin, i just set the EN to 0 at this point and it works without issue.
The second problem is that i am not able to increase the speed at which the stepper motor turns, it always seems to be too slow, as i plan to connect it to a reducer in the future, so it has to turn quickly.
There are no issues with my wiring, as they are spot on and i have checked them about 10 times.
I am running the tmc2209 using a 5v input from 4 500mA rechargable AA batteries, as i dont have a better power source (could this be the reason?).

I am also not sure if there is a problem with my pio on the pico2.

Kindly advise.

my PIO code:-

Code:

.program Squarewave.wrap_target    set pins, 1 [19]    nop         [19]    nop         [19]    set pins, 0 [19]    nop         [19]    nop         [19].wrap% c-sdk {void Squarewave_program_init(PIO pio, uint sm, uint offset, uint pin, float div) {    pio_sm_config c = Squarewave_program_get_default_config(offset);    pio_gpio_init(pio, pin);    sm_config_set_set_pins(&c, pin, 1);    pio_sm_set_consecutive_pindirs(pio, sm, pin, 1, true);    sm_config_set_clkdiv(&c, div);    pio_sm_init(pio, sm, offset, &c);}%}
and my C code

Code:

#include "pico/stdlib.h"#include "hardware/pio.h"#include "hardware/gpio.h"#include "hardware/clocks.h"#include "Squarewave.pio.h"int main() {    gpio_init(22);    gpio_init(15);    gpio_set_dir(15, GPIO_OUT); //dir pin    gpio_init(14);    gpio_set_dir(14, GPIO_OUT);//ms1    gpio_init(13);    gpio_set_dir(13, GPIO_OUT);//ms2    gpio_init(12);    gpio_set_dir(12, GPIO_OUT);//enable    gpio_put(15, 1);    gpio_put(14, 0);    gpio_put(13, 0);    gpio_put(12, 0);    static const uint step_pin = 22;    static const float pio_freq = 400000;    // Choose PIO instance (0 or 1)    PIO pio = pio0;    // Get first free state machine in PIO 0    uint sm = pio_claim_unused_sm(pio, true);    // Add PIO program to PIO instruction memory. SDK will find location and    // return with the memory offset of the program.    uint offset = pio_add_program(pio, &Squarewave_program);    // Calculate the PIO clock divider    float div = (float)clock_get_hz(clk_sys) / pio_freq;    // Initialize the program using the helper function in our .pio file    Squarewave_program_init(pio, sm, offset, step_pin, div);    // Start running our PIO program in the state machine    pio_sm_set_enabled(pio, sm, true);    // Do nothing    while (true) {        sleep_ms(1000);    }}
Thanks in advance.

Statistics: Posted by Joraspberry — Fri Oct 24, 2025 1:38 pm



Viewing all articles
Browse latest Browse all 8082

Trending Articles