Thursday, June 4, 2015

Arduino blocks: Driving a simple unipolar stepper motor

I picked up a simple stepper motor with a little driver board for about $2.50 on eBay.

Making things actually physically move with your projects is very gratifying and interesting. There are a number of ways to do it. Stepper motors are one of the simpler ways.

This particular motor and driver is a unipolar setup, which is about as simple as it gets for stepper motors. This video will go through it in what I hope is an understandable way.





Link to data sheet

Code:
<pre>// Very simple stepper motor driver demo
// John Ridley June 2015
// CC0 public domain dedication

int stepperpins[] = {2,3,4,5};
int stepperCurPos = 0;

void SetupStepper()
{
  for (int x=0; x<4; x++)
  {
    pinMode(stepperpins[x], OUTPUT);
    digitalWrite(stepperpins[x], 1);
  }
}

void MoveStepper(int direction)
{
  digitalWrite(stepperpins[stepperCurPos], 0); // turn off current pin
  stepperCurPos += direction;
  if (stepperCurPos > 3)
    stepperCurPos = 0;
  if (stepperCurPos < 0)
    stepperCurPos = 3;
    
  digitalWrite(stepperpins[stepperCurPos], 1); // turn on new current pin
}

void setup() {
  // put your setup code here, to run once:

  SetupStepper();
}

int msdly = 3;

void loop() {
  
  for (int x=0; x<200; x++)
  {
    MoveStepper(1);
    delay(msdly);
  }
  for (int x=0; x<200; x++)
  {
    MoveStepper(-1);
    delay(msdly);
  }
}
</pre>

No comments: