Stepper Motor Speed Control using RPM ineffective...

Dear all,

I'm new to Arduino (have used RasPi in the past but Arduino is better for my current purpose).
I'm also new to Stepper motors but have read the guides people have posted on here about the basics.

I have a Zapp Automation SY57STH56-4004A motor (4A, 200 steps/rotation) connected to a Zapp Automation Stepper Motor Drive unit UIM240C04T

They're connected to an arduino mega 2560 on pins 8 and 9 of the PWM section (for direction and steps control) and the 5V out for the VCC. Powered by a pair of 12V yuasa lead acid gel batteries in series to give a healthy 24v supply.

I have been able to get the motor to rotate using the simple "steps per revolution" tutorial provided in the examples code but now I need to speed up its RPM to 120.

I've tried simply amending the code to the following but it's just rotating incredibly slowly:

#include <Stepper.h>

const int stepsPerRevolution = 200;  // change this to fit the number of steps per revolution for your motor
const int motorRPM = 120;

// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8, 9);

int stepCount = 0;         // number of steps the motor has taken


void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:
  
  // set the motor speed and run it:
  myStepper.setSpeed(motorRPM);
  myStepper.step(stepsPerRevolution);

I've called the manufacturer and they've told me that 120 is fairly slow and if there's no load on the motor (which currently there isn't) it shouldn't need ramping up. If it was closer to 500 or 600RPM, then that would be a different matter and I'd need to look in to acceleration.

Please can someone assist me with my code as I'm sure there should be something really simple as the library isn't exactly complex.

Thank you all in advance.

The odd thing is that when I take out the setSpeed code, it runs faster!

You should use the AccelStepper library, it can drive step+direction interfaces, as well as ramping
speed (necessary anyway).

Ah, I heard about that and have downloaded it, annoyingly I'm going to have to remove the line numbers from each line as the download from their website is copied that way.

Thank you I'll take a look.

annoyingly I'm going to have to remove the line numbers from each line as the download from their website is copied that way

Download the AccelStepper zip file, without line numbers here.

Try putting your test code in setup() instead of loop()

You understand why it was failing? You were asking the motor to accelerate to speed instantly, which isn't
physically possible. Once a motor is completely miss-stepping it can just sit there and vibrate as it cannot
regain lock within one step pulse period.

Think about it as pulling a car with a thin rope - yank the rope too hard and it snaps, accelerate
smoothly and it doesn't.

Hi all.

Thank you for replying - I rejoined as myself and have certainly made headway since.

With the AccelStepper, it has a mode for controlling a driver, which is exactly what I wanted. I am slowly understanding stepper motors.

However, I do actually have a new but sort of related query.

In using a latched momentry button to turn the motor on with one press and then off with the next, repeating adinfenitum, I'm getting some odd behaviour in that even though the speed settings have not changed, the motor runs so much more slowly and I don't see why.

My current code is here:

//Control 1 stepper motor connected to a driver, run it as a DC motor running at 120 RPM.

#include <AccelStepper.h>

const int buttonPin = 2;     // the number of the pushbutton pin

int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button

int motorSpeed = 6400; //120 RPM at 200 steps per rev (notated in microsteps)
int motorAccel = 3200; //microsteps/second/second to accelerate

int motorDirPin = 8; //digital pin 8     < ===THIS IS A DIRECTION PIN
int motorStepPin = 9; //digital pin 9

int stepCount; // counter for number of steps taken

//set up the accelStepper intance
//the "1" tells it we are using a driver
AccelStepper stepper(1, motorStepPin, motorDirPin);



void setup()
{
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT_PULLUP);

  //bits for the stepper motor speed and acceleration:
  stepper.setMaxSpeed(motorSpeed);
  stepper.setSpeed(motorSpeed);
  stepper.setAcceleration(motorAccel);

  stepper.move(-192000);
  // initialize serial communication:
  Serial.begin(9600);
  
}



void loop()
{
  /*The code in this loop will detect if a button is pressed and if so, it will run the motor
  if it is pressed again it will turn off the motor.
  */
  
  //read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button went from off to on:
      buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes: ");
      Serial.println(buttonPushCounter);
    } else {
      // if the current state is LOW then the button went from on to off:
      Serial.println("off");
    }
    // Delay a little bit to avoid bouncing
    delay(100);
  }
  // save the current state as the last state, for next time through the loop
  lastButtonState = buttonState;


  // turns on the motor every two button pushes by checking the modulo of the
  // button push counter. the modulo function gives you the remainder of the
  // division of two numbers:
  if (buttonPushCounter % 2 == 0) {
    stepCount = 0;
    
    while (stepCount < 3200) {
      stepper.enableOutputs();
      stepper.run();
      stepCount++;
      Serial.println(stepCount);
      buttonState = digitalRead(buttonPin);
      
      if (buttonState != lastButtonState){
        break;
        delay(100);
      }
      
    }
    
  } else {
    stepper.disableOutputs();
  }
  
}