UStream Not Live

cyancdesign: blog

Controlling a Stepper Motor

One day I tore apart a scanner I owned. I didn't have the power cord for it, so no loss. In fact it became a gain! Time to learn about the stepper motor.

I started researching, like most things I didn't get it at first. But I didn't want to tear apart the motor. I wanted to use it. So I kept on Googling.

With some really basic Arduino knowledge I started there. What do I need to control a stepper motor with an Arduino. Without burning out the Arduino or Stepper motor. Turns out there is a board specifically for that.

EasyDriver V4.2
Purchase Here: Sparkfun

Sparkfun is AWESOME too! *shameless plug*

It's nice, the EasyDriver powers the stepper motor so 12V doesn't have to find its way through the Arduino. First this I suggest it just get that up and running itself.

This site help me a lot to understand what a stepper motor is, and how to get started. see Tom Igoe's notes on steppers. But not necessarily the code portion. That code doesn't use the EasyDriver, so we need code for that instead, example here.

Or, here is the exact code I used.

#define stepPin 4
#define dirPin 5
 
void setup()
{
  Serial.begin(9600);
  Serial.println("Starting stepper exerciser.");
 
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
 
  digitalWrite(dirPin, HIGH);
  digitalWrite(stepPin, LOW);
}
 
void loop()
{
    int i, j;
 
    for (i=1650; i>=600; i-=150)
    {
      Serial.print("Speed: ");
      Serial.println(i);
 
      for (j=0; j<2000; j++)
      {
        digitalWrite(stepPin, HIGH);
        delayMicroseconds(2);
        digitalWrite(stepPin, LOW);
        delayMicroseconds(i);
      }
 
      delay(500);
      Serial.println("Switching directions.");
      digitalWrite(dirPin, !digitalRead(dirPin));
 
      for (j=0; j<2000; j++)
      {
        digitalWrite(stepPin, HIGH);
        delayMicroseconds(2);
        digitalWrite(stepPin, LOW);
        delayMicroseconds(i);
      }
 
      delay(1000);
      Serial.println("Switching directions.");
      digitalWrite(dirPin, !digitalRead(dirPin));
  }
}

Evolving the Code

After that was running, I wanted more control. So through a bit of trial and error...and brute force. I was able to work in a potentiometer for speed control. And after that, a switch to control the direction. I didn't even deviate from the Arduino examples. I just used the one by one to work into it.

Some differences from the above code is that there is no longer a progression of speeds automatically. That is now controlled by the potentiometer. And the switch is used to control direction.

And here is the code for you to try out.

#include <Potentiometer.h>
 
Potentiometer potentiometer = Potentiometer(2); //a Potentiometer at analog in 2
 
#define stepPin 4
#define dirPin 5
 
const int buttonPin = 9;
int buttonState = 0;
 
void setup()
{
  pinMode(buttonPin, INPUT); // Switch
 
  pinMode(stepPin, OUTPUT); //stepper pin
  pinMode(dirPin, OUTPUT);  //direction pin
 
  digitalWrite(dirPin, HIGH); //sets the direction one way
  digitalWrite(stepPin, LOW); //starts the steppin'
}
 
void loop()
{
  //this is the simplest version, just steps high to low
  digitalWrite(stepPin, HIGH);
  delayMicroseconds(10 + potentiometer.getValue());
  digitalWrite(stepPin, LOW);
  delayMicroseconds(10 + potentiometer.getValue());
 
  //then checks to see if there is a change in the button
  //if the button in pressed, then the electricity is now going through the resistor
  //and the LED, which changes the digital read.
  //try out different resistors if the one listed doesn't function properly
  buttonState = digitalRead(buttonPin);
  if (buttonState == HIGH) {
    digitalWrite(dirPin, HIGH);
  }else{
    digitalWrite(dirPin, LOW);
  }
}

I'd like to note that I am a hobbyist, and do not have any actual electrical engineering education other than my own messing around with this stuff.I would assume you'd be able to kinda tell by my stellar diagrams.

So if anything looks out of place or completely wrong, let me know, and let me know why it is wrong too.

(^_^)//

Tags: , ,

One Response to “Controlling a Stepper Motor”

  1. UIrobot featured Stepper Motor Controllers and Stepper Motor Drivers size only 42.3*42.3*13.5mm include parallel port stepper motor driver, serial port (RS232) and network(CAN Protocol) Stepper Motor Controller which can be directly mounted onto size 42/57/86/110mm (They are NEMA17, NEMA23, NEMA34 and NEMA42 step motors) stepper motors. Ultra-miniature size, high performance, standalone workable or as built-in control and drive unit, easy wiring and strong driving force are the unique selling point of UIM stepper motor controllers and stepper motor drivers.

Leave a Reply