Stepper Motor jerking ( video )

Hi guys and sorry for bugging you with my stepper motor problems :slight_smile:

video: - YouTube

Here's my code that's supposed to make the motor work smoothing one half-step at a time and then pause for a second.

int pin1 = 2;
int pin2 = 3;
int pin3 = 5;
int pin4 = 6;

int delayMin = 10;
int delayTime = 1000;

void setup() {
  
  pinMode(pin1, OUTPUT);
  pinMode(pin2, OUTPUT);
  pinMode(pin4, OUTPUT);
  pinMode(pin3, OUTPUT);

  Serial.begin(9600);
  pwm();
  
}

void loop() {}


void pwm()
{

///////////////////////////////////////////////

Serial.println("COIL ONE->TWO STARTING!");

for ( int i = 0; i<=250; i=i+25 )
{ 
    Serial.print("COIL ONE->TWO : ");
    Serial.println(i);
  
    analogWrite(pin1, 0);
    analogWrite(pin2, 250);
    analogWrite(pin3, 250);
    analogWrite(pin4, i);
  
    delay(delayMin);
} 
  
  Serial.println("COIL ONE->TWO FINISHED!");
  delay(delayTime);
  Serial.println(" - - - ");

///////////////////////////////////////////////

///////////////////////////////////////////////

Serial.println("COIL TWO->THREE STARTING!");

for ( int i = 0; i<=250; i=i+25 )
{ 
    Serial.print("COIL TWO->THREE : ");
    Serial.println(i);
  
    analogWrite(pin1, 0);
    analogWrite(pin2, 250);
    analogWrite(pin3, 250-i);
    analogWrite(pin4, 250);
  
    delay(delayMin);
} 
  
  Serial.println("COIL TWO->THREE FINISHED!");
  delay(delayTime);
  Serial.println(" - - - ");

///////////////////////////////////////////////

///////////////////////////////////////////////

Serial.println("COIL THREE->FOUR STARTING!");

for ( int i = 0; i<=250; i=i+25 )
{ 
    Serial.print("COIL THREE->FOUR : ");
    Serial.println(i);
  
    analogWrite(pin1, i);
    analogWrite(pin2, 250);
    analogWrite(pin3, 0);
    analogWrite(pin4, 250);
  
    delay(delayMin);
} 
  
  Serial.println("COIL THREE->FOUR FINISHED!");
  delay(delayTime);
  Serial.println(" - - - ");
  
///////////////////////////////////////////////

///////////////////////////////////////////////

Serial.println("COIL FOUR->FIVE STARTING!");

for ( int i = 0; i<=250; i=i+25 )
{ 
    Serial.print("COIL FOUR->FIVE : ");
    Serial.println(i);
  
    analogWrite(pin1, 250);
    analogWrite(pin2, 250-i);
    analogWrite(pin3, 0);
    analogWrite(pin4, 250);
  
    delay(delayMin);
} 
  
  Serial.println("COIL FOUR->FIVE FINISHED!");
  delay(delayTime);
  Serial.println(" - - - ");

///////////////////////////////////////////////

///////////////////////////////////////////////

Serial.println("COIL FIVE->SIX STARTING!");

for ( int i = 0; i<=250; i=i+25 )
{ 
    Serial.print("COIL FIVE->SIX : ");
    Serial.println(i);
  
    analogWrite(pin1, 250);
    analogWrite(pin2, 0);
    analogWrite(pin3, i);
    analogWrite(pin4, 250);
  
    delay(delayMin);
} 
  
  Serial.println("COIL FIVE->SIX FINISHED!");
  delay(delayTime);
  Serial.println(" - - - ");

///////////////////////////////////////////////

///////////////////////////////////////////////

Serial.println("COIL SIX->SEVEN STARTING!");

for ( int i = 0; i<=250; i=i+25 )
{ 
    Serial.print("COIL SIX->SEVEN : ");
    Serial.println(i);
  
    analogWrite(pin1, 250);
    analogWrite(pin2, 0);
    analogWrite(pin3, 250);
    analogWrite(pin4, 250-i);
  
    delay(delayMin);
} 
  
  Serial.println("COIL SIX->SEVEN FINISHED!");
  delay(delayTime);
  Serial.println(" - - - ");

///////////////////////////////////////////////

///////////////////////////////////////////////

Serial.println("COIL SEVEN->EIGHT STARTING!");

for ( int i = 0; i<=250; i=i+25 )
{ 
    Serial.print("COIL SEVEN->EIGHT : ");
    Serial.println(i);
  
    analogWrite(pin1, 250);
    analogWrite(pin2, i);
    analogWrite(pin3, 250);
    analogWrite(pin4, 0);
  
    delay(delayMin);
} 
  
  Serial.println("COIL SEVEN->EIGHT FINISHED!");
  delay(delayTime);
  Serial.println(" - - - ");

///////////////////////////////////////////////

///////////////////////////////////////////////

Serial.println("COIL EIGHT->ONE STARTING!");

for ( int i = 0; i<=250; i=i+25 )
{ 
    Serial.print("COIL EIGHT->ONE : ");
    Serial.println(i);
  
    analogWrite(pin1, 250-i);
    analogWrite(pin2, 250);
    analogWrite(pin3, 250);
    analogWrite(pin4, 0);
  
    delay(delayMin);
} 
  
  Serial.println("COIL EIGHT->ONE FINISHED!");
  delay(delayTime);
  Serial.println(" - - - ");

///////////////////////////////////////////////

    pwm();

}

And the smooth transit work for most of the times, except for transits 3-4 and 8-1 , at this moments you can see it jerking.

Any idea what I might be doing wrong? Also is this hi-pitch sound normal?

Thanks. :slight_smile:

Hi you need to digitalwrite high or low, not analogue, you are trying to use PWM control which is causing a problem for a start.
Your delay as I see it is set for delaymin which is 10, use 1000 and you can then see if the steps are in the right sequence.
A circuit diagram and also what type of stepper it is would help.
Tom.

By the looks of the video you have the sequence right, but PWM is stuffing up how the drive works.

Hi you need to digitalwrite high or low, not analogue, you are trying to use PWM control which is causing a problem for a start.

Nope, the PWM works like a charm! Appears that just pin 1 of arduino nando doesn't provide it. :slight_smile: Switched to pin 11 instead and now everything works perfect! 8)

What's the difference between analogWrite(250) and digitalWrite(HIGH) ?

...R

AnalogWrite(250) writes the numeric value 250 to the depending pin.
DigitalWrite(High) sets the port to digital +1 (High)

What's the difference between analogWrite(250) and digitalWrite(HIGH) ?

There isn't any as far as I know. But using analogWrite(250-i) or analogWrite(i) , I can make a smooth transit from HIGH to LOW. )

analogWrite(pin, 250) isn't fully on, much better to use digitalWrite (pin, HIGH), as that's clearer.

Use analogWrite (pin, 255-i), 255 is the maximum, not 250.

For better linearity use sin() and cos() (or a small look-up table of sines/cosines), perhaps (although with
PWM this may not be great, since you are not directly controlling winding current, which is the normal
way to do micro-stepping like this)