Stepper motor speed

I’ve just started to work with stepper motors. For the most part my code works well except when I change the speed of the stepper. I’m either not understanding the function or not doing some correctly, both very real possibilities.

I’m using a Mega with a bi polar motor controlled by a L298N. The code increments the motor a defined number of steps, delays a set time, then increments the same number of steps. The motor has 200 steps per revolution. If I enter 20 steps to increment 10 times with a speed of 64 it will go 1 revolution. If I change the speed to 256 it only goes about 80%

The sketch is below. When you open the serial monitor you are prompted to enter the number of steps, dwell time between steps, the number of times to increment and the speed. There are 4 commands to control the cycle that you enter on the serial monitor:
s = start cycle
p = pause
r = resume
e = end

Is there a limit to the speed? If so is that a motor specific value? Is it a limit of the L298N?

There is one other seemingly inconsistent problem, the first increment doesn’t go the full number of steps, most times it does but I have notice enough times that it doesn’t

Thanks
John

[code]
// stepper motor control code
// Copyright Process Services 2020

#include <Stepper.h> // Include stepper motor library

int led = 13; // set led to pin 13
String dataIn; // string from operator
char d1;        // character from string
String x;     // String from operator


// variables for test cycle
int incr = 0;  // number of poles per increment
int poles = 10;  // total poles incremented
int dwell = 1000;  // dwell time between steps
int totSteps = 10; // total number of steps
int stepSpeed = 64; // set speed of stepper // 1 min//64 1rpm //128 2rpm //ENTER 4100 FOR 1 rev OF THE ROLLER
int runCycle = 0; // run cycle enable
int stepCount = 0; // current step counts
int i = 0;


// change this to the number of steps on your motor
#define STEPS 200 // Bob = 32
// create an instance of the stepper class using the steps and pins
// Stepper stepper(STEPS, 8, 10, 9, 11); // Bob's stepper
Stepper stepper(STEPS, 8, 9, 10, 11); // my stepper


void setup() {
  Serial.begin(9600);
  pinMode(led, OUTPUT); // set led pin to an output

  Serial.println("Enter number of steps");
  while (Serial.available() == 0) {} // wait for number of steps to be entered
  poles = Serial.parseInt(); // set steps to entered value
  Serial.read(); // clear serail buffer

  Serial.println("Enter Dwell Time in milliseconds");
  while (Serial.available() == 0) {} // wait for Dwell Time to be entered
  dwell = Serial.parseInt(); // set steps to entered value
  Serial.read(); // clear serial buffer

  Serial.println("Enter Number of Increments");
  while (Serial.available() == 0) {} // wait for Dwell Time to be entered
  totSteps = Serial.parseInt(); // set total number of steps to entered value
  Serial.read(); // clear serial buffer

  Serial.println("Enter Speed");
  while (Serial.available() == 0) {} // wait for Dwell Time to be entered
  stepSpeed = Serial.parseInt(); // set total number of steps to entered value
  Serial.read(); // clear serial buffer

  stepper.setSpeed(stepSpeed); // set stepper motor speed

} // end Setup

void loop() {

  if (Serial.available()) {
    dataIn = Serial.readString();
    d1 = dataIn.charAt(0);

    switch (d1) { //cases based on 1st char of dataIn
      case 's': // start cycle with low case s
        runCycle = 1;
        break;
      case 'S': // start cycle with upper case S
        runCycle = 1;
        break;
      case 'e': // // end cycle with lower case e
        runCycle = 0;
        totSteps = 0;
        break;
      case 'E': // // end cycle with lower case E
        runCycle = 0;
        break;
      case 'R': // // resume cycle with upper case R
        runCycle = 1;
        break;
        totSteps = 0;
        break;
      case 'p': // // pause cycle with lower case p
        runCycle = 0;
        break;
      case 'P': // // pause cycle with upper case P
        runCycle = 0;
        break;
      case 'r': // //resume cycle with lower case r
        runCycle = 1;
        break;
      case 'g': // //Read Steps Value
        x = dataIn.substring(1);
        poles = x.toInt();
        break;
      case 'd': // //Read Steps Value
        x = dataIn.substring(1);
        dwell = x.toInt();
        break;
      case 't': // //Read Steps Value
        x = dataIn.substring(1);
        totSteps = x.toInt();
        break;
    } // end  switch (d1)

  } // end if(Serial.available)

  if (runCycle == 1) {
    if (stepCount < totSteps) {
      Serial.print("step Count = ");
      Serial.print("\t");
      Serial.println(stepCount);

      digitalWrite(led, HIGH);
      incr = incr + poles;
      stepCount = stepCount + 1;
      stepper.step(poles);

      digitalWrite(led, LOW);
      delay(dwell);

    } // end if(stepCount <= totSteps;)

    if (i > totSteps) {
      runCycle = 0;
    } // end if(i > totSteps)

  } // end if(runCycle == 1)

} // end loop

