I'm still confused. L298N has 4 input pins, not just 2. Anyway, if this helps I'm using the following class to drive 5V NEMA 8 stepper with L298N driver.
/*
Bipolar stepper motor NEMA 8 with L298N driver (connect NEMA 8 green and black wires to motor A pins of L298N and red blue wires to motor B pins of L298N)
For ESP32
Version 001, 12.8.2024, Bojan Jurca
*/
#ifndef __L298N_NEMA8__
#define __L298N_NEMA8__
// TUNNING PARAMETERS
#define __L298N_NEMA8_STEP_MODE__ 8 // 4 (1.8 ° / step) or 8 (0.9 ° / step)
#define __L298N_NEMA8_STEPS_PER_AXIS_ROTATION__ (50 * __L298N_NEMA8_STEP_MODE__) // 200 for __L298N_NEMA8_STEP_MODE__ == 4, 400 for 8
class L298N_NEMA8_t {
public:
L298N_NEMA8_t (byte pinIn1, byte pinIn2, byte pinIn3, byte pinIn4) {
// remember pins connected to ULN2003 controller
__pinIn__ [0] = pinIn1;
__pinIn__ [1] = pinIn2;
__pinIn__ [2] = pinIn3;
__pinIn__ [3] = pinIn4;
// set pin mode to OUTPUT
for (byte i = 0; i < 4; i++) {
pinMode (__pinIn__ [i], OUTPUT);
digitalWrite (__pinIn__ [i], 0);
}
}
// ----- speed (speed or rather slowness) -----
unsigned long microsToStayInStep = 1000; // 700 is the lovest value (the fastest speed) possible with no load (for __L298N_NEMA8_STEP_MODE__ == 4, 300 for 8)
// ----- relative movement -----
// (relative) move number of steps from current position
void doSteps (long stepsToDo) {
__lock__ ();
__stepsToDo__ = stepsToDo;
__absoluteStep__ += stepsToDo;
__unlock__ ();
}
// ----- absolute movement -----
// to be called only once - at initialization
void defineInitialStep (long initialStep) {
__lock__ ();
__absoluteStep__ = initialStep;
__unlock__ ();
}
// (absolute) move to target step
void moveToStep (long targetStep) {
__lock__ ();
// correct __absoluteStep__ if the previous move has not cmpleted yet
__absoluteStep__ -= __stepsToDo__;
// do relative movement from here
doSteps (targetStep - __absoluteStep__);
__unlock__ ();
}
// get current step
inline long getCurrentStep () __attribute__((always_inline)) { return __absoluteStep__; }
void stop () {
__lock__ ();
__stepsToDo__ = 0;
for (byte i = 0; i < 4; i++)
digitalWrite (__pinIn__ [i], 0);
__unlock__ ();
}
inline bool isStopped () __attribute__((always_inline)) { return __stepsToDo__ == 0; }
// call this function form loop ()
void doThings () {
__lock__ ();
unsigned long currentMicros = micros ();
if (currentMicros - __lastMicros__ >= microsToStayInStep) { // time to take the next step
__lastMicros__ = currentMicros;
if (__stepsToDo__ > 0) { // do a step in clockwise direction
__step__ = (__step__ + 1) % __L298N_NEMA8_STEP_MODE__; // 4 or 8
for (byte i = 0; i < 4; i ++) digitalWrite (__pinIn__ [i], __state__ [__step__][i]);
__stepsToDo__ --;
} else if (__stepsToDo__ < 0) { // do a step in anti-clockwise direction
__step__ = (__step__ + (__L298N_NEMA8_STEP_MODE__ - 1)) % __L298N_NEMA8_STEP_MODE__; // 4 or 8
for (byte i = 0; i < 4; i ++) digitalWrite (__pinIn__ [i], __state__ [__step__][i]);
__stepsToDo__ ++;
} else { // __stepsToDo__ == 0
stop ();
}
}
__unlock__ ();
}
private:
// locking mechanism
SemaphoreHandle_t __semaphore__ = xSemaphoreCreateRecursiveMutex ();
void __lock__ () { xSemaphoreTakeRecursive (__semaphore__, portMAX_DELAY); }
void __unlock__ () { xSemaphoreGiveRecursive (__semaphore__); }
long __stepsToDo__ = 0; // down-counter of steps yet to be done
volatile byte __pinIn__ [4]; // pins connected to Uln2003 stepper controller
byte __step__ = 0; // operational variable
#if __L298N_NEMA8_STEP_MODE__ == 4
volatile byte __state__ [4][4] = { // [state][pin]
{1, 0, 0, 0}, // coil A current direction 1
{0, 0, 1, 0}, // coil B current direction 1
{0, 1, 0, 0}, // coil A current direction 2
{0, 0, 0, 1} // coil B current direction 2
};
#else
volatile byte __state__ [8][4] = { // [state][pin]
{1, 0, 0, 0}, // coil A current direction 1
{1, 0, 1, 0}, // coil A current direction 1 + coil B current direction 1
{0, 0, 1, 0}, // coil B current direction 1
{0, 1, 1, 0}, // coil A current direction 2 + coil B current direction 1
{0, 1, 0, 0}, // coil A current direction 2
{0, 1, 0, 1}, // coil A current direction 2 + coil B current direction 2
{0, 0, 0, 1}, // coil B current direction 2
{1, 0, 0, 1} // coil A current direction 1 + coil B current direction 2
};
#endif
unsigned long __lastMicros__ = 0;
long __absoluteStep__ = 0;
};
#endif
You can use it like this:
#include "L298N-NEMA8.hpp"
L298N_NEMA8_t stepper (40, 38, 36, 34); // pins connected to L298N
void setup () {
stepper.moveToStep (-80);
}
void loop () {
stepper.doThings ();
}