Big Easy Driver Hookup Guide
Arduino Code
Basic Arduino Example
Now that you have the hardware hooked up and ready to go, it's time to get the code uploaded. For the most up-to-date code available, please check the GitHub repository. Or download the Arduino code from the link below. Navigate to Big_Easy_Driver > Firmware > SparkFun_Big_ Easy_Driver_Basic_Demo > SparkFun_Big_Easy_Driver_Basic_Demo.ino example and open it in the Arduino IDE.
The first section of the sketch defines all of the pin connections between the Arduino and the Big Easy Driver. It also sets these pins as outputs, and puts them to the proper logic levels to begin driving the motor.
language:c
//Declare pin functions on Arduino
#define stp 2
#define dir 3
#define MS1 4
#define MS2 5
#define MS3 6
#define EN 7
//Declare variables for functions
char user_input;
int x;
int y;
int state;
void setup() {
pinMode(stp, OUTPUT);
pinMode(dir, OUTPUT);
pinMode(MS1, OUTPUT);
pinMode(MS2, OUTPUT);
pinMode(MS3, OUTPUT);
pinMode(EN, OUTPUT);
resetBEDPins(); //Set step, direction, microstep and enable pins to default states
Serial.begin(9600); //Open Serial connection for debugging
Serial.println("Begin motor control");
Serial.println();
//Print function list for user selection
Serial.println("Enter number for control option:");
Serial.println("1. Turn at default microstep mode.");
Serial.println("2. Reverse direction at default microstep mode.");
Serial.println("3. Turn at 1/16th microstep mode.");
Serial.println("4. Step forward and reverse directions.");
Serial.println();
}
One thing worth noting is that the code also initializes the serial connection at 9600 bps. This enables the user (you!) to control the motor's functionality and debug your circuit if needed.
The main loop of the code is pretty simple. The Arduino scans the serial port for input from the user. When it is received, it's compared to the four possible functions for the motor, which are triggered from user input. If no valid input is received, the Arduino prints an error over the serial port. After the requested function is completed, the pins on the Big Easy Driver are reset to the defaults.
language:c
//Main loop
void loop() {
while(Serial.available()){
user_input = Serial.read(); //Read user input and trigger appropriate function
digitalWrite(EN, LOW); //Pull enable pin low to set FETs active and allow motor control
if (user_input =='1')
{
StepForwardDefault();
}
else if(user_input =='2')
{
ReverseStepDefault();
}
else if(user_input =='3')
{
SmallStepMode();
}
else if(user_input =='4')
{
ForwardBackwardStep();
}
else
{
Serial.println("Invalid option entered.");
}
resetBEDPins();
}
}
Driving Stepper Motor in Forward
The first of the four functions this demo sketch enables is a basic example to show the motor spinning in one direction. The direction pin is held LOW, which for our sketch, we define as the 'forward' direction. The sketch then transitions the step pin HIGH, pauses, and then pulls it LOW. Remember, the motor only steps when the step pin transitions from LOW to HIGH, thus we have to switch the state of the pin back and forth. This is repeated 1000 times, and then the Arduino requests more user input to determine the next motor activity.
language:c
//Default microstep mode function
void StepForwardDefault()
{
Serial.println("Moving forward at default step mode.");
digitalWrite(dir, LOW); //Pull direction pin low to move "forward"
for(x= 0; x<1000; x++) //Loop the forward stepping enough times for motion to be visible
{
digitalWrite(stp,HIGH); //Trigger one step forward
delay(1);
digitalWrite(stp,LOW); //Pull step pin low so it can be triggered again
delay(1);
}
Serial.println("Enter new option");
Serial.println();
}
Driving Stepper Motor in Reverse
The reverse function works exactly the same as the forward function. The only difference is that instead of pulling the direction pin LOW, we set it HIGH, thus switching the direction of the motor spin. One thing you can try on either of these first two functions is modifying the motor speed by changing the value in delay()
. It is currently set to 1 microsecond, making each step pulse take 2 microseconds. Increasing the delay will slow down the motor, while decreasing the delay will speed up the motor.
language:c
//Reverse default microstep mode function
void ReverseStepDefault()
{
Serial.println("Moving in reverse at default step mode.");
digitalWrite(dir, HIGH); //Pull direction pin high to move in "reverse"
for(x= 0; x<1000; x++) //Loop the stepping enough times for motion to be visible
{
digitalWrite(stp,HIGH); //Trigger one step
delay(1);
digitalWrite(stp,LOW); //Pull step pin low so it can be triggered again
delay(1);
}
Serial.println("Enter new option");
Serial.println();
}
Microstepping
The third function shows off the different microstepping functionality that the Big Easy Driver provides. To enable the motor to step in 1/16th microsteps, we must set MS1, MS2, and MS3 HIGH. This sets the logic of the board to 1/16th microstep mode. If you want to have the motor step at a different microstep mode, change the settings for one of the MS# pins. Check the truth table in the Hardware Overview section, if you need a reminder of what settings are enabled by the various pin states.
language:c
// 1/16th microstep foward mode function
void SmallStepMode()
{
Serial.println("Stepping at 1/16th microstep mode.");
digitalWrite(dir, LOW); //Pull direction pin low to move "forward"
digitalWrite(MS1, HIGH); //Pull MS1,MS2, and MS3 high to set logic to 1/16th microstep resolution
digitalWrite(MS2, HIGH);
digitalWrite(MS3, HIGH);
for(x= 0; x<1000; x++) //Loop the forward stepping enough times for motion to be visible
{
digitalWrite(stp,HIGH); //Trigger one step forward
delay(1);
digitalWrite(stp,LOW); //Pull step pin low so it can be triggered again
delay(1);
}
Serial.println("Enter new option");
Serial.println();
}
Forward and Reverse
The final motor function available shows how the motor can change direction on the fly. The function works just as the forward and reverse functions above, but switches between states quickly. This example will step the motor 1000 steps forward and then reverse 1000 steps. This allows you to precisely move something with the motor in one direction, and return to the starting position exactly. Precise position control is a great benefit of stepper motors!
language:c
//Forward/reverse stepping function
void ForwardBackwardStep()
{
Serial.println("Alternate between stepping forward and reverse.");
for(x= 1; x<5; x++) //Loop the forward stepping enough times for motion to be visible
{
//Read direction pin state and change it
state=digitalRead(dir);
if(state == HIGH)
{
digitalWrite(dir, LOW);
}
else if(state ==LOW)
{
digitalWrite(dir,HIGH);
}
for(y=0; y<1000; y++)
{
digitalWrite(stp,HIGH); //Trigger one step
delay(1);
digitalWrite(stp,LOW); //Pull step pin low so it can be triggered again
delay(1);
}
}
Serial.println("Enter new option");
Serial.println();
}
Once the requested action is completed, the pins must be set back to the default state to prevent unexpected or unwanted motor behavior. We use the resetBEDPins()
function to achieve this.
language:c
//Reset Easy Driver pins to default states
void resetBEDPins()
{
digitalWrite(stp, LOW);
digitalWrite(dir, LOW);
digitalWrite(MS1, LOW);
digitalWrite(MS2, LOW);
digitalWrite(EN, HIGH);
}
Additional Examples
In addition to the example here, you can also install the AccelStepper Library. There are some additional examples with this library that may be beneficial to you for use with your Big Easy Driver. Download this and install the library in your Arduino libraries directory.
You can also find some additional examples on Brian's Easy Driver examples page. The examples work for both the Easy Driver and Big Easy Driver.