Hello,
I try to control a Stepper Motor with a TMC 2208 Stepper Motor driver. On the Internet I found this code. (It works the stepper rotates continuous).
The Problem is I don´t understand the code. The Stepper rotates only when the Step_Pin switchs between high and low in the loop, but why is that ?
Can someone explane me the lines which start with driver. ?
I want to use the Code to Control the Stepper so it only rotates when something happens or it rotates only a fixed number of Steps. How can I do this ?
Do I have to use this library ? Or is there a way without it ?
I did some experiments with an Stepper motor too. I used the driver ASIC A4988. This ASIC is quite same like the one you have used (TMC 2208). I tried with the code below. You have to adapt the pins to your layout and then it shoud work fine. Then you can give some values for stepping with the "step" comand.
i would have thought the definition for the driver specifies the pins and has functions to rotate the motor a specified # of steps in a specified direction
As far as I can see from this link, this lib doesn't care about the step and dir pins. It configures the driver ( microsteps, motor current and much more ). And you can interrogate the actual internal state of the driver, e.g. the actual microstep position. To move the stepper you usually have to create step pulses, but this lib doesn't care about this - that's why this is done in the example in loop apart from the lib.. ( But there is a command to rotate the stepper endlessly with a given speed independent from step and dir pins ).
You must do this by yourself in the sketch or additionally use one of the 'usual' stepper libraries, e.g. AccelStepper or my MobaTools library.
You don't need the library, no. You can hook up the tmc2208 exactly like an A4988. This is how I have one connected now. So:
Pin 4 to DIR
Pin 5 to STEP
Pin 6 to EN
CLK and PDN are jumpered together - this is SLP and RST on the A4988
VIO to 5V on Arduino
VM is 12V input
Should be it aside from ground and the four motor connections. Hopefully this is what you have, or close enough. Then this code should be easy to understand. I have to do about 1600 steps for one complete revolution. A good tip is to put a piece of tape on the motor shaft, so you have a little flag. Makes it easy to see where it starts/stops.
void setup()
{
pinMode(6, OUTPUT); //Enable
pinMode(5, OUTPUT); //Step
pinMode(4, OUTPUT); //Direction //HIGH = CC / LOW = Clockwise
digitalWrite(6, LOW);//LOW is Enable - if this is high the motor will be disabled
setMotorDirection(true); //clockwise
}
void loop()
{
doMotorStep(); //just turn continuously
}
//Pin 4 is Direction
void setMotorDirection(bool clockwise)
{
delay(500);//ms
if (clockwise) {
digitalWrite(4, LOW);//clockwise - LOW for 2208 / HIGH for 4988
} else {
digitalWrite(4, HIGH);//counter clockwise - HIGH for 2208 / LOW for 4988
}
digitalWrite(6, LOW);//Enable - Pin 6 is Enable/Disable
delay(500);//ms
}
//Pin 5 is Step
//Does one step in the currently set direction
void doMotorStep()
{
digitalWrite(5, HIGH);
delayMicroseconds(450); //lower = faster, but louder
digitalWrite(5, LOW);
delayMicroseconds(450);
}
This is really helpful! I'm new to arduino and stepper motors/drivers, and the code that you provided is super enlightening.
I am trying to set up the code so that a push button changes direction from clockwise to counter clockwise. Would you be able to show how to add that into the code that you provided?
I am trying to use a TMC2298 as well. But in stand-alone mode with none of the MS pins configured, it defaults to 8 microsteps since both MS1 and MS2 have PD resistors and when they are low, that is the setting.
What I can't figure out is how to get the driver to run in regular, full-steps. It almost seems like that isn't possible with this driver. has anyone else had any luck with this?
I think you have to use the serial configuration to set full steps. To do this is getting away from the simplicity of running the TMC2208 like a standard step/dir driver.
I would recommend that you run in half step mode unless you have some over riding need to run in full steps. If so, explain more about that constraint.