I looking for the best way to control a NEMA 34 stepper motor (1.8º angle) http://www.act-motor.com/product/34hs_en.html with a non-arduino shield. Basically I need to send a PWM square wave on one pin of the arduino.
Most tutorials and code I have come across use four pins and either an h-bridge or other chip to control the stepper motor, using the arduino to generate all four pulse waves. But that is NOT what I need to do.
I have a stepper motor driver http://www.leadshine.com/productdetail.aspx?type=products&category=stepper-products&producttype=stepper-drives&series=M&model=MA860H
The driver receives a 5v+ signal on PUL+ and when PUL- is set LOW by the arduino it will make the motor go one step. (or so it seems!) I my sketch PUL- is connected to Pin 3.
The driver requires a minimum 1.5ms pulse, I was able to create the pulse by using a potentiometer connected to A0 and an oscilloscope until I found the right pulse width, and wrote out to the serial monitor to then hard code that delay into the sketch (310 microseconds)
I then tried using a second potentiometer connected to A1 to set the frequency or delay between the pulses to control the speed of the motor. Which seems to work.
Then I changed the code to have the first potentiometer control the direction (I should use a switch but the potentiometer was already connected). Basically if the pot is >= 500 the motor spins in one direction and <500 it spins the other.
First Problem:
If I do any serial communication however it severely slows down the motor. I believe adapting the blinkwithoutdelay sketch could help but the way it is implemented in the tutorial is confusing to me.
2nd Problem:
I'd like to be able to program in ramps for changing direction and slowing down to a stop. and also functions.
Any help appreciated
Here is what I have working so far, it is very basic, like a "Hello World" for this stepper motor, and eventually need to do this much better.
int dirpin = 2;
int steppin = 3;
int sensorPin1 = A0; // select the input pin for the direction potentiometer
int sensorPin2 = A1; // select the input pin for the delay potentiometer
int dirValue = 0; // variable to store the value coming from the sensor
int sensorValue2 = 0; // variable to store the value coming from the sensor
int pulseWidth = 310; // this creates aprox 1.5microsceond to 2microsecond pulse width
int pulseDelay2 = 0; // variable pulse frequency for speed of motor
void setup()
{
pinMode(dirpin, OUTPUT);
pinMode(steppin, OUTPUT);
Serial.begin(9600);
}
void loop()
{
dirValue = analogRead(sensorPin1); // read direction pot
if (dirValue >= 500){
digitalWrite(dirpin, LOW); // turn CW
delay(5); // delay for driver to accept direction change
}
else {
digitalWrite(dirpin, HIGH); // turn CCW
delay(5); // delay for driver to accept direction change
}
sensorValue2 = analogRead(sensorPin2); // read frequency or speed pot
pulseDelay2 = ((sensorValue2*10)+300); // do some math on the analogue reading, add 300 so frequency is never below pulse width
//Serial.print("width 1 = "); Serial.print(pulseWidth); Serial.print(" delay 2 = "); Serial.println(pulseDelay2);
digitalWrite(steppin, LOW); // This LOW to HIGH change is what creates the
delayMicroseconds(pulseWidth);
digitalWrite(steppin, HIGH); // "Rising Edge" so driver knows to when to step.
delayMicroseconds(pulseDelay2);
}