I am trying to dynamically change the speed of a slider on a linear rail using a stepper motor (NEMA 17) an Arduino Uno, a motor driver (SN754410) and a 10K Pot. Also, since the rail has a finite length I am trying to have the slider stop right before it reaches the end. I have done my calculations to translate the speeds into RPMs and the total number of steps the stepper needs to turn for the slider to traverse the entire length of the rail. I am trying to use the stepper motor sketch, which I've modified for my application. My hardware setup is exactly the same as is shown for the stepper motor tutorial. When I upload my code and try to run the motor it does not run but makes a loud vibrating noise, the more I turn the pot the louder it gets but doesn't turn. Here is my code:
#include <Stepper.h>
const int stepsPerRevolution = 200; // change this to fit the number of steps per revolution
// for your motor
// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);
int stepCount = 0; // number of steps the motor has taken
void setup() {
// nothing to do inside the setup
}
void loop() {
// read the sensor value:
int sensorReading = analogRead(A0);
// map it to a range from 0 to 100:
int motorSpeed = map(sensorReading, 0, 1023, 0, 65);
if (motorSpeed >= 0 && motorSpeed <= 9) {myStepper.setSpeed(0); myStepper.step(0);}
if (motorSpeed >= 10 && motorSpeed <= 19) {myStepper.setSpeed(99); myStepper.step(4600);}
if (motorSpeed >= 20 && motorSpeed <= 29) {myStepper.setSpeed(199); myStepper.step(4600);}
if (motorSpeed >= 30 && motorSpeed <= 39) {myStepper.setSpeed(298); myStepper.step(4600);}
if (motorSpeed >= 40 && motorSpeed <= 49) {myStepper.setSpeed(397); myStepper.step(4600);}
if (motorSpeed >= 50 && motorSpeed <= 59) {myStepper.setSpeed(496); myStepper.step(4600);}
}
I am currently using a benchtop power supply at 12V and 0.5 A. The 4600 is what I calculated to be the entire length of the rail in steps of the motor. So my intent is to have the motor turn 4600 steps starting from one end of the rail to the other.
I'm no expert at steppers so I'm sure someone will/could correct me, but at that point myStepper.step() you're telling the motor how many steps to do per revolution, have you tested it using stepsPerRevolution. This could at least get it moving as you want?
Once moving at the different speeds desired put another condition in to stop the motor when desired.
Thank you guys for your replies/suggestions/clarifications. @TomGeorge and @Robin2 The stepper motor is MT-1704HS168A-OB and the technical specs are here:
An update from yesterday; I have changed the mapping of the potentiometer to allow for wider ranges between different speeds. When I turn on the power (12V at 0.5 A) having my pot at zero position, as I turn the pot and it reaches the range 331 - 334 (looking at the Serial Monitor) the motor starts turning and goes the entire length of the rail. However when I turn the pot to a desired speed range and then turn the power on, the motor doesn't turn, i.e. it does not recognize the position of the pot upon startup.
Here is my updated code:
#include <Stepper.h>
const int stepsPerRevolution = 200; // change this to fit the number of steps per revolution
// for your motor
// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);
int stepCount = 0; // number of steps the motor has taken
void setup() {
// nothing to do inside the setup
Serial.begin(9600);
}
void loop() {
// read the sensor value:
int sensorReading = analogRead(A0);
// map it to a range from 0 to 100:
int motorSpeed = map(sensorReading, 0, 1023, 0, 810);
if (motorSpeed >= 0 && motorSpeed <= 200) {myStepper.setSpeed(0.00); myStepper.step(0); Serial.println(motorSpeed);}
if (motorSpeed >= 190 && motorSpeed <= 410) {myStepper.setSpeed(99.00); myStepper.step(4600); Serial.println(motorSpeed);}
if (motorSpeed >= 400 && motorSpeed <= 510) {myStepper.setSpeed(199.00); myStepper.step(4600); Serial.println(motorSpeed);}
if (motorSpeed >= 500 && motorSpeed <= 610) {myStepper.setSpeed(298.00); myStepper.step(4600); Serial.println(motorSpeed);}
if (motorSpeed >= 600 && motorSpeed <= 710) {myStepper.setSpeed(397.00); myStepper.step(4600); Serial.println(motorSpeed);}
if (motorSpeed >= 700 && motorSpeed <= 810) {myStepper.setSpeed(496.00); myStepper.step(4600); Serial.println(motorSpeed);}
}
println_SG:
Thank you guys for your replies/suggestions/clarifications. @TomGeorge and @Robin2 The stepper motor is MT-1704HS168A-OB and the technical specs are here:
That motor requires 1.68 amps and the SN754410 is only rated for 1 amp. Also, as the motor coil resistance is only 1.65 ohms anything over 2.8 volts will overload the motor unless you are using a specialized stepper driver that can limit the current to protect the motor. The Pololu DRV8825 stepper driver should be suitable, but it will probably need a heat sink.
Robin2:
That motor requires 1.68 amps and the SN754410 is only rated for 1 amp. Also, as the motor coil resistance is only 1.65 ohms anything over 2.8 volts will overload the motor unless you are using a specialized stepper driver that can limit the current to protect the motor. The Pololu DRV8825 stepper driver should be suitable, but it will probably need a heat sink.