This motor has 200 steps per revolution and this code makes the motor spin once. Great but I have a question: this code above tells the motor to take one step every 1ms (500 + 500). So, in 200 steps (one revolution) this motor should take 0.2 second but no, it takes almost a second.
So I ask you: which relation is there with the delayMicroseconds(500) and the RPM of the motor? If I make delayMicroseconds(1) (yes 1) so will my motor take 1 microsecond to every step? Probably not cause it would be too fast, so what is gonna happen if I make 200 steps (using delayMicroseconds(1)), will the motor miss some steps?
@jremington yes, it spins once and restarts that once again and again. I just would like to know what is the meaning of delayMicrosenconds in terms of RPM. Is there a relation? Max, min?
You are probably exceeding the maximum speed of your motor. 200ms per revolution works out to be 300RPM. The A4988 library recommends no more than 200RPM.
When I tried your code, my stepper didn't even rotate until I increased the delay.
I just would like to know what is the meaning of delayMicrosenconds in terms of RPM. Is there a relation? Max, min?
A suitably creative person should have no difficulty with the concept that "1000 microseconds per step" might lead to a certain number of steps per second.
From there, given that 200 steps = one revolution, it is not particularly difficult to calculate rotations per minute (RPM).
jremington:
A suitably creative person should have no difficulty with the concept that "1000 microseconds per step" might lead to a certain number of steps per second.
From there, given that 200 steps = one revolution, it is not particularly difficult to calculate rotations per minute (RPM).
And that would be true of course if the physics of the motor and controller are such that the motor does actually react to 500 microsecond on/off instructions. (Note that the for loop will add actually a bit of delay to loop, so the off period on the pin is actually a bit longer than the on period)
So question is: if you put your code is setup() instead of loop() are you actually seeing your motor spin a FULL 360 degree ? That is are all the 200 steps executed properly or does the motor miss some and you get to see a full turn only because the loop loops and thus motor keeps spinning and eventually gets to a full rotation?
batata004:
So, in 200 steps (one revolution) this motor should take 0.2 second but no, it takes almost a second.
I suspect you are trying to get the motor to move too fast without any acceleration. It is probably missing steps that you are not aware of.
Start with a slow speed and gradually increase the speed to see what is possible. For high speeds you will need to ramp-up the speed so as not to miss steps (and ramp-down when stopping). Stepper motors also suffer from resonance - especially if they have no load.
If your motor gets too less current at a given required torque situation, it will miss steps.
Depending on the given environment the stepper is in, you might need a library which adds acceleration/deceleration (ramp-up/ramp-down as @Robin2 said); so have a look at the AccelStepper library. Be adviced that the maximum speed is limited to 4000 steps/sec with that library. Unless your project needs to exceed this limit, you will be fine with this library.
An A4988 driver has no limit like that. It requires at least a 1µsec HIGH and 1µsec LOW pulse which implies a max of about 500,000 pulses per second.
...R
It recommends not setting the RPM higher than 200. I would guess it is related to your reasoning about acceleration and skipping steps rather than a limitation of the library itself since setting it to 300 produced the exact same result on my stepper as using the OPs code.
@Robin2 sure I want to control it without any library
But my question remains: what does the number inside delayMicroseconds(x) means? In this thread some of you said you are using X = 500, others sayins are using X = 800.
Let's do the math: if X = 500us, so the period of each step will be 1ms, so if the motor is a 200/step it will run at 300 RPM which is too HIGH cause many of you said I should be below 300 RPM
So, if X = 500us, RPM = 300. Is this correct?
What value of X do you recommend in which I will have the maximum torque? High values of X (and low RPM) causes better torque?
Torque and number of degree stepper are not related. You.can buy 200 step motor that will have different torques
For a given voltage, torque and speed in a DC motor are said to be inversely proportional. The speed-torque curve that you see on data sheets is only valid for the rated voltage and the motor will operate on that curve. So if torque goes up, the speed will follow that curve and go down.
batata004:
But my question remains: what does the number inside delayMicroseconds(x) means? In this thread some of you said you are using X = 500, others sayins are using X = 800.
This is an extract from my code in Simple Stepper Code. Please study the full program to see the context.
for(int n = 0; n < numberOfSteps; n++) {
digitalWrite(stepPin, HIGH);
delayMicroseconds(pulseWidthMicros); // this line is probably unnecessary
digitalWrite(stepPin, LOW);
delay(millisbetweenSteps);
digitalWrite(ledPin, !digitalRead(ledPin));
}
I hope the variable names are self-explanatory.
If not, please let me know what you don't understand.
@Robin2 I really apprecite your help with this. Your code is really easy to understand and modify, it certainly is helping lots of people that needs a simple code.
But I still have this question: how to make a single step. One only step. If I use this code of yours only once:
digitalWrite(stepPin, HIGH);
delayMicroseconds(pulseWidthMicros); // this line is probably unnecessary
digitalWrite(stepPin, LOW);
delay(millisbetweenSteps);
You agree with me that if there is ONLY ONE step the delay(millisbetweenSteps); will have no need, correct? So how will the motor know the speed to make this one step?
Pelase delete this thread for some reason it got duplicated. The problem was wiring!. @J-M-L discovered that wiring could be my problem and indeed!!! The wires were in wrong order.
batata004:
So how will the motor know the speed to make this one step?
There is no such thing as the speed for one step. A single step always happens as fast as the motor's magnetism can achieve. A stepper motor moves as a series of jerks. You can change the interval between the jerks. But not the speed of the jerks themselves.
Using microstepping reduces the distance moved in each jerk - but there will still be jerks.
And why have you now got 3 Threads going? Please click Report to Moderator and ask for them to be merged so we have all the info in one place.