#include <AccelStepper.h>
#define dirPin 2
#define stepPin 3
#define motorInterfaceType 1
#define stepsPerRev 1600 // defined via driver
#define motorSpeed 3200 // steps/sec (3200 steps/sec = 120 rpm)
long rotations;
long target;
String motorDirection; // direction
AccelStepper stepper = AccelStepper(motorInterfaceType, stepPin, dirPin);
void setup() {
// put your setup code here, to run once:
// Start serial communication:
Serial.begin(9600);
// Declare pins as output:
// pinMode(dirPin,INPUT);
pinMode(stepPin,OUTPUT);
// Set max speed and acceleration/deceleration:
stepper.setMaxSpeed(motorSpeed); // steps/sec
stepper.setAcceleration(motorSpeed); // steps/sec^2
}
void loop() {
// put your main code here, to run repeatedly:
// // Select direction:
Serial.println("Select direction. Enter CW or CCW.");
while (Serial.available()==0) {
// exits loop once input is made
}
// Read direction:
motorDirection=Serial.readString();
motorDirection.trim();
if (motorDirection=="CW" || motorDirection=="cw") {
pinMode(dirPin,OUTPUT);
// digitalWrite(dirPin,HIGH);
}
else if (motorDirection=="CCW" || motorDirection=="ccw") {
pinMode(dirPin,INPUT);
// digitalWrite(dirPin,LOW);
}
else {
Serial.println("Error: enter valid direction");
return; // returns to beginning of if statement
}
Serial.print("The motor will turn ");
Serial.println(motorDirection);
// Display rotations message:
Serial.println("How many rotations would you like to turn?");
while (Serial.available()==0) {
// exits loop once input is made
}
// read rotations message:
rotations=Serial.parseInt();
Serial.print("The motor will turn: ");
Serial.print(rotations);
Serial.println(" rotations");
delay(1000); // wait 1 sec
// move to target
target = stepsPerRev * rotations;
stepper.moveTo(target);
stepper.runToPosition();
exit(0); // exits the loop
}
I can't get my motor to rotate at high speeds. I tried adjusting the voltage on the power supply but that didn't do anything. I used StepperOnline's internal motor control program and I can get the motor to turn at high speeds that way but not when running my Arduino code. I did some trial and error and figured that while running my Arduino code, I can only go about 6000 steps/sec. When using StepperOnline's internal program, I can go like 1000 rpm (around 26,667 steps/sec for a 1600 steps/rev resolution). Anyone know why this would be?
Edit:
For those wondering what I my setup is:
- Power Supply: 350W 60V 5.9A 115/230V Switching Power Supply
- Driver: Closed Loop Stepper Driver
- Motor: Nema 34 Closed Loop Stepper Motor 4.8 Nm
- Controller: Arduino Uno
StepperOnline, the company whom I get my equipment from, has their own software that can be used to manually control/monitor the motor. You use it by connecting the driver to a computer via a RS232 cable (I have a RS232 to USB adapter).