Control Stepper Motor with TMC 2208

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).

#define EN_PIN    13 								
#define STEP_PIN  54 								

#include <TMC2208Stepper.h>							
TMC2208Stepper driver = TMC2208Stepper(&Serial);														

void setup() {
	Serial.begin(115200);							
	while(!Serial);									

	pinMode(EN_PIN, OUTPUT);
	pinMode(STEP_PIN, OUTPUT);

	driver.pdn_disable(1);							
	driver.I_scale_analog(0);						
	driver.rms_current(500);						
	driver.toff(0x2);								

	digitalWrite(13, LOW);			
}

void loop() {
	digitalWrite(STEP_PIN, !digitalRead(STEP_PIN));
	delay(10);
}

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 ?

Thank you very much :smiley:

Hello albeseb, Good day.

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.

// Steppermotor Ansteuerung für bipolare Motoren mit A4988 Board
// Erste Version 18.04.2021

#define dirPin      2
#define stepperPin  4
#define ms1Pin      8
#define ms2Pin     12
#define ms3Pin     13

void setup() {
 pinMode(dirPin,      OUTPUT);
 pinMode(stepperPin,  OUTPUT);
 pinMode(ms1Pin,      OUTPUT);
 pinMode(ms2Pin,      OUTPUT);
 pinMode(ms3Pin,      OUTPUT);
 digitalWrite(ms1Pin,     LOW);
 digitalWrite(ms2Pin,     LOW);
 digitalWrite(ms3Pin,     LOW);
 digitalWrite(dirPin,     LOW);
 digitalWrite(stepperPin, LOW);
}

void loop(){
 step(true,10);  
 delay(1000);
 step(false,120); 
 delay(1000);
 step(true,500);  
 delay(1000);
 step(true,300); 
 delay(1000);
  step(true,10);  
 delay(1000);
 step(false,800);  
 delay(1000);
  step(true,10);  
 delay(1000);
 step(true,100); 
 delay(1000);

 
}

void step(boolean dir,int steps){
 digitalWrite(dirPin,dir);
 delay(50);
 for(int i=0;i<steps;i++){
   digitalWrite(stepperPin, HIGH);
   delayMicroseconds(1000);  
   digitalWrite(stepperPin, LOW);
   delayMicroseconds(1000);  
 }
}

Hope will work fine with your project.
Best regards Markus

1 Like

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

The TMC2208 can be controlled/configured by means of a serial line (UART). And that's what this lib does.

1 Like

so what are those commands? do they include defining the pins, setting direction and speed and # of step to rotate?

what controller function needs to be called in loop()?

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);
}
1 Like

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.

Yeah, that's what I'm doing for now.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.