And I am using an example code that I found in the forum. Since this stepper has 200 steps per revolution, in order to turn 90º, I have to make it run 50 steps in each direction, which is what I am doing in the code, but in reality, the stepper is turning less than 90º. Is there something wrong in the code?
thanks!
const int M0 = 3;
const int M1 = 4;
const int M2 = 5;
const int stepPin = 6;
const int dirPin = 7;
int numberOfSteps = 50;
int millisbetweenSteps = 10;
void setup() {
pinMode(dirPin, OUTPUT);
pinMode(stepPin, OUTPUT);
pinMode(M0, OUTPUT);
pinMode(M1, OUTPUT);
pinMode(M2, OUTPUT);
// digitalWrite(M0,HIGH);
// digitalWrite(M1,HIGH);
// digitalWrite(M2,HIGH);
digitalWrite(M0, LOW);
digitalWrite(M1, LOW);
digitalWrite(M2, LOW);
}
void loop() {
digitalWrite(dirPin, HIGH);
for (int n = 0; n < numberOfSteps; n++) {
digitalWrite(stepPin, HIGH);
digitalWrite(stepPin, LOW);
delay(millisbetweenSteps);
}
digitalWrite(dirPin, LOW);
for (int n = 0; n < numberOfSteps; n++) {
digitalWrite(stepPin, HIGH);
digitalWrite(stepPin, LOW);
delay(millisbetweenSteps);
}
}
Only thing I see that might possibly be an issue is that you go from HIGH to LOW almost instantly. Maybe try adding your delay between there and see what happens.
Later, when you want other things to happen while the steppers are turning, you'll want to look in to using millis() timing.
I should add, I've not tried to control that type of stepper as yet with code written by myself.
As DTM says, add a delay between the 2 step changes. Start using a 1000 microseconds and tune it down. Maybe also add a delay after the Dir is set to make sure the driver has settled down.
thanks guys! I tried it out and didnt see much change. I also slowed down the pace of the turning, and actually, I think it was ok in the first place, but because the change was so quick, the eye doesnt recognize the desired position has been reached.
If you're trying to gauge angles by looking at the end if the shaft, put a piece of tape on it (like a flag). Much easier for the eye to see what is really happening then.
You have not told us what stepper motor driver you are using.
If you are using an A4988 or DRV8825 then the short pulse provided by successive digitalWrite()s will be fine.
However I suggest you start testing with a much bigger value for millisBetweenSteps - a value of 500 will give 2 steps per second. If that works experiment with smaller values. If you want a stepper motor to move fast you will need to use acceleration - like any other motor they won't go from zero to high speed instantly.
Another important factor is motor power supply voltage. With a driver such as the A4988 you can limit the current to protect the motor and use a high voltage for better performance at higher speeds.