ESP32 + L298N: Issue Controlling DC Motor Speed

Hi everyone,

I’m using an ESP32 and L298N to control a DC motor, and I’ve set up a web interface to adjust the motor speed. Here's my hardware setup:

  • ESP32
  • L298N
  • Power: 6 AA batteries in series
  • Motor connected to the L298N

Problem:
When I connect the ENA pin of the L298N to GPIO 19 on the ESP32 for PWM speed control, the motor doesn’t run at all. However:

  1. If I remove the ENA connection and use the jumper cap to short ENA on the L298N, the motor runs fine (but speed cannot be controlled).
  2. If I connect ENA directly to the ESP32’s 5V power output, the motor runs (but speed still cannot be controlled).

Questions:

  1. Is this a hardware wiring issue or is there a limitation with the L298N’s ENA pin?
  2. Is my PWM setup correct in the code?
  3. Is there a better way to control the motor speed using the ESP32 and L298N?

Here’s my schematics:
Note: Battery are all AA Battery

Here’s my code:

#include <WiFi.h>
#include <WebServer.h>

#define IN1 5    
#define IN2 18   
#define ENA 19   

const char* ssid = "xxxx";  
const char* password = "xxxx";   

WebServer server(80);  

int speed = 128;  

void setup() {
  Serial.begin(115200);

  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }
  Serial.println("Connected to WiFi");

  Serial.print("ESP32 IP Address: ");
  Serial.println(WiFi.localIP());

  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);
  pinMode(ENA, OUTPUT);

  ledcAttach(ENA, 30000, 8);

  server.on("/", HTTP_GET, handleRoot);
  server.on("/forward", HTTP_GET, handleForward);
  server.on("/backward", HTTP_GET, handleBackward);
  server.on("/stop", HTTP_GET, handleStop);
  server.on("/speed", HTTP_GET, handleSpeed);

  server.begin();
}

void loop() {
  server.handleClient();
}

void handleRoot() {
  String html = "<html><body>";
  html += "<h1>Control Motor</h1>";
  html += "<button onclick=\"location.href='/forward'\">Forward</button><br><br>";
  html += "<button onclick=\"location.href='/backward'\">Backward</button><br><br>";
  html += "<button onclick=\"location.href='/stop'\">Stop</button><br><br>";
  html += "<form action='/speed' method='get'>Set Speed (0-255): ";
  html += "<input type='number' name='value' min='0' max='255'>";
  html += "<input type='submit' value='Set Speed'></form><br>";
  html += "</body></html>";
  server.send(200, "text/html", html);
}

void handleForward() {
  digitalWrite(IN1, HIGH);
  digitalWrite(IN2, LOW);
  ledcWrite(0, speed);
  String html = "<html><body><h1>Motor Moving Forward</h1><a href='/'>Back</a></body></html>";
  server.send(200, "text/html", html);
}

void handleBackward() {
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, HIGH);
  ledcWrite(0, speed);
  String html = "<html><body><h1>Motor Moving Backward</h1><a href='/'>Back</a></body></html>";
  server.send(200, "text/html", html);
}

void handleStop() {
  ledcWrite(0, 0);
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, LOW);
  String html = "<html><body><h1>Motor Stopped</h1><a href='/'>Back</a></body></html>";
  server.send(200, "text/html", html);
}

void handleSpeed() {
  if (server.hasArg("value")) {
    int newSpeed = server.arg("value").toInt();
    if (newSpeed >= 0 && newSpeed <= 255) {
      speed = newSpeed;
      String html = "<html><body><h1>Speed Set to " + String(speed) + "</h1><a href='/'>Back</a></body></html>";
      server.send(200, "text/html", html);
    } else {
      server.send(400, "text/plain", "Invalid speed value! Must be between 0 and 255.");
    }
  } else {
    server.send(400, "text/plain", "Speed value missing!");
  }
}

You need to post the description of the hardware and its name is "Schematics".

done just uploaded it!

just uploaded it

Please add new information in a new reply. Else the following replies look odd.

You tried, but... That's not a schematic.
Sorry but that picture is useless to me. You can make Your grandmother wire it without understanding anything. Helpers need to understand.
Helpers that don't play daily with exactly those circuits have no help of pin positions.

Sorry, I'm out.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.