I need a stepper motor to step quickly back and forth for use in a Heads Up Display. I am using a Nema 17 Stepper Motor with 200 steps, and a L298N Motor Driver.
I have used both <AccelStepper.h> and <Stepper.h> to try to control the motor, but both have resulted in the motor "jumping" whenever it reaches a stopping step, and throwing the position of the reflected laser off-position.
I have resorted to directly manipulating the stepper driver, as shown in the code below.
double stepDelay = 1; // Delay between steps in milliseconds
void setup() {
// X-Motor Pin Setup
DDRH |= (1 << 5);//8 - A
DDRH |= (1 << 6);//9 - A0
DDRB |= (1 << 4);//10 - B
DDRB |= (1 << 5);//11 - B0
DDRD = DDRD | (1 << 3);//Laser
PORTD |= (1 << 3);//Laser On
}
void step1() {
PORTH |= (1 << 5); // A ON
PORTB |= (1 << 4); // B ON
PORTH &= ~(1 << 6); // A0 OFF
PORTB &= ~(1 << 5); // B0 OFF
delay(stepDelay);
}
void step2() {
PORTH &= ~(1 << 5); // A OFF
PORTB |= (1 << 4); // B ON
PORTH |= (1 << 6); // A0 ON
PORTB &= ~(1 << 5); // B0 OFF
delay(stepDelay);
}
void step3() {
PORTH &= ~(1 << 5); // A OFF
PORTB &= ~(1 << 4); // B OFF
PORTH |= (1 << 6); // A0 ON
PORTB |= (1 << 5); // B0 ON
delay(stepDelay);
}
void step4() {
PORTH |= (1 << 5); // A ON
PORTB &= ~(1 << 4); // B OFF
PORTH &= ~(1 << 6); // A0 OFF
PORTB |= (1 << 5); // B0 ON
delay(stepDelay);
}
void stopMotor() {
PORTH &= ~(1 << 5); // A OFF
PORTB &= ~(1 << 4); // B OFF
PORTH &= ~(1 << 6); // A0 OFF
PORTB &= ~(1 << 5); // B0 OFF
}
void loop() {
step1();
step2();
step3();
step4();
stopMotor();
delay(500);
}
However, whenever I run the code it jumps in increments of 4 steps. I have tried only calling one of the step methods(Ex: step1() but the motor does nothing. How can I make the stepper only make one step?
An L298 is a very poor choice for driving a stepper motor - especially if you want performance. The L298 is intended for DC motors and it is old technology, even for that. You need to use a specialised stepper motor driver. And you will need to post a link to the datasheet for your stepper motor before we can suggest a suitable driver - there are hundreds of different Nema 17 motors.
Robin2:
An L298 is a very poor choice for driving a stepper motor - especially if you want performance. The L298 is intended for DC motors and it is old technology, even for that. You need to use a specialised stepper motor driver. And you will need to post a link to the datasheet for your stepper motor before we can suggest a suitable driver - there are hundreds of different Nema 17 motors.
I sadly read the "Stepper Motor Basics" post after I had already purchased the L298. Im looking into getting a couple A4988's right now(They seem to be better suited to stepper motors from what I've read), but am still wondering if there is any way to single step the motor instead of having 4 steps.
Bushvacka:
but am still wondering if there is any way to single step the motor instead of having 4 steps.
I'm not sure what you mean. With an A4988 or DRV8825 driver the motor will move one step for every pulse on the step pin.
My apologies, it is a Nema 8 motor and the data sheet was in the original post,
Sorry, I did not see there was a URL in the Original Post. That motor draws 0.3amps and will work fine with an A4988 or DRV8825. However it has a high coil resistance (40 ohms) and that sort of motor is not designed for high speeds. You may need a high voltage motor power supply to achieve your 800 steps per second and still have some useful torque. Just don't exceed the max voltage of the driver and make sure the current limit is correctly set to protect the motor.
I have some 33ohm motors that I power with an 18v laptop power supply and IIRC they do work up to 1000 steps per second.
You will almost certainly need acceleration to reach the high speed, and to decelerate from them without missing steps. Look up the AccelStepper library.
Robin2:
I'm not sure what you mean. With an A4988 or DRV8825 driver the motor will move one step for every pulse on the step pin.
'I've figured it out now, I just needed to insert the laser turning on an off in between calling the step functions. I have ordered an A4988 and DRV8825 per your suggestion.
Are there any other attributes I should look for choosing a new faster motor?(Besides the coil resistance you mentioned) Sorry, I'm very new to motors.
For two motors with the same torque the one with a higher coil resistance will be less capable of higher speeds.
But you should also be aware that the torque of all stepper motors drops off sharply as speed increases. To some extend a higher power supply voltage counteracts that. The better manufacturers publish graphs showing torque vs speed and voltage.
This means that if you want high speeds it may be wise to choose a more powerful motor (which is probably true of any sort of motor).