Stepper motor makes linear deviation, advice on project needed

The whole construction of the project is as follows: Arduino Uno, Capacitive Sensor, JoyIt SBC-MotoDriver 2 and the QSH5718-56-28-126 stepper motor. The motor moves a metal plate, which is connected through a spindel, to a target position entered by the user. The plate should move 5 millimeters per revolution, so 1 mm for 40 steps. The motor is connected to the MotoDriver 2, which is connected to the Arduino. My problem is, that the more distanced the target position is, a bigger deviation from that target position occures. The range is from 0 to 1000 millimeters. The motor/ the plate is accurate at a target position of 100 millimeters. At 250 millimeters, the plate moves 1 millimeter too short, at 500 millimeters the deviation is 2 millimeters too too short and at 1000 millimeters the error is 3 millimeters too short. This is part of a project from someone else, which I have to optimize. So I tried to write a new code myself for controlling the Plate (which is the one above). For that, I tried to write the code in a way that person did. So I did not use the AccelStepper library as I have seen in every Stepper Motor guide now. My code is as follows:

int motorSpeed = 6;
int CalibrSensor = 5;

const in stepsPerRevolution = 200;
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);
unsigned int stepCount = 0;
int dirStep = 1;    //backwards
int mdirStep = -1;  //forward

const int stepsPerMillimeter = stepsPerRevolution / 5;  //5 millimeters per 200 steps -> 1 millimeter per 40 steps
long int targetPosition = 0;                            //desired location in Steps, initiated as 0 for calibrating
long int stepsToMove = 0;                               //Steps required to reach target position

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(CalibrSensor, INPUT);

  //Calibration of Testbench, set zero point/starting point
  //CalibrSensor goes to LOW, when Plate is on zero point
  //Plate moves backwards, until zero point is reached
  while (digitalRead(KaliSensorIn) == HIGH && targetPosition == 0) {
    myStepper.step(dirStep);
    delay(motorSpeed);
    stepCount--;
  }

  //Turn off current
  digitalWrite(8, LOW);
  digitalWrite(9, LOW);
  digitalWrite(10, LOW);
  digitalWrite(11, LOW);

  stepCount = 0;
  Serial.println("Testbench calibrated. Starting Point set.");
}

void loop() {

  while (stepCount != targetPosition) {  //loop, as long as targetPosition is not reached
    stepsToMove = targetPosition - stepCount;
    if (stepsToMove > 0) {      //move forward, if targetPosition is in postive direction
      myStepper.step(mdirStep);
      delay(motorSpeed);
      stepCount++;
    } else {                    //move backward, if targetPosition is in postive direction
      myStepper.step(dirStep);
      delay(motorSpeed);
      stepCount--;
    }
  }

  Serial.print("Step count at current destination: ");    //print current stepCount after reaching destination
  Serial.println(stepCount);
  Serial.println("Enter a new target position in mm: ");  //ask for new target position in mm through Serial Monitor
  while (Serial.available() == 0) {                       //wait for user input and do nothing
  }
  if (Serial.available() > 0) {           //if user entered target position
    targetPosition = Serial.parseInt();   //get target position as an integer
  }
  targetPosition = targetPosition * stepsPerMillimeter;   //convert target position to steps needed for reaching position
  Serial.print("New Target Position: ");                  //print new target position
  Serial.println(targetPosition);
}

My guess is, that the motor looses steps with greater distance and thus the plate does not move far enough to the target position. But I can't seem to find the reason for that in my program, so it could be a hardware problem. The cooling body on the MotoDriver does get really hot, so that you can't touch it with bare hands. But the motor itself stays cool, so I wasn't sure if that was the problem. As I am not an expert at this, more of a beginner/intermediate, I was hoping for answers to the problem or possible solutions, recommendations or tips for the project. I would be more than happy for every form of help, if there is any more information needed I will provide it.

5mm in one revolution is a lot, it is necessary to reduce at least 10 times

