The steps are not counted in the serial monitor

Hello.

I have an Arduino code here that works very well so far.

A stepper motor (Moon Stepping Motor C17HD2024N) is controlled via a potentiometer so that I can control the speed of rotation. I use an Arduino Mega 2560 and the stepper motor is driven with 24V.

The only thing that doesn't work is the display in the serial monitor. I want the number of total revolutions to be "calculated". When the motor starts to turn, regardless of where the potentiometer is positioned, only the "0" is output in the serial motor.

Could someone please tell me where I am ignoring the error?

#include <AccelStepper.h>

// Pin-Definitionen für den TMC2208
#define STEP_PIN 2
#define DIR_PIN 3

// Pin-Definitionen für das Potentiometer
#define POT_PIN A0

// Erstelle ein AccelStepper-Objekt
AccelStepper stepper(AccelStepper::DRIVER, STEP_PIN, DIR_PIN);

// Variable zur Zählung der Steps und Schrittauflösung
volatile long totalSteps = 0;
const int stepsPerRevolution = 200; // Schrittzahl pro Umdrehung

void setup() {
  Serial.begin(9600);

  // Setze die maximale Geschwindigkeit und den Startpunkt des Motors
  stepper.setMaxSpeed(5000); // Maximale Geschwindigkeit in Schritten pro Sekunde
  stepper.setSpeed(500); // Startgeschwindigkeit
  
  // Initialisiere das Potentiometer
  pinMode(POT_PIN, INPUT);
}

void loop() {
  // Lies den Potentiometerwert
  int potValue = analogRead(POT_PIN);
  
  // Konvertiere den Potentiometerwert in eine Geschwindigkeit
  int speed = map(potValue, 0, 1023, 0, 5000);
  
  // Setze die Geschwindigkeit des Motors
  stepper.setSpeed(speed);
  
  // Bewege den Motor einen Schritt vorwärts
  stepper.runSpeed();

  // Überprüfe, ob eine Umdrehung abgeschlossen ist
  if (stepper.distanceToGo() == 0) {
    totalSteps += stepsPerRevolution;

    // Gib die Anzahl der Steps und Umdrehungen im seriellen Monitor aus
    Serial.print("Steps: ");
    Serial.print(totalSteps);
    Serial.print(", Umdrehungen: ");
    Serial.println(totalSteps / stepsPerRevolution);
  }
}

Greetings Jan

with runSpeed() you don't have a destination. Not sure what stepper.distanceToGo() would do.

I have corrected this to

if (stepper.currentPosition() == 0)

Now the serial monitor only shows the content after uploading, then the display stops.

Your comment is not really true. runSpeed() may create a step, but it does not do it with each call - depending on the speed. Only if runSpeed() returns true a step was created.

currentPosition() returns what its name says: the current position in steps. This is only 0 at the start of the sketch and is incremented with every step runSpeed creates. You can use this to compute the actual revolutions.

1 Like
    // Bewege den Motor einen Schritt vorwärts
    //stepper.runSpeed();

    // Überprüfe, ob eine Umdrehung abgeschlossen ist
    if (stepper.runSpeed() ) {
        totalSteps = stepper.currentPosition();
        if (totalSteps % stepsPerRevolution == 0) {
            // Gib die Anzahl der Steps und Umdrehungen im seriellen Monitor aus
            Serial.print("Steps: ");
            Serial.print(totalSteps);
            Serial.print(", Umdrehungen: ");
            Serial.println(totalSteps / stepsPerRevolution);
        }
    }

Hi @MicroBahner !

Thank you very much!

Currently, the counter in the serial monitor starts counting automatically, regardless of whether the motor is running or not.

Here is the modified code:

#include <AccelStepper.h>

// Pin-Definitionen für den TMC2208
#define STEP_PIN 2
#define DIR_PIN 3

// Pin-Definitionen für das Potentiometer
#define POT_PIN A0

// Erstelle ein AccelStepper-Objekt
AccelStepper stepper(AccelStepper::DRIVER, STEP_PIN, DIR_PIN);

// Variable zur Zählung der Steps und Schrittauflösung
volatile long totalSteps = 0;
const int stepsPerRevolution = 100; // Schrittzahl pro Umdrehung

void setup() {
  // Setze die maximale Geschwindigkeit und den Startpunkt des Motors
  stepper.setMaxSpeed(5000); // Maximale Geschwindigkeit in Schritten pro Sekunde
  stepper.setSpeed(500); // Startgeschwindigkeit
  
  // Initialisiere das Potentiometer
  pinMode(POT_PIN, INPUT);

  // Initialisiere seriellen Monitor
  Serial.begin(9600);
}

void loop() {
  // Lies den Potentiometerwert
  int potValue = analogRead(POT_PIN);
  
  // Konvertiere den Potentiometerwert in eine Geschwindigkeit
  int speed = map(potValue, 0, 1023, 0, 5000);
  
  // Setze die Geschwindigkeit des Motors
  stepper.setSpeed(speed);

  // Überprüfe, ob eine Umdrehung abgeschlossen ist
  if (stepper.runSpeed() ) {
    totalSteps = stepper.currentPosition();
    if (totalSteps % stepsPerRevolution == 0) {
    // Gib die Anzahl der Steps und Umdrehungen im seriellen Monitor aus
       Serial.print("Steps: ");
       Serial.print(totalSteps);
       Serial.print(", Umdrehungen: ");
       Serial.println(totalSteps / stepsPerRevolution);
    }
  }
}

I'm not quite shure what you mean with that. All printing is within a block that is only executed when a step is done. Is there anything else that prevents the motor from running?

1 Like

That's right, normally the code should work, and it does!

In this line, I have now changed the steps from "200" to "3200".

const int stepsPerRevolution = 3200;

In my tests, the motor now rotates exactly 360 degrees (i.e. one revolution) and "one revolution" is also displayed in the serial monitor.

I find it confusing that the revolution is also written in the serial monitor, even if I switch off the 24V supply and the motor does not rotate at all.

If you simply switch of the power of the stepper your sketch doesn't know anything about that. It still creates steps. And that may be even bad for your driver.
Why do you switch of the power source? if you don't want the stepper to consume power, you could simply disable the driver ( which are you using? ) - and that could be done by the sketch.

Hi @MicroBahner ...

The shutdown was not deliberately forced. I did not disconnect 24V when the motor was running and the code was active.

When I ran the code, I disconnected the USB connection and the 24V supply was also switched off.

After the Arduino was started up and the potentiometer was at "0", I just forgot to switch on the 24V supply. After I turned up the potentiometer, I noticed that the counter in the serial monitor was counting up but the motor was not turning.

I would like to add something on this subject. I made a mistake in the driver module. First I tested with the TMC2208 and then I did the final tests with the TMC2130.

The TMC2130 data sheet shows that 16 steps are implemented in the "open state" of CFG1 and CFG2.

I wanted to add this information. The stepper motor above normally has 200 steps and these were also stored in the code. The pins CFG1 and CFG2 are connected to GND and there are only 200 steps.

Thanks again to @MicroBahner

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