I've been playing around with a stepper motor using the Arduino motor shield. So far i have this piece of code (see below), which indefinitely moves the carriage in a set direction as long as it's connected to power, given that the rails don't come to an end. How do i get my stepper motor to, for example, move N steps in forward/backward direction? Is there a function i can call for that (something like: moveStepper(forwards, 300 steps) ? And how do you determine the distance the stepper moves - in terms of lead screw rotations or in terms of time? Currently, when i want to reverse the direction, i have to swap HIGH and LOW in the indicated positions in the code (marked: change HIGH/LOW for direction!). I would really appreciate it if someone with a wide stepper motor knowledge could help me out!
int delaylength = 30;
void setup() {
//establish motor direction toggle pins
pinMode(12, OUTPUT); //CH A -- HIGH = forwards and LOW = backwards???
pinMode(13, OUTPUT); //CH B -- HIGH = forwards and LOW = backwards???
//establish motor brake pins
pinMode(9, OUTPUT); //brake (disable) CH A
pinMode(8, OUTPUT); //brake (disable) CH B
}
void loop(){
digitalWrite(9, LOW); //ENABLE CH A
digitalWrite(8, HIGH); //DISABLE CH B
digitalWrite(12, HIGH); //Sets direction of CH A
analogWrite(3, 255); //Moves CH A
delay(delaylength);
digitalWrite(9, HIGH); //DISABLE CH A
digitalWrite(8, LOW); //ENABLE CH B
digitalWrite(13, LOW); //Sets direction of CH B - **change HIGH/LOW for direction!**
analogWrite(11, 255); //Moves CH B
delay(delaylength);
digitalWrite(9, LOW); //ENABLE CH A
digitalWrite(8, HIGH); //DISABLE CH B
digitalWrite(12, LOW); //Sets direction of CH A
analogWrite(3, 255); //Moves CH A
delay(delaylength);
digitalWrite(9, HIGH); //DISABLE CH A
digitalWrite(8, LOW); //ENABLE CH B
digitalWrite(13, HIGH); //Sets direction of CH B - **change HIGH/LOW for direction!**
analogWrite(11, 255); //Moves CH B
delay(delaylength);
}
I tried the code below and it gets the stepper motor buzzing but the carriage moves nowhere. I'm not sure what pins to put as the inputs for the myStepper function, so i just guessed based on this:
"The pin breakdown is as follows:
Function Channel A Channel B
Direction Digital 12 Digital 13
Speed (PWM) Digital 3 Digital 11
Brake Digital 9 Digital 8
Current Sensing Analog 0 Analog 1".
Can anyone tell whether i'm approaching this problem from the right direction or am i doing it completely wrong?
#include <Stepper.h>
const int stepsPerRevolution = 400; // change this to fit the number of steps per revolution
// for your motor
// initialize the stepper library on pins:
Stepper myStepper(stepsPerRevolution, 3, 11, 12, 13);
void setup() {
// set the speed at 60 rpm:
myStepper.setSpeed(60);
// initialize the serial port:
Serial.begin(9600);
}
void loop() {
// step one revolution in one direction:
Serial.println("clockwise");
myStepper.step(stepsPerRevolution);
delay(500);
// step one revolution in the other direction:
Serial.println("counterclockwise");
myStepper.step(-10*stepsPerRevolution);
delay(500);
}
OK, lets guess, its a 1.7 ohm, 1.7A NEMA17 and its totally unsuitable for a motor shield, requiring
a DRV8825 or similar?
But maybe its a 40 ohm 0.3A high impedance stepper than can be driven from a motor shield,
but only at low rpm, since there is no voltage overhead available for fast operations?