OVERVIEW
Following in the foot steps of our last tutorial, we will now see how to achieve the same results using the popular “AccelStepper” library.
Instead of using a joystick to control the stepper motor movements after Homing, we will be using the Serial Monitor window.
PARTS USED
EasyDriver Stepper Driver
Arduino NANO
Stepper Motor NEMA 17
These are Amazon affiliate links...
They don't cost you anything and it helps me keep the lights on
if you buy something on Amazon. Thank you!
CONNECTIONS
This time we are using the Arduino NANO instead of the UNO.
Since the NANO is basically a smaller version of the UNO (give or take a few differences), the connections would be similar, but since we are using the AccelStepper library we need even less connections.
The Easy Driver:
Pin 2 of the NANO is connected to STEPS Pin 3 to DIR and GND is connected to a GND pin on the Arduino
*The Voltage and GND (at the top of the Easy Driver) are connected to a 12V 1A power supply.
The NEMA 17 motor we are using has a max amperage draw of around 0.45A.
Stepper Motor Connections:
The 4 leads of the NEMA stepper (2 per coils), are connected directly to the Easy Driver A and B group pins.
A quick way to identify which wires are part of the same coil is to connect two of those four wires together and if you feel resistance when trying to turn the stepper motor shaft, that means that those 2 wires are part of the same coil.
The limit switch NO (Normally Open) pin is connected to pin 9 of the Arduino
The COM (common) pin of the switch is connected to a GND pin on the Arduino
THE CODE
As in our last tutorial, we want to start homing the stepper right away at startup.
The way the AccelStepper library works, is that at startup the position value is always zero, then you tell the library to move to a position relative to the zero position.
For example if you set the position to “move to” to 300, and then issue the “run” command, the stepper will move 300 steps away clockwise from the zero position.
If after this, you set the position to 350, the stepper will move clockwise 50 steps, since it his already at the position 300.
So the position to move to is relative to the current position.
A negative number will move the stepper counter clockwise.
In this tutorial we are using the Serial Monitor window to control the movements of the stepper once the Homing is complete.
As always for more information about the tutorial and explanation of the code please watch our tutorial video.
/* Motor Homing code using AccelStepper and the Serial Monitor
Created by Yvan / https://Brainy-Bits.com
This code is in the public domain...
You can: copy it, use it, modify it, share it or just plain ignore it!
Thx!
*/
#include "AccelStepper.h"
// Library created by Mike McCauley at http://www.airspayce.com/mikem/arduino/AccelStepper/
// AccelStepper Setup
AccelStepper stepperX(1, 2, 3); // 1 = Easy Driver interface
// NANO Pin 2 connected to STEP pin of Easy Driver
// NANO Pin 3 connected to DIR pin of Easy Driver
// Define the Pins used
#define home_switch 9 // Pin 9 connected to Home Switch (MicroSwitch)
// Stepper Travel Variables
long TravelX; // Used to store the X value entered in the Serial Monitor
int move_finished=1; // Used to check if move is completed
long initial_homing=-1; // Used to Home Stepper at startup
void setup() {
Serial.begin(9600); // Start the Serial monitor with speed of 9600 Bauds
pinMode(home_switch, INPUT_PULLUP);
delay(5); // Wait for EasyDriver wake up
// Set Max Speed and Acceleration of each Steppers at startup for homing
stepperX.setMaxSpeed(100.0); // Set Max Speed of Stepper (Slower to get better accuracy)
stepperX.setAcceleration(100.0); // Set Acceleration of Stepper
// Start Homing procedure of Stepper Motor at startup
Serial.print("Stepper is Homing . . . . . . . . . . . ");
while (digitalRead(home_switch)) { // Make the Stepper move CCW until the switch is activated
stepperX.moveTo(initial_homing); // Set the position to move to
initial_homing--; // Decrease by 1 for next move if needed
stepperX.run(); // Start moving the stepper
delay(5);
}
stepperX.setCurrentPosition(0); // Set the current position as zero for now
stepperX.setMaxSpeed(100.0); // Set Max Speed of Stepper (Slower to get better accuracy)
stepperX.setAcceleration(100.0); // Set Acceleration of Stepper
initial_homing=1;
while (!digitalRead(home_switch)) { // Make the Stepper move CW until the switch is deactivated
stepperX.moveTo(initial_homing);
stepperX.run();
initial_homing++;
delay(5);
}
stepperX.setCurrentPosition(0);
Serial.println("Homing Completed");
Serial.println("");
stepperX.setMaxSpeed(1000.0); // Set Max Speed of Stepper (Faster for regular movements)
stepperX.setAcceleration(1000.0); // Set Acceleration of Stepper
// Print out Instructions on the Serial Monitor at Start
Serial.println("Enter Travel distance (Positive for CW / Negative for CCW and Zero for back to Home): ");
}
void loop() {
while (Serial.available()>0) { // Check if values are available in the Serial Buffer
move_finished=0; // Set variable for checking move of the Stepper
TravelX= Serial.parseInt(); // Put numeric value from buffer in TravelX variable
if (TravelX < 0 || TravelX > 1350) { // Make sure the position entered is not beyond the HOME or MAX position
Serial.println("");
Serial.println("Please enter a value greater than zero and smaller or equal to 1350.....");
Serial.println("");
} else {
Serial.print("Moving stepper into position: ");
Serial.println(TravelX);
stepperX.moveTo(TravelX); // Set new moveto position of Stepper
delay(1000); // Wait 1 seconds before moving the Stepper
}
}
if (TravelX >= 0 && TravelX <= 1350) {
// Check if the Stepper has reached desired position
if ((stepperX.distanceToGo() != 0)) {
stepperX.run(); // Move Stepper into position
}
// If move is completed display message on Serial Monitor
if ((move_finished == 0) && (stepperX.distanceToGo() == 0)) {
Serial.println("COMPLETED!");
Serial.println("");
Serial.println("Enter Travel distance (Positive for CW / Negative for CCW and Zero for back to Home): ");
move_finished=1; // Reset move variable
}
}
}
TUTORIAL VIDEO
DOWNLOAD
Copy the above Sketch code you want to use above in your Arduino IDE software to program your Arduino.
Download the AccelStepper library created by Mike McCauley here