Motor too fast. Any problems in the code?

Hi!

I'm building a barn door using Arduino + Nema 17 + A4988.

I'm following the project's code, but the motor is turning too fast, when in fact, it should be at 4.7RPM.

Does the code appear to have a problem?

#define DELAY 3997 //3997 = 1.09 vueltas por minuto en el engranaje grande

int paso = 9;
const int led = 13;
int direccion = 8;
int ledState = LOW;
unsigned long previousMillis = 0;
const long interval = 500;

// the setup routine runs once when you press reset:
void setup() {                
  pinMode(paso, OUTPUT); 
  pinMode(direccion, OUTPUT); 
  pinMode(led, OUTPUT);

}

// the loop routine runs over and over again forever:

void loop() {
    unsigned long currentMillis = millis();
      if(currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED 
    previousMillis = currentMillis;   

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW)
      ledState = HIGH;
    else
      ledState = LOW;

    // set the LED with the ledState of the variable:
    digitalWrite(led, ledState);
  }
    digitalWrite(paso, LOW);   
    digitalWrite(paso, HIGH);  
    delayMicroseconds(DELAY);    

}

How many steps per revolution for your motor? Are you using microsteps? If so, how many?

1 Like

What kind of motor is it?
What kind of driver is it?
How fast is it going?
How fast do you want it to go?

Look like maybe a steppper motor, getting a little under 250 step pulses per second (3997+ microseconds per step).

1 Like

I apologize for the lack of information!

What kind of motor is it? - Stepper motor 17hs4401s
What kind of driver is it? A4988
How fast is it going? approximately 74 RPM
How fast do you want it to go? 4.7036 RPM

My math is as follows:

// radius of the barn door tracker = 200mm (hinge to threaded rod)
// full revolution is 2 * radius * PI = 2 * 200 * 3.141592 = 1256.6368mm
// a full day is 23:56 = 23 * 60 + 56 = 1436 minutes
// movement speed (of the hinge) = 1256.6368 / 1436 = 0.87509 mm/min at threaded rod level
// the M5 stdandard picth is 0.8mm
// Rotation speed of the large gear = 0.85509 / 0.8 = 1.0939 RPM
// Larger gear has 43 teeth, the small one has 10 teeth, gear ratio = 43 / 10 = 4.3
// Small gear (and motor) speed has to be 1.0939 * 4.3 = 4.7036 RPM

3997 us per step is for a 28BYJ-48 motor, 2048 steps per rev. For a 200 step motor you would need 63781 microseconds per step.

so i should change the delay from 3997 to 63781 right?

Well, give it a try and see what happens. You would get smoother operation using 8x microstepping, read the documentation for the A4988.

1 Like

74 rpm * 200 ppr = 14,800 ppm = 246.666666666666667 pps = 4,054 microseconds per pulse. That matches well with your 3,997-microsecond delay.

If you want 4.0736 RPM you want 814.72 ppm (13.578666 pps) or 73645 microseconds per pulse. Subtract about 57 microseconds for loop overhead and you get a delay of 73597.

I found the problem!! using 1/16 microsteps the speed is correct! Thank you all!

a doubt: is it normal for the high vibration of the motor?

I was going to suggest that. :roll_eyes:

your tip of using x8 brought me the solution! thank you so much!

a doubt: is it normal for the high vibration of the motor?

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