Monday, March 23, 2015

Monduino 002: Stepper Motor Control

    Recently I decided I wanted to build my own 3D printer, that was until my computer crashed... for good. However before that happened I had already ordered some parts for the 3D printer, including a servo NEMA 17 stepper motor, a few A4988 stepper drivers, along with various other components. Today I am going over some stepper basics and how to control one with an Arduino.

    So, what is a stepper motor? Well, it sort of mixes the continuous rotation of a normal DC motor and the precision turning of a servo motor. This means it can be used in applications requiring parts to move a precise distance that can't be obtained in a single rotation of the motor shaft (the part that moves). This precision is a achieved by moving the motor in a series of "steps," or a specific angle.

     To learn more about how a stepper works check out this great article, on explain that stuff.





    Connected to the Arduino are two buttons to control direction, and two outputs to the A4988 that control direction and steps. When one button is pushed the motor spins while that button is pushed in whatever the specified direction is. For this usage a conventional DC motor would suffice, but if wanted one could specify  a certain number of steps  to move the motor every time a button is pressed. Until I've replaced my pc i won't be posting wiring schematics, so a future update will fix that.

    The leads of your stepper will depend upon the manufacturer, and they should have documentation to refer to. A four wire motor I find is easiest to use, I t connects to the A4988 easily. Make sure to have an appropriate power supply as most steppers require more than the Arduino's 5v output.

The code below should get the stepper moving left and right if configured properly:

int directionPin = 5; // controlls stepper direction on A4988
int stepPin = 3; // controlls no. of steps on A4988 
int numberOfSteps = 200; //No. of steps in full rotation
int pulseWidthMicros = 600;  // microseconds
int millisbetweenSteps = 1; // milliseconds
int b1 = 4; // button 1
int b2 = 7; // button 2

void setup() 


 Serial.begin(9600);
 Serial.println("Stpper Movement");

 pinMode(directionPin, OUTPUT);
 pinMode(stepPin, OUTPUT);
 pinMode(b1, INPUT);
 pinMode(b2, INPUT);



}

void loop() 

  
  digitalRead(b1);
  digitalRead(b2);

  while(digitalRead(b1) == HIGH){
    
   digitalWrite(directionPin, HIGH);
   for(int n = 0; n < numberOfSteps; n++) {
     digitalWrite(stepPin, HIGH);
     delayMicroseconds(pulseWidthMicros);
     digitalWrite(stepPin, LOW);
     Serial.println("B1 HIGH");
     delay(millisbetweenSteps);
     
     if(digitalRead(b1) == LOW){
     
      break ;
     }
 }
  }
  
  while(digitalRead(b2) == HIGH){
        digitalWrite(directionPin, LOW);
     for(int n = 0; n < numberOfSteps; n++) {
       digitalWrite(stepPin, HIGH);
       delayMicroseconds(pulseWidthMicros);
       digitalWrite(stepPin, LOW);
       Serial.println("B2 HIGH"); 
       delay(millisbetweenSteps);
   
       if(digitalRead(b1) == LOW){
         
         break ;
     }
 }

  }
  
}