On RPM determination of stepper motors

Dear Forum Arduino members,

I need some guidance on stepper motors. I have Nema 17 (200 pulses per revolution) connected to Arduino Mega, Ramps 1.6 and DRV8825 (with 3 jumpers, i.e., 1/16 stepping precision). The pins for the X motor are as follows.

#define X_STEP_PIN    54
#define X_DIR_PIN     55
#define X_ENABLE_PIN  38

I want to run the motor for a given number of RPM value and I want to be able to increase/decrease the rpm during the run. I can run the stepper with

#include <AccelStepper.h>

But I dont know how can I determine the RPM value of the motor, also when I start running the motor, I cannot change the speed after the motor runs.
Question 1. Which stepper motor library should I use?
Question 2. How can I run my motor for a prescribed RPM value?
Question 3. How can I increase/decrease the value of RPM with two buttons (or the encoder on RepRapDiscount Full Graphic Smart Controller)?
Question 4. Or should I pulse the stepper without any library?

Thank you very much.
bkarpuz

You don't. Set the speed before starting the stepping. Either the stepper moves according to the stepping rate or it stalls. Nothing in between.
Q1. MobaTool is recommended by another, knowing helper. I haven't tested it yet.
Q2. The AccelStepper parameter is pulses per second, not RPM. Do some calculating. On an UNO max pulses per second turns out to be 4000. Likely the same on a Mega.
Q3. Let the buttons increase/decrease a variable used to set the stepping rate.

I rotation = 200 steps.
200 steps in one minute = 1 RPM

From there, you should be able to work out the time per step.

what do you have to lose if you try?

Thank you (Railroader, jremington, kolaha) for your help. As far as I understand, sending HIGH and LOW in this order to X_STEP_PIN posts a single pulse to the stepper motor. I have to repeat this for a total of 400 times for Nema 17 if microstepping is not activated. Depending on the microstepping values, I have to adjust this due to the datasheet of DRV8825. Sending HIGH or LOW to X_DIR_PIN determines the direction of Nema 17 (this is also related to the wiring of the stepper motor). Finally, Nema 17 is activated or deactivated by sending LOW or HIGH to X_ENABLE_PIN, respectively.

Okay, I came up with a working code as follows.

//Objective:
//Rotate Nema 17 Stepper Motor with 20 RPM with Alternating Direction
//
//Hardware:
//Arduino Mega
//RAMPS 1.6 (or 1.4)
//DRV8825 with VRef 0.60 and 3 Jumpers on X Slot
//
//References:
//https://www.makerguides.com/drv8825-stepper-motor-driver-arduino-tutorial/

#define X_STEP_PIN    54
#define X_DIR_PIN     55
#define X_ENABLE_PIN  38
//#define X_CS_PIN    53

#define ppr (16 * 400) // Pulses per Revolution, 3 jumpers are attached, i.e., 1/16 microsteps
#define rpm_delay (60 * 1000000) / (16 * 400)  // Delay for Rotation per Minute, 1 Minute = 60 Seconds, 1 Seconds = 1000000 Microseconds, 60*1000000/(16*400)

int pulse_counter = 0;
int rpm_val = 20;
int rpa_val = 1;
bool cw_rotation = true;

void setup() {

  pinMode(X_STEP_PIN, OUTPUT);
  pinMode(X_DIR_PIN, OUTPUT);
  pinMode(X_ENABLE_PIN,OUTPUT);

  digitalWrite(X_ENABLE_PIN,LOW);
  digitalWrite(X_DIR_PIN, HIGH);
  cw_rotation = true;

  Serial.begin(9600);
  Serial.println("Started");

}

void loop() {

  if (pulse_counter < ppr * rpa_val)
  {
    digitalWrite(X_STEP_PIN, HIGH);
    digitalWrite(X_STEP_PIN, LOW);
    delayMicroseconds(rpm_delay / rpm_val);
    pulse_counter = pulse_counter + 1;
  }
  else
  {
    if (cw_rotation == true)
    {
      digitalWrite(X_DIR_PIN, LOW);
      cw_rotation = false;
    }
    else
    {
      digitalWrite(X_DIR_PIN, HIGH);
      cw_rotation = true;
    }
    pulse_counter = 0;
    Serial.println("Restarted");
  }
  //Here additional codes can be added to change rpm_val and rpa_val by using buttons

}

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