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);
}
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
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.