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;
}
}
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.
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);
}
}
➜ 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() {}
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.