Stepper motor code Problem

Hello:

I am using the following stepper motor - nema 17 motor 8HS11-0204S, a adafruit TMC2209 Stepper Motor Driver Breakout Board, and a Arduino Mega 2560 R3.

The code below is the new code I have been working on.

It does work (the wheel does one full rotation pauses and then rotates 5 more times in a row - no pause). I want the wheel to do 5 full rotations.

there is a flaw in the logic that I don't get.

#include <Stepper.h>

const int stepsPerRevolution = 6400;  // change this to fit the number of steps per revolution
// for your motor

// initialize the stepper library on pins 10 and 11:
Stepper myStepper(stepsPerRevolution, 10, 11);

boolean running = true;  // Global variable

void setup() {
  // set the speed at 60 rpm:
  myStepper.setSpeed(60);
  // initialize the serial port:
  Serial.begin(9600);
}
int i = 0;

void loop() {
  if (!running) {
    return;  // Exit loop() if running is false
  }

  // Your main loop logic here
  myStepper.step(-stepsPerRevolution);
  delay(2);
  i++;
  Serial.println(i);

  // Example condition to stop the loop
  if (i == 5) {
    running = false;
  }
}

  • Always show us a good schematic of your proposed circuit.
    Show us good images of your ‘actual’ wiring.

  • These are not a problem, however, get into the habit. :thinking:

if (i == 5) {
verses
if (i >= 5) {
. . .

if (!running) {
verses
if (running == false) {  // less cryptic

There is a green wire near the yellow but it's hard to see

The layout is the following -

Arduino Mega 2560 R3 that is connected to a adafruit TMC2209 Stepper Motor Driver Breakout Board (No kidding that its the full name - From now on I will be calling it the TMC2209 Board)

Pin D 10 is connected to the Dir on the TMC2209 Board

Pin D 11 is connected to the STEP on the TMC2209 Board

The +5 on the Mega is connected to the VDD on the TMC2209 Board

The GND right above Pin D13 on the Mega is connected to the GND on the TMC2209 Board.

The + & - on the TMC2209 Board is connected to my HM310 power supply which is supplying 12V and 1.5 Amps.

2B, 2A, 1A, 1B on the TMC2209 Board are connected to a nema 17 motor 8HS11-0204S

When the program exits void loop(), then void loop() just gets called again automatically. This is the underlying code that actually runs void loop():

for (;;) {
    loop();
    if (serialEventRun) serialEventRun();
}

So once running becomes false, the MCU is stuck going around void loop() endlessly anyway. On a micro-controller board there is nowhere really to exit to anyway, although the micro-processor can be reset or halted.

Since you have the stepper motion and i++ before the IF condition, your stepper will run for i values of 0,1,2,3,4 and 5, hence running 6 times. Only when i becomes 5 after the 6th rotation, does running become false.

If you want a fixed number of rotations it might be simpler to just do something like:

int rotations = 5;
int i = 0;

void loop() {
  if (i<rotations> {
    myStepper.step(-stepsPerRevolution);
    delay(2);
    i++;
    Serial.println(i);
  }
}

(Disclaimer: I haven't tested it!)

However, in reality I expect you will want to put that in a function and call it with the required number of steps or rotations. Not quite sure why there is a pause after the first rotation.

I found a typo in your code and corrected it.

Below is the latest code I used and it still gives me the exact same problem.

#include <Stepper.h>

const int stepsPerRevolution = 6400;  // change this to fit the number of steps per revolution
// for your motor

// initialize the stepper library on pins 10 and 11:
Stepper myStepper(stepsPerRevolution, 10, 11);

boolean running = true;  // Global variable

void setup() {
  // set the speed at 60 rpm:
  myStepper.setSpeed(60);
  // initialize the serial port:
  Serial.begin(9600);
}

int rotations = 5;
int i = 0;

void loop() {
  if (i < rotations) {
    myStepper.step(-stepsPerRevolution);
    delay(2);
    i++;
    Serial.println(i);
  }
}

I prefer

if (not running) { // much better :) 
1 Like

Verses v. Versus : )

1 Like

if you really want the code to do 5 rotations and then nothing, may be you can put everything in the setup and keep the loop empty

crude approach where you give sequential instructions.

#include <Stepper.h>
const int stepsPerRevolution = 6400;
Stepper myStepper(stepsPerRevolution, 10, 11);

void setup() {
  myStepper.setSpeed(60);

  myStepper.step(-stepsPerRevolution);
  delay(2);

  myStepper.step(-stepsPerRevolution);
  delay(2);

  myStepper.step(-stepsPerRevolution);
  delay(2);

  myStepper.step(-stepsPerRevolution);
  delay(2);

  myStepper.step(-stepsPerRevolution);
  delay(2);

}

void loop() {}

now you notice you are repeating instructions, so you can use a for loop

#include <Stepper.h>
const int stepsPerRevolution = 6400;
const int numRevolutions = 5;
Stepper myStepper(stepsPerRevolution, 10, 11);

void setup() {
  myStepper.setSpeed(60);

  for (int i = 0; i < numRevolutions; i++) {
    myStepper.step(-stepsPerRevolution);
    delay(2);
  }
}

void loop() {}

That being said, waiting 2ms is invisible to the human eye... If you wanted to wait 2 seconds, then use 2000 instead of 2 in the delay()

PS/ are you sure about this 6400 steps Per Revolution?

"If you don't like the answer, change the question."
Captain Kirk.

I tried the crude sequential instructions and got the same thing. I think the library is the problem, but thats just a guess.

I just noticed you said

➜ you don't use that one with the Stepper library.

The Arduino Stepper library is designed for directly driving stepper motor coils through two or four digital output pins connected to a basic H-bridge driver (like ULN2003 or L298N). It manually sequences the coil energizing pattern in software.

The TMC2209 is a step/dir driver, meaning it expects two signals:

  • STEP: a pulse for each step
  • DIR: a high or low signal for direction

To use a TMC2209 with an Arduino, you should use a library like AccelStepper or just manually toggle a digital output for the STEP pin while setting the DIR pin as needed (as per the example from their tutorial )

may be something like that will do (install the AccelStepper library)

#include <AccelStepper.h>

const int stepsPerRevolution = 6400;
const int numRevolutions = 5;

AccelStepper stepper(AccelStepper::DRIVER, 10, 11);

void setup() {
  stepper.setMaxSpeed(800);
  stepper.setAcceleration(400);

  for (int i = 0; i < numRevolutions; i++) {
    stepper.moveTo(-stepsPerRevolution);
    stepper.runToPosition();
    delay(2); // you'll need sharp eyes to detect a 2ms pause...
  }
}

void loop() {}

1 Like

I contacted Adafruit this morning and the following is for them -

"The Stepper.h library does support 2-pin 'step & direction' drivers. But it is a rather basic library. The AccelStepper library would be a better way to go."

I would like to say thank you to everyone in the forum who tried to help me.