Stepper Motor jerking ( video )

arduinostepper motor

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

video: http://youtu.be/Ly5F-fKxIcw

Here's my Arduino 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?

Best Answer

thanks for the tip, Anindo. Basically the code above work fine, the only problem was that int pin1 = 2; didn't support PWM. Switching to pin 11 fixed it. :)