Checking stepper motor steps

I have a stepper motor with a 30:1 reduction gearbox . I have set the steps on the driver to 100 steps per revolution, at least I thought I had. I have attached two pieces of code that I used to test the 100 step setting. In the first code, using the accelstepper library, I included " myStepper.moveTo(3000);" i’e 3000 steps divided by the gearbox ratio of 30 gives one revolution of the gearbox output shaft. So with this code the gearbox output shaft turns one revolution as expected. With the second code, using no library, I have stated ‘100 steps * 30’ in the code but the gearbox output shaft only turns a half revolution, suggesting the driver is set to 200 steps. I’m probably making a stupid mistake in the coding somewhere but I cannot figure it out. I tried using the “stepper’h” library in another piece of code and that suggested the driver steps were set to 800 steps but I have not included that code here.

// Include the AccelStepper Library
#include <AccelStepper.h>

// Define pin connections
const int dirPin = 8;
const int stepPin = 9;

// Define motor interface type
#define motorInterfaceType 1

// Creates an instance
AccelStepper myStepper(1, 9, 8);

void setup() {
  // set the maximum speed, acceleration factor,
  // initial speed and the target position
  myStepper.setMaxSpeed(1000);
  myStepper.setAcceleration(1000);
  
  myStepper.moveTo(3000);
  myStepper.setSpeed(1000);
}

void loop() {
  // Change direction once the motor reaches target position
  if (myStepper.distanceToGo() == 0) 
    myStepper.moveTo(-myStepper.currentPosition());

  // Move the motor one step
  myStepper.run();
}

“Second code”

}[/code]

// Define stepper motor connections and steps per revolution:
#define dirPin 8
#define stepPin 9
#define stepsPerRevolution 100


void setup() {
  // Declare pins as output:
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
}

void loop() {
  // Set the spinning direction clockwise:
  digitalWrite(dirPin, HIGH);

  // Spin the stepper motor 1 revolution slowly:
  for (int i = 0; i < stepsPerRevolution *30; i++) {
    // These four lines result in 1 step:
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(400);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(400);
  }

  delay(1000);

  
}

brid:
With the second code, using no library, I have stated '100 steps * 30' in the code but the gearbox output shaft only turns a half revolution,

You don't seem to have posted the second program.

...R
Stepper Motor Basics
Simple Stepper Code

I have now added the second code

In your second program try making the motor move much more slowly. At the moment you are doing over 1000 steps per second. Try it at 100 steps per second.

...R

OK changed the second code as follows

// Spin the stepper motor 1 revolution slowly:
for (int i = 0; i < stepsPerRevolution *30; i++) {
// These four lines result in 1 step:
digitalWrite(stepPin, HIGH);
delayMicroseconds(5000);
digitalWrite(stepPin, LOW);
delayMicroseconds(5000);

Still the same result, a half revolution.

What happens if you delete these two lines in the AccelStepper program?

  if (myStepper.distanceToGo() == 0) 
    myStepper.moveTo(-myStepper.currentPosition());

By the way, rather than this

  digitalWrite(stepPin, HIGH);
    delayMicroseconds(5000);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(5000);

it better reflects how a stepper motor works if you do it like this

  digitalWrite(stepPin, HIGH);
    delayMicroseconds(10); // a short step pulse
    digitalWrite(stepPin, LOW);
    delayMicroseconds(10000); // the interval between steps determines the speed

...R

If I delete the two lines in the first programme as you suggest then the shaft moves half a revolution and then stops. That is odd, I thought it would revolve one revolution and stop. I made the change in the second progamme as you suggested but the motor would not run.

brid:
If I delete the two lines in the first programme as you suggest then the shaft moves half a revolution and then stops. That is odd, I thought it would revolve one revolution and stop.

I don't think it is odd. Now your two program behave the same with 3000 steps. If that only moves the output shaft 180° then you need 6000 steps for a full 360°.

I made the change in the second progamme as you suggested but the motor would not run.

Please post the actual code that you tried. There is probably a small error somewhere.

...R

I'm confused by your reply. Let me go back to the start to explain my reasoning. Please excuse my simplistic approach.

I have set the driver to 100 steps.
So in the first program, I ask the motor to move to 3000 steps, so the motor revolves 3000/100 =30
.
But I have a 30:1 gearbox attached to the motor so the gearbox shaft takes one revolution as expected. That I understand.

In the second program I ask for the motor to turn 100*30, i,e 3000 steps which should give me one revolution of the output shaft but it only turns one half of a revolution.

So forgive me, I do not understand why deleting those two line should cause the motor to only turn a half revolution.

It appears to me the 'accelstepper' program is producing the correct response. If I change the steps to 6000 the gearbox output shaft turns 2 revolutions, which is what I would expect with the driver set to 100 steps.

I was using a Nano before but I have now built the circuit with a Mega 2560. This has not changed anything but it has raised another question. All my PWM pins are used up with a LCD shield, so I have used pins 26 and 27 for the DIR and STEP and the motor does seem to work OK, but I read that I should be using PWM pins. Is this a proble? Is this a question I should raise in another post?

brid:
I'm confused by your reply. Let me go back to the start to explain my reasoning. Please excuse my simplistic approach.

I have set the driver to 100 steps.
So in the first program, I ask the motor to move to 3000 steps, so the motor revolves 3000/100 =30
.
But I have a 30:1 gearbox attached to the motor so the gearbox shaft takes one revolution as expected. That I understand.

I don't have your hardware so I can't replicate your experience.

I was working back from the second program which is very straightforward and to my mind gives the correct result - albeit not what you were expecting. It seemed to me that the two lines I suggested you remove from your first program are confusing the matter - effectively giving a wrong result which gives the illusion of being correct.

Most stepper motor armatures are designed for 200 steps per revolution of the motor - maybe that is where the problem arises, You have not yet posted a link to the datasheet for your motor.

...R

You have stated something there that makes me think the problem might be the driver and motor combination. The motor spec states the full step is 1.8 deg. The driver has a setting for 100 steps. I set the driver to 100 steps but is the motor capable of producing 100 steps. Is that that the problem? OK the first program using “accelstepper” works OK with the driver set to 200 steps and “myStepper.moveTo(6000);” the output shaft takes one revolution. However the second simple program with a sated 200 x 30 steps only turns the gearbox output shaft a 1/2 revolution.

17LS13-0404E-300G.pdf (158 KB)

ISD02.pdf (705 KB)

Sorry I sent you the wrong motor sheet. The correct one is attached

17HS15-1684S-HG30.pdf (136 KB)

brid:
The motor spec states the full step is 1.8 deg.

If there is 1.8° per step then there are 200 steps per revolution because 1.8 * 200 = 360

Which is the case with both motors.

...R

PS ... if possible post links to the datasheets so we don't have to download PDF files.

OK Links attached.

Just in case of doubt, I had downloaded your attachments before posting Reply #12.

My suggestion about links was for future situations and other readers - and thank you for providing the links.

...R