L298N PWM Troubles

I have a L298N Dual H Bridge DC Stepper Motor Board and an unknown motor but I’m having troubles getting good speed control. I have an optical switch accurately tracking the RPM. Controlling the speed (originally using analogWrite), putting the Enable pin on 127 from 255 doesn't change the RPM at all, and only putting the enable pin at 8 causes any change, decreasing the RPM from 140 to 137. I tried using the LEDC system to get a higher resolution, but I am still having the same problems. Does anyone know what's going on? I need an accurate control of the RPM from 40 to 140. Here's my code:

int limitswitch1 = 27;
int enable1Pin = 26; 

const int freq = 16000;
const int pwmChannel = 0;
const int resolution = 12;

long revolutions = 0;
long lastTime = millis();
bool lastState = false;

void setup() {
  Serial.begin(9600);  // initialize serial
  // put your setup code here, to run once:
   pinMode(motor1pin1, OUTPUT);
  pinMode(motor1pin2, OUTPUT);
  
  pinMode(18,   OUTPUT); 
  pinMode(limitswitch1, INPUT_PULLUP);
  digitalWrite(limitswitch1, HIGH);
  analogWrite(18, 1023); //ENA   pin

  pinMode(33, OUTPUT);
  pinMode(25, OUTPUT);
  pinMode(enable1Pin, OUTPUT);

  ledcAttachChannel(enable1Pin, freq, resolution, pwmChannel);
  ledcWrite(enable1Pin, 1023); 

  digitalWrite(33, LOW);
  digitalWrite(25, HIGH);
}

void loop() {
  if (Serial.available() > 0) {
    int receivedValue = Serial.parseInt(); // Read an integer
    if(receivedValue > 0) {
      ledcWrite(enable1Pin, receivedValue); 
    }
  }
   // put your main code here, to run repeatedly:
  if(digitalRead(limitswitch1) == HIGH) {
    if(lastState) {
      revolutions++;
      if(revolutions % 2 == 0) {
        Serial.println(1000.0 / (millis() - lastTime) * 60);
        lastTime = millis();
      }
    }
    lastState = false;
  } else {
    lastState = true;
  }
  delay(1);
}

I get it's probably not the greatest, so tips are recommended (just try not to be condescending, I'm new to this). Regardless, help is appreciated!

Welcome to the forum

You mention using LEDC which implies that you are using an ESP32, which is a 3.3V device. What voltage do the LM298 inputs expect ?

oops I should've specified:

I'm stepping up the 3.3V to 5V to power the optical sensor as well as the logic on the L289N, and I'm using an external power supply to give the extra power for the motor, ~14.5 volts

Logic HIGH switchpoint is 2.3volt, so 3.3volt logic should work without the need for level shifting.
Leo..

These two lines do the same on some processors.
The second line might not be valid on an ESP.

millis() should be declared unsigned long.
Leo..

It doesn't make sense to use analogwrite here. Use ledc for both and if you want individual frequencies, don't use adjacent channels.

1 Like

The L298N is a brushed DC motor driver, not a stepper driver. It will work as a stepper driver only for certain low current, high impedance steppers.

If your motor is low impedance, buy a current limiting stepper driver instead. Pololu has the best selection.

1 Like

I'm pretty sure it's not a stepper motor, but I don't know how to confirm. If it isn't a stepper motor what should I do, try different driver?

Does it have just two wires or four wires comming out of the motor?

What is motor1pin1 and motor1pin2, they are not defined?

Which L298 pins are connected to which ESP pins?

just 2

motor1pin stuff is leftover, i removed that from the sketch.

ENA goes to 26
IN1 goes to 25
IN2 goes to 33

everything else is hooked up externally from the ESP

Then it just a plain DC motor not a stepper.

then any idea what I could do to fix my problem? should I try switching motor drivers?

You can achieve better PWM control if you set the ENA HIGH and apply the PWM to either IN1 or IN2.

Post a picture of it and a wiring diagram of your setup. Pencil and paper is fine.

The type should be obvious. Brushed DC motors usually have only two terminals or leads.

Brushless motors can have two leads, but often can't be controlled with PWM.

First see post #15 and use the simple Arduino analogWrite function, forget about the LEDC stuff for now. analogWrite goes from 0 to 255.