Stepper Motor Issue

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);
}

I am using the "FULL STEP 2 PHASE-Ex" As shown in the data sheet: https://www.omc-stepperonline.com/download/8HS15-0304S.pdf to determine which outputs I need to give to the motor driver.

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():wink: 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.

These links may help
Stepper Motor Basics
Simple Stepper Code

...R

I need a stepper motor to step quickly back and forth

Define quickly.

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.

These links may help
Stepper Motor Basics
Simple Stepper Code

...R

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.

My apologies, it is a Nema 8 motor and the data sheet was in the original post, but I'll post it here for clarity.
Motor: https://www.omc-stepperonline.com/nema-8-bipolar-1-8deg-3ncm-4-25oz-in-0-3a-12v-20x20x38mm-4-wires.html
Datasheet: https://www.omc-stepperonline.com/download/8HS15-0304S.pdf

Thanks for your help!

JCA34F:
Define quickly.

The motor needs to be able to step ~800 times/second. The motor will need to stop for most of these steps, and reverse every sixth step.

For example, this would need to be the movement of the motor. Black arrows represent the motor in question which needs to step "quickly."

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.

...R

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).

...R