Stepper Motor not rotating (oscillating) in a particular range of speed (100-150 RPM)

Stepper motor oscillates instead of rotating in the particular range of RPM (100-150). It does rotate when I change the speed (StepRPM) below 100 and way above 150 i.e. up to 900 RPM.

int StepRPM=100;
float pulserate;
float delaytime;
int delayint;


void setup() {
  
  pinMode(3,OUTPUT);
  pinMode(5,OUTPUT);
  
}

void loop() {
 

    pulserate=(200/60)*StepRPM;
    delaytime=(1000000/pulserate);
    delayint = static_cast<int>(delaytime);


   if(StepRPM>0) {

        digitalWrite(3,HIGH);

          for(int x=0; x<200; x++) {
              digitalWrite(5,HIGH);
              delayMicroseconds(delayint);
              digitalWrite(5,LOW);
              delayMicroseconds(delayint);

          }
   }

   if(StepRPM<0) {

        digitalWrite(3,LOW);

          for(int x=0; x<200; x++) {
              digitalWrite(5,HIGH);
              delayMicroseconds(delayint);
              digitalWrite(5,LOW);
              delayMicroseconds(delayint);

          }
   }
    
}

Sounds like you have encountered resonance. Enable microstepping will mitigate resonance effects. X4 to X8 at least. I have motors that shudder/stop that at 200 steps/sec, no microstepping. Setting microstepping to x4 helps a lot.

And/or use some damping in the mechanical drive (belt drive is better damped than lead screw, for example).

const byte StepperRPM = 100;
const unsigned long Delay = 300000UL / StepperRPM ;

void setup() {
  pinMode(3, OUTPUT);
  pinMode(5, OUTPUT);
  digitalWrite(3, HIGH);
}

void loop() {
  digitalWrite(5, HIGH);
  delayMicroseconds(Delay);
  digitalWrite(5, LOW);
  delayMicroseconds(Delay);
}

Try : pulserate = (200.0 / 60.0) * StepRPM; delaytime = (1000000.0 / pulserate);

in order to make sure the compiler uses float in the intermediate math.

Hi, @sat20
Welcome to the forum.

Thanks for using code tags. :+1:

Can you please post a circuit diagram of your project.
An image of a hand drawn schematic will be good, please include all power supplies, component names and pin labels.

Can you post links to stepper and driver units spec/data please?

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

Thanks a lot, @groundFungus. Microstepping to x4 worked.

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