SchmalzHaus logoEasy Driver Examples

Sample code and projects to get your stepper running!

Description:

Lots of folks buy EasyDrivers or BigEasyDrivers and then get them to work just fine in their project. But some don't, and so I thought it would be a good idea to write down some simple instructions for getting your Easy Driver working as quickly and easily as possible.

All of these examples are going to be done with my Easy Driver and Big Easy Driver stepper motor driver boards driving several different random stepper motors I have lying around the lab. I will be generating the step and direction pulses with an Arduino UNO and a chipKIT UNO32, although all of these examples should work with any Arduino or Arduino clone or Arduino compatible (like all chipKIT boards).

And don't forget to read Dan Thompson's excellent Easy Driver tutorial blog post if you want to read more up on this stuff. Some great questions answered in the comments on that blog post.

Note1: All examples will work equally well with Easy Drivers or Big Easy Drivers.
Note2: All examples will work on Arduino as well as chipKIT boards (and some will run much better on chipKIT because of the PIC32 speed)
Note3: All examples show a barrel jack for power input - you need to supply power to the EasyDrivers somehow, but it doesn't need to be a barrel jack. You should have a power supply that can output some voltage between 5V and 30V, at 1 Amp or more.

Example 1: Basic Arduino setup

This is the most basic example you can have with an Arduino, an Easy Driver, and a stepper motor. Connect the motor's four wires to the Easy Driver (note the proper coil connections), connect a power supply of 12V is to the Power In pins, and connect the Arduino's GND, pin 8 and pin 9 to the Easy Driver.

Example 1

Then load this sketch and run it on your Arduino or chipKIT:
void setup() {                
  pinMode(8, OUTPUT);     
  pinMode(9, OUTPUT);
  digitalWrite(8, LOW);
  digitalWrite(9, LOW);
}

void loop() {
  digitalWrite(9, HIGH);
  delay(1);          
  digitalWrite(9, LOW); 
  delay(1);          
}
It doesn't get much simpler than that. What is the code doing? It sets up pin 8 and 9 as outputs. It sets them both low to begin with. Then in the main loop, it simply toggles pin 9 high and low, waiting 1ms between toggles. We use pin 9 as the STEP control and pin 8 as the DIRECTION control to the Easy Driver.

Since we are not pulling either MS1 or MS2 low on the Easy Driver low, the Easy Driver will default to 1/8th microstep mode. That means that each time the "digitalWrite(9, HIGH);" call is executed, the stepper motor will move 1/8th of a full step. So if your motor is 1.8 degrees per step, there will be 200 full steps per revolution, or 1600 microsteps per revolution.

So how fast is this code going to run the stepper? Well, with the STEP signal 1ms high and 1ms low, each complete pulse will take 2ms of time. Since there are 1000ms in 1 second, then 1000/2 = 500 microsteps/second.

What if we wanted the motor to go slower? We change the delay(); lines to have longer delays. If you use delay(10); for both, the you'll move at 50 microsteps/second.

What if you wanted the motor to go faster? We can't really delay for less than 1 ms, can we? Yes, of course we can! We can change the delay() calls to delayMicroseconds(100); calls and then each delay would be 100 microseconds (or us), so the motor would be driven at 5000 microsteps/second.