Your driver is NOT a stepper motor driver. IT is a regular motor driver being used with a stepper motor. Test with a real stepper motor driver with the current limit set for your stepper motor.
With a real driver you can experiment with fractional steps.

I do not see where you project has any homing function, so you cannot count steps from a known zero position.

1 Like

What is attached to the motor shaft? How do you translate rotary motion to linear motion?

An L298 brushed DC motor driver for a 2.8Amp stepper?
Never going to work.
Try a current controlled driver, like the TB6600, with a 24volt supply (or 12volt, minimum).
Leo..

The else causes the stepper to take a step backward at the end, then stepCount = -1 then,

if (stepsToMove > 0) { // returns false.

Now, since:

while (stepCount != targetPosition) {

Will never be false, the while loop keeps looping, doing a backward step every Motorgeschw millisseconds.

Do the while loop in your head with a target position of 2.

@JCA34F

I am really grateful for your help. I have to excuse myself, as I translated the code from German to English, I made some silly mistakes in the while loop. As you mentioned, the while loop makes no sense in that way and loops infinitely. I now edited my post and corrected the loop to the way it should be. With a target position of 2, the motor moves 2 steps forward, incrementing stepCount to 2. This should then make the condition of the loop false, as stepCount now equals targetPosition. It could very well still be a mistake in the code, which I just don't seem to notice. So I am more than thankful for any more of your advice!

Tbh honest I am not sure if work related questions are allowed here. I would want to send a picture of the whole construction, to make easier to understand. But as this is work related, I unfortunately can not send a picture and have to keep as general as possible. I understand this may make it really hard for other people to understand. To translate the rotary motion to linear motion, a spindle is attached to the shaft of the motor. The spindle on the other hand move the metal plate, so with each revolution of the spindle (which should equal one revolution of the motor) the plate moves 5 millimeters.

Sorry, first post in this forum so I just noticed how to cite and mention people.

First of all, thank you for the advice! As I am new to stepper motors, I wondered if the driver is suited for the application, as it is not possible to configure the step resolution with it. So using a driver like a A4988 and using for example a quarter-step resolution could maybe fix the problem? On the other hand, does the speed of the motor automatically slow down as a result of the higher resolution, because it can now "move more steps"? Speed is not a key factor in this project, but I was just wondering.

My tought process for the zero position: In the setup, I let the plate move backwards until it hits the calibration sensor. The calibration sensor is just a capacitive sensor, sending a binary signal. So if the plate is in very close proximity to the capacitive sensor, the sensor goes to LOW. This breaks the loop, after which the current to the motor gets turned off. The stepCount gets set to 0, so whenever I move one step forward, the stepCount goes up to 1 and vice versa for backwards movement. If I moves 20 steps forward and then again 20 steps backwards, I would arrive at my zero point/starting position.

Sorry, I forgot to mention the supply voltage and current... The supply voltage for the motor driver is set to 10volt with a maximum current of 2amps. Could there be a problem with the supply or would it simply just be better to use a other driver?

No, the A4988 is a ~1Amp driver, and you have a 2.8 Amp motor.
You need a stepper driver that can be set to close to 2.8Amp, like the TB6600.
A 24volt supply will give you the ability for sustained torque at higher speeds.
A 12volt supply could be ok for less demanding setups.

You have a 0.9 ohm motor connected to a 10volt supply.
That could in theory draw 10/0.9 = 11 Amp per winding.
What do you think will go up in smoke first. The supply, the 1Amp L298 or the 2.8Amp motor.
Leo..

Now I understand the need for the other driver, thank you so much! Because of your comment I stumbled upon the Stepper Motor Basics Guide, which definitely cleared a lot of basic questions for me. This may be a dumb question, but how exactly does the low current result in errors? Does a low current not only result in a low torque, meaning that in this case, the motor would just have a harder time moving the plate?

Not sure what you mean.
A current controlled driver controls the current through the motor coils.
At higher speeds a higher supply voltage is needed for that to work.
So if you need torque at high speed, you have to make sure the driver has that voltage available.
Leo..

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