Arduino Stepper Motor Library

StepperLab3

The StepperLab3 stepper motor library is made for our SensorAktor Shield but it works also with other motor driver IC’s like the popular L293.


Compared to the standard Arduino stepper library we added some features for more convenient handling and getting most out of your stepper. The library functions are attached to an interrupt process what allows an accurate speed control and a non blocking behavior. If you tell the motor to go to a certain position this is done in the background while you can do other things in your code.


A ramp function allows a acceleration in a given number of steps to the speed which was set before. When you want to move masses with an inertia, a matched acceleration improves the dynamic behavior of your setup.


The Half step mode doubles the given step number of your motor. This improves not only the positional accuracy but leads also to a more smooth rotation. Because in the half step mode both motor coils can be activated together the library tries to provide a constant motor current.


The power control sets the motor current by using the PWM function of pin 9 and 10. This allows to adapt the motor current to different motor speeds or reduce the current when the motor is stopped. Be sure that the motor current does not exceed the limit of your driver IC. The L293 provides 600mA. In our shield we have a 500mA polyfuse to protect the driver IC.


Keep in mind that the library is using ATMEGAs timer1 and the corresponding interrupt. Some other libraries or arduino functions might work not correct.

Driver Schematic


Methods

attach(motor1_1,motor1_2,motor2_1,motor2_2);


void setPower(int power );// set motor torque 0..1023


void setSpeed(int speed );// set motor step speed [steps per second x 10]


void setRampSteps(int steps );//number of steps within the motor is accelerated to  setSpeed, 0= ramp  off


void setFullStep();//fullstep mode, normal operation


void setHalfStep();//double number of motor steps


void absoluteSteps(int steps );//move  to absolute target position


void relativeSteps(int steps );//move relative to last target


void rotate(int r ); // continous rotation, 1=fwd, -1=rev, 0=stop


int getSteps();// read current absolute position


int stepReady();// !=0  when stepper reached target position


Download the Library

Updated 1/12

StepperLab3


Examples

Ramp Test Example


/* stepper library StepperLab3
/*
 * ramp example
 *
 * KHM Lab3 2010
 * Kunsthochschule fuer Medien Koeln
 * Academy of Media Arts Cologne
 * http://interface.khm.de

 */

// import stepper library
#include "StepperLab3.h"
// create instance for stepper
StepperLab3 myStepper;

int state=1;
int statea=0;

int cnt10ms;
int cnt2;

int ledGreen= 5;
int motor1_1=8;
int motor1_2= 13;
int motor2_1= 11;
int motor2_2= 12;

void setup(){
  Serial.begin(115200);
  Serial.println(" stepperLab3 library test");

  pinMode(ledGreen,OUTPUT);  // LED pin
  myStepper.attach(motor1_1,motor1_2,motor2_1,motor2_2);

  myStepper.setPower(900);
  myStepper.setSpeed(500);
  myStepper.setFullStep();

}

// this block is executed in a loop after setup is called one time
void loop(){
  delay(10);
  cnt10ms++;
  cnt2++;

  if ( state != statea) {  // print state
    Serial.print("state:");
    Serial.print(state);
    Serial.println("");
  }
  statea=state;

  //  myStepper.debugger();

  switch (state) {

  case 1:
    state = 300;
    myStepper.setRampSteps(15);
    break;

  case 300:  //  start new position
    myStepper.setPower(900);   // power = 900 run
    myStepper.setSpeed(1000);  // speed = 1000* 100ms = 100 steps/second
    myStepper.absoluteSteps(100);
    state=301;
    break;

  case 301:  //  wait unil position is reached
    if (myStepper.stepReady() == 1) {
      myStepper.setPower(500);   // power = 500  idle
      state=302;
    }
    break;

  case 302: //  start new position
    myStepper.setPower(900);   // power = 900 run
    myStepper.absoluteSteps(0);
    state=303;
    break;

  case 303: //  wait unil position is reached
    if (myStepper.stepReady() == 1) {
      myStepper.setPower(500);   // power = 500  idle
      state=400;
      cnt2=0;
    }
    break;

  case 400: // 2 sec pause
    if (cnt2 > 200) {
      cnt2=0;
      state=300;
    }
    break;
  }

  if (cnt10ms % 50==0)  { // blink led 500 ms
    digitalWrite(ledGreen,!digitalRead(ledGreen));
  }
}
//*********************************************************************




Serial Command Positon Example  Control of a stepper motor with terminal commands

Download SerialCommandStepperLab3 sketch

Contact

Martin Nawrath KHM 11/2010, nawrath@khm.de