Now, one thing you should play with is the current adjustment pot on your Easy Driver. You need a tiny little screw driver to turn it, and be sure not to force it too far one way or the other (they're delicate). Also, some Easy Drivers were built with pots that have no physical stops on them, so they spin around and around. As you run the above code, slowly turn the pot one way or the other. Depending upon the type of motor you have (and its coil resistance) you may hear/feel no difference as you spin the pot, or you may notice quite a big difference.

Example 2: Moving back and forth

If we take Example 1, and simply change the sketch a little bit, we can move a certain number of steps forward or backward. Like so:

int Distance = 0;  // Record the number of steps we've taken

void setup() {               
  pinMode(8, OUTPUT);     
  pinMode(9, OUTPUT);
  digitalWrite(8, LOW);
  digitalWrite(9, LOW);
}

void loop() {
  digitalWrite(9, HIGH);
  delayMicroseconds(100);          
  digitalWrite(9, LOW); 
  delayMicroseconds(100);
  Distance = Distance + 1;   // record this step
  
  // Check to see if we are at the end of our move
  if (Distance == 3600)
  {
    // We are! Reverse direction (invert DIR signal)
    if (digitalRead(8) == LOW)
    {
      digitalWrite(8, HIGH);
    }
    else
    {
      digitalWrite(8, LOW);
    }
    // Reset our distance back to zero since we're
    // starting a new move
    Distance = 0;
    // Now pause for half a second
    delay(500);
  }
}
Now using this sketch, we move for 3600 steps in one direction, pause for a bit, and move 3600 steps in the other direction. I'm sure you can figure out how to make many different lengths of moves now. And you can change the delay between steps for each move to occur at separate speeds.

Example 3: Using a pre-built library - AccelStepper

One thing the above examples can't do well is handle multiple steppers from the same Arduino or chipKIT. Also, acceleration and deceleration are difficult as well. Other people have run into this problem, and so now we have libraries that we can download and install into the Arduino IDE or MPIDE to fix these problems.

Download the zip file for the AccelStepper library from this page. Unzip the downloaded file, and place the AccelStepper in to the libraries folder in your Arduino install directory. Note that for MPIDE (chipKIT) users, you need to copy the AccelStepper folder into both the libraries folder at the top level as well as \hardware\pic32\libraries so that both the AVR and PIC32 sides can use it.

Using the same hardware from Example 1, restart the IDE, and enter the following sketch:
#include <AccelStepper.h>

// Define a stepper and the pins it will use
AccelStepper stepper(1, 9, 8);

int pos = 3600;

void setup()
  stepper.setMaxSpeed(3000);
  stepper.setAcceleration(1000);
}

void loop()
{
  if (stepper.distanceToGo() == 0)
  {
    delay(500);
    pos = -pos;
    stepper.moveTo(pos);
  }
  stepper.run();
}
This code does basically the same thing as Example 2, but using acceleration/deceleration via the AccelStepper library, and running for twice as many steps. (Thanks Mr. Duffy for pointing out this important fact!) The reason it runs twice as many steps is because we do "pos = -pos" to keep things short and simple. This means that it will run from 0 to 3600, then from 3600 to -3600 (which is 7200 steps).

Example 4: Running multiple stepper motors

One of the great things about the AccelStepper library is that you can run as many stepper motors as you want, at the same time, just by making more AccelStepper objects. Now, if you try to run them too fast, the steps won't be smooth, so you have to be careful not to load down the Arduino too much. The chipKIT does not have this problem because it is so much faster than the Arduino.

In this diagram, we now have two Easy Drivers and two stepper motors. We just need 2 more pins from the Arduino to add this second motor.

Easmple 4 diagram
The code for this example is shown below:
#include <AccelStepper.h>

// Define two steppers and the pins they will use
AccelStepper stepper1(1, 9, 8);
AccelStepper stepper2(1, 7, 6);

int pos1 = 3600;
int pos2 = 5678;

void setup()
  stepper1.setMaxSpeed(3000);
  stepper1.setAcceleration(1000);
  stepper2.setMaxSpeed(2000);
  stepper2.setAcceleration(800);
}

void loop()
{
  if (stepper1.distanceToGo() == 0)
  {
        pos1 = -pos1;
    stepper1.moveTo(pos1);
  }
  if (stepper2.distanceToGo() == 0)
  {
    pos2 = -pos2;
    stepper2.moveTo(pos2);
  }
  stepper1.run();
  stepper2.run();
}

If you run this code, you may find that the acceleration and deceleration are not quite as smooth as with a single motor (on an Arduino - again, this problem doesn't occur on chipKIT) - that is because our two maximum speeds (3000 and 1000) are pretty high for the ability of the processor to handle them. One solution is to make your max speeds lower, then switch from 1/8th microstepping to 1/4, half, or full step mode. If done right, you'll see the same shaft rotation speeds, but with less CPU load (because you aren't generating as many steps per second.)

You can see that for this example, I just copied and pasted the code from Example 3 and made two positions and two steppers. This example code is very simple and not all that useful, but you can study the existing examples from the AccelStepper library, and read the help pages on the different functions, and get good ideas about what else you can do with your stepper control.

References:

Easy Driver Pinout:



EasyDriver V4.4 pin
        descriptions





Questions? E-mail me at e-mail
Creative Commons License
EasyDriver by Brian Schmalz is licensed under a Creative Commons Attribution 3.0 United States License.
Based on a work at www.schmalzhaus.com/EasyDriver.
Permissions beyond the scope of this license may be available at www.schmalzhaus.com/EasyDriver.