Hi. Using the tmc2209 stepper motor driver along with a nema 811 motor. PSU is 12v 2A
I have copied this code from somewhere else and tried to modify it to accept a pot input on A1. I want to control the motor speed via the pot.
This is the code:
#include <TMCStepper.h> // TMCstepper - GitHub - teemuatlut/TMCStepper
#include <SoftwareSerial.h> // Software serial for the UART to TMC2209 - https://www.arduino.cc/en/Reference/softwareSerial
#include <Streaming.h> // For serial debugging output - Streaming - Arduino Reference
#define EN_PIN 2 // Enable - PURPLE
#define DIR_PIN 3 // Direction - WHITE
#define STEP_PIN 4 // Step - ORANGE
#define SW_SCK 5 // Software Slave Clock (SCK) - BLUE
#define SW_TX 6 // SoftwareSerial receive pin - BROWN
#define SW_RX 7 // SoftwareSerial transmit pin - YELLOW
#define DRIVER_ADDRESS 0b00 // TMC2209 Driver address according to MS1 and MS2
#define R_SENSE 0.11f // SilentStepStick series use 0.11 …and so does my fysetc TMC2209 (?)
SoftwareSerial SoftSerial(SW_RX, SW_TX); // Be sure to connect RX to TX and TX to RX between both devices
TMC2209Stepper TMCdriver(&SoftSerial, R_SENSE, DRIVER_ADDRESS); // Create TMC driver
int potPin = A1; // Analog input pin for potentiometer
int accel;
long maxSpeed;
int speedChangeDelay;
bool dir = false;
//== Setup ===============================================================================
void setup() {
Serial.begin(115200); // initialize hardware serial for debugging
SoftSerial.begin(115200); // initialize software serial for UART motor control
TMCdriver.beginSerial(115200); // Initialize UART
pinMode(EN_PIN, OUTPUT); // Set pinmodes
pinMode(STEP_PIN, OUTPUT);
pinMode(DIR_PIN, OUTPUT);
digitalWrite(EN_PIN, LOW); // Enable TMC2209 board
TMCdriver.begin(); // UART: Init SW UART (if selected) with default 115200 baudrate
TMCdriver.toff(5); // Enables driver in software
TMCdriver.rms_current(500); // Set motor RMS current
TMCdriver.microsteps(256); // Set microsteps
TMCdriver.en_spreadCycle(false);
TMCdriver.pwm_autoscale(true); // Needed for stealthChop
}
//== Loop
void loop() {
accel = 10000; // Speed increase/decrease amount
maxSpeed = map(analogRead(potPin), 0, 1023, 0, 50000); // Map the potentiometer value to a motor speed
speedChangeDelay = 0; // Delay between speed changes
TMCdriver.shaft(true); // SET DIRECTION
for (long i = 0; i <= maxSpeed; i = i + accel){ // Speed up to maxSpeed
TMCdriver.VACTUAL(i); // Set motor speed
Serial << TMCdriver.VACTUAL() << endl;
delay(speedChangeDelay);
}
}
Currently the motor spins very slowly, but I don't really get any change in speed when turning the pot. Maybe I am, but it just might be so slow that I can't see any difference. At both extreme pot positions, the motor stops completely.
I have set Vref manually for the stepper driver, via the pot, but it seems it is being set in code, via
TMCdriver.rms_current(500); // Set motor RMS current" ? Could I delete that, and will it then revert back to the calibrated pot value on the stepper driver?
EDIT: Just changed the rms current from 500 to 2500, and the motor seemed to spin faster, but still no change when adjusting pot on A1
I am new to coding and want to learn. What am I doing wrong here?
Bring up arduino/referense/map and check what types it can use. I suspect map accepts ints and returns an integer. Max value of an int is 32767. Try a map limit of 25000. Then multiply the mapped value by 2.
Are You sure the driver is coded for a long type variable? Maybe You can use an unsigned int. Max is 65535 then.
I am not familiar with this driver or library. But setting the rms_current will probably set the max current for your driver. It should be set below the max current of your motor and below the max current of your power supply. It is not meant to set the speed.
@Railroader I can see in the code that I have microstepping set to 256, that might the culprit. I will see how I can lower that and if it fixes anything.
UPDATE: Setting it to 16 (was 265), def makes it go faster, but the pot still doesnt change speed.
Will look into how I can implement your solution. I am used to node based programming, so I don't even know how to multiply 2 numbers in c++ hehe... Will have to look into it.
Feel free to post the c++ code for what you are talking about railroder (or anyone else), but I will investigate in the meantime.
Just tested: Setting the maxspeed value higher here:
maxSpeed = map(analogRead(potPin), 0, 1023, 0, 50000);
Def. makes the motor go faster. I changed it from 50000, to 500000 (and extra zero at the end).
I just get pretty jittery behaviour sadly. Maybe it's because my psu cont keep up. Will try a 24v psu with higher amps as well.
Penetrate my post about ints and longs! Extending from 50000 to 500000 is a step in the wrong direction I'll say.
The more microsteps the slower it runs. The ,ibrary uses steps per second.
Did this: Changed long maxSpeed, to unsigned int maxSpeed. Also made map limit to 2500
I'm using a 10k Lin A pot.
After changing to 2500 the motor only runs at the pot extreme. I'm that much of a noob, that I don't know where and how to insert the code to multiply the maxSpeed, as suggested by @Railroader. Sorry about that.
This is the new code.
#include <TMCStepper.h> // TMCstepper - GitHub - teemuatlut/TMCStepper
#include <SoftwareSerial.h> // Software serial for the UART to TMC2209 - https://www.arduino.cc/en/Reference/softwareSerial
#include <Streaming.h> // For serial debugging output - Streaming - Arduino Reference
#define EN_PIN 2 // Enable - PURPLE
#define DIR_PIN 3 // Direction - WHITE
#define STEP_PIN 4 // Step - ORANGE
#define SW_SCK 5 // Software Slave Clock (SCK) - BLUE
#define SW_TX 6 // SoftwareSerial receive pin - BROWN
#define SW_RX 7 // SoftwareSerial transmit pin - YELLOW
#define DRIVER_ADDRESS 0b00 // TMC2209 Driver address according to MS1 and MS2
#define R_SENSE 0.11f // SilentStepStick series use 0.11 …and so does my fysetc TMC2209 (?)
SoftwareSerial SoftSerial(SW_RX, SW_TX); // Be sure to connect RX to TX and TX to RX between both devices
TMC2209Stepper TMCdriver(&SoftSerial, R_SENSE, DRIVER_ADDRESS); // Create TMC driver
int potPin = A1; // Analog input pin for potentiometer
int accel;
unsigned int maxSpeed;
int speedChangeDelay;
bool dir = false;
//== Setup ===============================================================================
void setup() {
Serial.begin(115200); // initialize hardware serial for debugging
SoftSerial.begin(115200); // initialize software serial for UART motor control
TMCdriver.beginSerial(115200); // Initialize UART
pinMode(EN_PIN, OUTPUT); // Set pinmodes
pinMode(STEP_PIN, OUTPUT);
pinMode(DIR_PIN, OUTPUT);
digitalWrite(EN_PIN, LOW); // Enable TMC2209 board
TMCdriver.begin(); // UART: Init SW UART (if selected) with default 115200 baudrate
TMCdriver.toff(5); // Enables driver in software
TMCdriver.rms_current(2500); // Set motor RMS current
TMCdriver.microsteps(256); // Set microsteps
TMCdriver.en_spreadCycle(false);
TMCdriver.pwm_autoscale(true); // Needed for stealthChop
}
//== Loop
void loop() {
accel = 10000; // Speed increase/decrease amount
maxSpeed = map(analogRead(potPin), 0, 1023, 0, 25000); // Map the potentiometer value to a motor speed
//speedChangeDelay = 0; // Delay between speed changes
TMCdriver.shaft(true); // SET DIRECTION
for (long i = 0; i <= maxSpeed; i = i + accel){ // Speed up to maxSpeed
TMCdriver.VACTUAL(i); // Set motor speed
Serial << TMCdriver.VACTUAL() << endl;
//delay(speedChangeDelay);
}
}