[/code]

Steppers can't go from zero to fast in one go, they need to accelerate up to speed. It looks like you are close to the limit with speed 256. Perhaps try the accelStepper library.

I believe you're right for both problems. I did find the accelstepper library. I'll have to read up on it to see how to use it. Thanks for the info.

I posted a problem I was having with a stepper motor sketch I am working on, my first stepper project, earlier today. It was suggested that I should use the accelstepper library instead of the standard stepper library. I can’t find much in the way of documentation explaining the parameters I can use, acceleration was what the forum reply suggested I use. I did search the forum and have narrowed it down to over 2000 threads. All I want to do is see if adding acceleration helps my problem. It’s a simple system, a bipolar stepper motor with no heavy load I’m trying to move.

If I use stepper.h, my instance is defined as “Stepper stepper(STEPS, 8, 10, 9, 11);”

How do I create the same in accelStepper? I believe that the acceleration parameter would be stepper.setAccerleration(). How do I use that, what value do I give it, is there anything else I need to add to implement it?

Thanks
John

Stumpy_L:
Is it a limit of the L298N?

We don't know. You didn't post a link to the stepper motor.

The L298 is a brushed DC motor driver, NOT a stepper motor driver.
But you can sort-off get away with that with a high impedance motor.

High speed becomes a problem with high impedance steppers.
You need a low impedance stepper, a stepper driver, and a high supply voltage for that.
Leo..

As mentioned my first stepper motor project but I have seen a few examples where they did use a L298N, I had one and it does work. Which driver would you suggest? I'm using a 12 VDC supply into the L298N for the motor. Here's the data sheet for the motor.

Thanks for the reply
John

The AccelStepper library comes with many examples. Which ones have you looked at and studied?

Stumpy_L:
I posted a problem I was having with a stepper motor sketch I am working on, my first stepper project, earlier today. It was suggested that I should use the accelstepper library instead of the standard stepper library.

Please click Report to Moderator and ask to have this Thread joined with the other one so we have all the info about your project in one place.

...R

I have sent the request to the moderator

thanks

Stumpy_L:
I have seen a few examples where they did use a L298N, I had one and it does work.

It does work with this motor, with a coil resistance of 34 ohms.
The L298 does not work with low impedance (< 5ohms) motors, which are more common.
Leo…

Thanks Leo, I’m learning more and more about steppers everyday, and that is a good thing to know, as I will probably be doing more. If I did use a motor with < 5 ohms, what driver module should I use?

Thanks for the info
John

@Stumpy_L

TOPIC MERGED.

Could you take a few moments to Learn How To Use The Forum.
Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum.

Thanks for merging my post. I have read the post you linked here but evidently I should go back and read again

thanks

Stumpy_L:
If I did use a motor with < 5 ohms, what driver module should I use?

A stepper driver that can comfortably handle the max current rating of the motor.
Leo…

Great selection of drivers, good stuff at Polloi

Thanks

@blh64 after my 2 post were merged it didn't show my response to your question about looking at the examples. Yes I did review a few of the examples but that is what confused me more. I do try to find the answers to my questions before posting on the forum, by searching the forum, Google and other programing sites but couldn't find anything to explain.

For example, in my instance to define the stepper using stepper.h I used "Stepper stepper(STEPS, 8, 10, 9, 11);" to define the pins on the Mega that go to the driver board. in the AFMotor_ConstantSpeed example it defines the instance as "AccelStepper stepper(forwardstep, backwardstep)," there are functions declared for forwardstep and backwardstep. forwardstep is "motor1.onestep(FORWARD, SINGLE);" motor1 is declared as "AF_Stepper motor1(200, 1);" How does that define the pins to my driver?

It was suggested that I may have to use acceleration. in the AFMotor_Multistepper example acceleration is defined as "stepper1.setAcceleration(100.0);" what do the values of 100 and 0 define?

I'm not looking for anyone to do this for me I'm looking for something that explains how to use this library.

thanks
John

Stumpy_L:
Yes I did review a few of the examples but that is what confused me more.

Did you try any of the examples, and did they work for you? IMHO that is the place to start. If the example does not work then please post the example program and your wiring diagram so we can help.

...R

@Robin2, Looking back at the examples I have to get a Adafruit motor shield, I don’t have one but will order it today. They have a tutorial using their library, they do make a reference to the accelstepper library. After I get the shield I’ll see how much I can figure out from there.

thanks again
John

It is not a requirement to have an adafruit motor shield. It is supported by the library, but so are others. What are you using?

Right now I'm using a L298N, which I've been told is not the best thing to use but is OK for this stepper. That's partly why I was confused, to use that I had to define the 4 pins on the mega that go to the L298N. Evidently by using a shield I don't have to define those, it's incorporated into the library. If I didn't use a shield is there a way to define the pins? What other shields would I use if not a Adafriut?

thanks for your reply
John