OVERVIEW
In this tutorial we will see how to control 2 stepper motor simultaneously using the Arduino Serial Monitor.
To help us achieve this we will be using the AccelStepper library developed by ‘Mike McCauley‘ which enable us to control up to 10 steppers at the same time if we want.
In this Part 1 we will look at the code that will control the stepper motor and how to use the Serial Monitor to send values to the Arduino to move the steppers.
In the next parts we will modify the code to calculate the speed and acceleration of the steppers automatically and add more modules (LCD, Buttons, etc…) to make our Camera Slider more portable.
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
Here are the connection needed for this tutorial:
To power the Stepper Motors connected to the Easy Driver boards we are using a 12V 1A power supply.
The GND and 5V (out) of the Power Supply are connected to the breadboard power rails.
GND and 5V (in) of the Easy Driver boards are connected to the breadboard power rails.
Each pair of winding of the Stepper motors are connected to the A-B pins of the Easy Drivers.
The GND of the UNO is connected to the breadboard GND power rail.
Each Easy Driver GND is connected to the breadboard GND power rail.
Pin 2 and 3 of the UNO are connected to the STEP and DIR pins of one Easy Driver.
Pin 5 and 6 of the UNO are connected to the STEP and DIR pins of the other Easy Driver.
THE CODE
As you see the code needed is quite short, since all we are doing is sending the ‘Move To’ values from the Serial Monitor and using those to make the stepper move.
When we first power up the Arduino, the Stepper motors have a position value of ‘zero’.
So if we enter a minus number the stepper will move counter clockwise and a positive number will move clockwise. To return to the starting point we just have to enter a value of zero.
Since we are controlling 2 stepper motors, we have to separate the values using a comma in the Serial Monitor: X,Z.
In the following tutorials, we will add more functions.
As always, Don’t forget to watch our Tutorial video for more information.
/* Arduino Multiple Stepper Control Using 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
// UNO Pin 2 connected to STEP pin of Easy Driver
// UNO Pin 3 connected to DIR pin of Easy Driver
AccelStepper stepperZ(1, 5, 6); // 1 = Easy Driver interface
// UNO Pin 5 connected to STEP pin of Easy Driver
// UNO Pin 6 connected to DIR pin of Easy Driver
// Stepper Travel Variables
long TravelX; // Used to store the X value entered in the Serial Monitor
long TravelZ; // Used to store the Z value entered in the Serial Monitor
int move_finished=1; // Used to check if move is completed
void setup() {
Serial.begin(9600); // Start the Serial monitor with speed of 9600 Bauds
// Print out Instructions on the Serial Monitor at Start
Serial.println("Enter Travel distance seperated by a comma: X,Z ");
Serial.print("Enter Move Values Now: ");
// Set Max Speed and Acceleration of each Steppers
stepperX.setMaxSpeed(500.0); // Set Max Speed of X axis
stepperX.setAcceleration(500.0); // Acceleration of X axis
stepperZ.setMaxSpeed(250.0); // Set Max Speed of Y axis slower for rotation
stepperZ.setAcceleration(250.0); // Acceleration of Y axis
}
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 Steppers
TravelX= Serial.parseInt(); // Put First numeric value from buffer in TravelX variable
Serial.print(TravelX);
Serial.print(" X Travel , ");
TravelZ= Serial.parseInt(); // Put Second numeric value from buffer in TravelZ variable
Serial.print(TravelZ);
Serial.println(" Y Travel ");
stepperX.moveTo(TravelX); // Set new move position for X Stepper
stepperZ.moveTo(TravelZ); // Set new move position for Z Stepper
delay(1000); // Wait 1 seconds before moving the Steppers
Serial.print("Moving Steppers into position...");
}
// Check if the Steppers have reached desired position
if ((stepperX.distanceToGo() != 0) || (stepperZ.distanceToGo() !=0)) {
stepperX.run(); // Move Stepper X into position
stepperZ.run(); // Move Stepper Z into position
}
// If move is completed display message on Serial Monitor
if ((move_finished == 0) && (stepperX.distanceToGo() == 0) && (stepperZ.distanceToGo() == 0)) {
Serial.println("COMPLETED!");
Serial.println("");
Serial.println("Enter Next Move Values (0,0 for reset): "); // Get ready for new Serial monitor values
move_finished=1; // Reset move variable
}
}
TUTORIAL VIDEO
DOWNLOAD
Copy the above Sketch code in your Arduino IDE software to program your Arduino.
Used Libraries:
Download The AccelStepper Library
created by ‘Mike McCauley’ :
Once downloaded, just extract the content of the zip files inside your “arduino/libraries” folder, and make sure to restart the Arduino IDE (Close and Reopen) software so it detect this newly installed library.
Comments