L298N Cant powered my 2 DC motors

Good day! I just want to ask why my L298N can't run both of my DC motors.

It works properly with one motor, but when I try to run both, they don't work.

I am currently using a 12V 2A power supply for my L298N seperate to the power of my ESP32.

Here is the wiring setup:

  • +12V = Positive terminal of the 12V 2A power supply
  • GND = Ground of the 12V 2A power supply
  • OUT1 and OUT2 = Motor 1
  • OUT3 and OUT4 = Motor 2
  • IN1 = ESP32 D32
  • IN2 = ESP32 D33
  • IN3 = ESP32 D27
  • IN4 = ESP32 D26

The power supply is probably inadequate.

Post a link to the motor data sheet or product page, and a complete wiring diagram, with pins and connections clearly labeled. Hand drawn is preferred.

1 Like

Which ESP32?
There are several and some of the pins you selected may be inputs only or may not exist.
Best to use the GPIO numbers instead.

Then you need GND connection between Esp32 and L298

1 Like

I was thinking the same, but having no common ground would not explain why each motor works ok. It's only when both motors try to run that the problem is seen.

I also think it could be a power supply problem.

Definitely not.
But it's missing from the wiring description...
And we still don't know anything about the motors...

1 Like

Im using ESP32 WROOM 32

  • L298N +12V = Positive terminal of the 12V 2A power supply
  • L298N GND = Ground of the 12V 2A power supply
  • L298N OUT1 and OUT2 = Motor 1
  • L298N OUT3 and OUT4 = Motor 2
  • L298N IN1 = ESP32 GPIO32
  • L298N IN2 = ESP32 GPIO33
  • L298N IN3 = ESP32 GPIO27
  • L298N IN4 = ESP32 GPIO26

Im sorry but i dont have any diagram, i just have this pins connected

  • L298N +12V = Positive terminal of the 12V 2A power supply
  • L298N GND = Ground of the 12V 2A power supply
  • L298N OUT1 and OUT2 = Motor 1
  • L298N OUT3 and OUT4 = Motor 2
  • L298N IN1 = ESP32 GPIO32
  • L298N IN2 = ESP32 GPIO33
  • L298N IN3 = ESP32 GPIO27
  • L298N IN4 = ESP32 GPIO26

Connect
L298 GND = ESP32 GND

There is a difference between D32 and GPIO32. Which do you use in the code?

I already did it but still same problem

Post your code using using code tags <CODE>

#define FAN_IN1 32
#define FAN_IN2 33
#define FAN2_IN3 27 
#define FAN2_IN4 26 

this is what i did in my code

post the entire code please

#include <WiFi.h>
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <Wire.h>
#include <DHTesp.h>
#include <ArduinoJson.h>
#include <SPIFFS.h>


const char* ssid = "SSID";
const char* password = "PASS";


#define RED_LED 12
#define GREEN_LED 13
#define BLUE_LED 14
#define LIGHT_1 2
#define LIGHT_2 4
#define LIGHT_3 5
#define LIGHT_4 18
#define LIGHT_5 19
#define FAN_IN1 32
#define FAN_IN2 33
#define FAN2_IN3 27 
#define FAN2_IN4 26  
#define DHT_PIN 15


float temperature = 0.0;
float humidity = 0.0;
bool fanStatus = false;
bool fan2Status = false;  
bool fanAutoMode = true;  
bool fan2AutoMode = true; 
bool light1Status = false;
bool light2Status = false;
bool light3Status = false;
bool light4Status = false;
bool light5Status = false;
String rgbStatus = "OFF";


void setRedLight();
void setGreenLight();
void setBlueLight();
void setLightsOff();
void turnFanOn();
void turnFanOff();
void turnFan2On();   // New function for second fan
void turnFan2Off();  // New function for second fan
void updateSensorData();
String getDeviceStates();


DHTesp dht;
AsyncWebServer server(80);
AsyncEventSource events("/events");


void setRedLight() {
  digitalWrite(RED_LED, HIGH);
  digitalWrite(GREEN_LED, LOW);
  digitalWrite(BLUE_LED, LOW);
}

void setGreenLight() {
  digitalWrite(RED_LED, LOW);
  digitalWrite(GREEN_LED, HIGH);
  digitalWrite(BLUE_LED, LOW);
}

void setBlueLight() {
  digitalWrite(RED_LED, LOW);
  digitalWrite(GREEN_LED, LOW);
  digitalWrite(BLUE_LED, HIGH);
}

void setLightsOff() {
  digitalWrite(RED_LED, LOW);
  digitalWrite(GREEN_LED, LOW);
  digitalWrite(BLUE_LED, LOW);
}


void turnFanOn() {
  digitalWrite(FAN_IN1, LOW);
  digitalWrite(FAN_IN2, HIGH);
  fanStatus = true;
}

void turnFanOff() {
  digitalWrite(FAN_IN1, LOW);
  digitalWrite(FAN_IN2, LOW);
  fanStatus = false;
}


void turnFan2On() {
  digitalWrite(FAN2_IN3, LOW);
  digitalWrite(FAN2_IN4, HIGH);
  fan2Status = true;
}

void turnFan2Off() {
  digitalWrite(FAN2_IN3, LOW);
  digitalWrite(FAN2_IN4, LOW);
  fan2Status = false;
}


void updateSensorData() {
  TempAndHumidity data = dht.getTempAndHumidity();


  if (!isnan(data.temperature) && !isnan(data.humidity)) {
    temperature = data.temperature;
    humidity = data.humidity;
  }
}


String getDeviceStates() {
  JsonDocument doc;

  doc["temperature"] = temperature;
  doc["humidity"] = humidity;
  doc["fan"] = fanStatus;
  doc["fanAutoMode"] = fanAutoMode;
  doc["fan2"] = fan2Status;           // Add fan2 status to JSON
  doc["fan2AutoMode"] = fan2AutoMode; // Add fan2 auto mode to JSON
  doc["light1"] = light1Status;
  doc["light2"] = light2Status;
  doc["light3"] = light3Status;
  doc["light4"] = light4Status;
  doc["light5"] = light5Status;
  doc["rgb"] = rgbStatus;

  String jsonString;
  serializeJson(doc, jsonString);
  return jsonString;
}

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


  if (!SPIFFS.begin(true)) {
    Serial.println("An error occurred while mounting SPIFFS");
    return;
  }


  pinMode(RED_LED, OUTPUT);
  pinMode(GREEN_LED, OUTPUT);
  pinMode(BLUE_LED, OUTPUT);
  pinMode(LIGHT_1, OUTPUT);
  pinMode(LIGHT_2, OUTPUT);
  pinMode(LIGHT_3, OUTPUT);
  pinMode(LIGHT_4, OUTPUT);
  pinMode(LIGHT_5, OUTPUT);
  pinMode(FAN_IN1, OUTPUT);
  pinMode(FAN_IN2, OUTPUT);
  pinMode(FAN2_IN3, OUTPUT);  // Initialize new fan pins
  pinMode(FAN2_IN4, OUTPUT);  // Initialize new fan pins


  digitalWrite(LIGHT_1, LOW);
  digitalWrite(LIGHT_2, LOW);
  digitalWrite(LIGHT_3, LOW);
  digitalWrite(LIGHT_4, LOW);
  digitalWrite(LIGHT_5, LOW);
  setLightsOff();
  turnFanOff();
  turnFan2Off();  // Initialize new fan as off


  dht.setup(DHT_PIN, DHTesp::DHT11);


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


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


  server.on("/", HTTP_GET, [](AsyncWebServerRequest * request) {
    request->send(SPIFFS, "/index.html", "text/html");
  });


  server.on("/style.css", HTTP_GET, [](AsyncWebServerRequest * request) {
    request->send(SPIFFS, "/style.css", "text/css");
  });


  server.on("/script.js", HTTP_GET, [](AsyncWebServerRequest * request) {
    request->send(SPIFFS, "/script.js", "application/javascript");
  });


  server.on("/status", HTTP_GET, [](AsyncWebServerRequest * request) {
    request->send(200, "application/json", getDeviceStates());
  });


  server.on("/light1", HTTP_POST, [](AsyncWebServerRequest * request) {
    if (request->hasParam("state", true)) {
      String state = request->getParam("state", true)->value();
      light1Status = (state == "1");
      digitalWrite(LIGHT_1, light1Status);
      request->send(200, "text/plain", "OK");
    } else {
      request->send(400, "text/plain", "Bad Request");
    }
  });

  server.on("/light2", HTTP_POST, [](AsyncWebServerRequest * request) {
    if (request->hasParam("state", true)) {
      String state = request->getParam("state", true)->value();
      light2Status = (state == "1");
      digitalWrite(LIGHT_2, light2Status);
      request->send(200, "text/plain", "OK");
    } else {
      request->send(400, "text/plain", "Bad Request");
    }
  });

  server.on("/light3", HTTP_POST, [](AsyncWebServerRequest * request) {
    if (request->hasParam("state", true)) {
      String state = request->getParam("state", true)->value();
      light3Status = (state == "1");
      digitalWrite(LIGHT_3, light3Status);
      request->send(200, "text/plain", "OK");
    } else {
      request->send(400, "text/plain", "Bad Request");
    }
  });
  
  server.on("/light4", HTTP_POST, [](AsyncWebServerRequest * request) {
    if (request->hasParam("state", true)) {
      String state = request->getParam("state", true)->value();
      light4Status = (state == "1");
      digitalWrite(LIGHT_4, light4Status);
      request->send(200, "text/plain", "OK");
    } else {
      request->send(400, "text/plain", "Bad Request");
    }
  });
  
  server.on("/light5", HTTP_POST, [](AsyncWebServerRequest * request) {
    if (request->hasParam("state", true)) {
      String state = request->getParam("state", true)->value();
      light5Status = (state == "1");
      digitalWrite(LIGHT_5, light5Status);
      request->send(200, "text/plain", "OK");
    } else {
      request->send(400, "text/plain", "Bad Request");
    }
  });


  server.on("/fan", HTTP_POST, [](AsyncWebServerRequest * request) {
    if (request->hasParam("state", true) && request->hasParam("mode", true)) {
      String state = request->getParam("state", true)->value();
      String mode = request->getParam("mode", true)->value();

      fanAutoMode = (mode == "auto");

      if (!fanAutoMode) {
        fanStatus = (state == "1");
        if (fanStatus) {
          turnFanOn();
        } else {
          turnFanOff();
        }
      }

      request->send(200, "text/plain", "OK");
    } else {
      request->send(400, "text/plain", "Bad Request");
    }
  });


  server.on("/fan2", HTTP_POST, [](AsyncWebServerRequest * request) {
    if (request->hasParam("state", true) && request->hasParam("mode", true)) {
      String state = request->getParam("state", true)->value();
      String mode = request->getParam("mode", true)->value();

      fan2AutoMode = (mode == "auto");

      if (!fan2AutoMode) {
        fan2Status = (state == "1");
        if (fan2Status) {
          turnFan2On();
        } else {
          turnFan2Off();
        }
      }

      request->send(200, "text/plain", "OK");
    } else {
      request->send(400, "text/plain", "Bad Request");
    }
  });


  events.onConnect([](AsyncEventSourceClient * client) {
    if (client->lastId()) {
      Serial.printf("Client reconnected! Last message ID that it got is: %u\n", client->lastId());
    }

    client->send(getDeviceStates().c_str(), "status", millis(), 1000);
  });
  server.addHandler(&events);


  server.begin();
}

void loop() {
  static unsigned long lastSensorUpdate = 0;
  static unsigned long lastEventUpdate = 0;


  if (millis() - lastSensorUpdate >= 1000) {
    updateSensorData();
    lastSensorUpdate = millis();


    if (temperature >= 33) {
      setRedLight();
      rgbStatus = "red";
    } else if (temperature < 33 && temperature >= 23) {
      setGreenLight();
      rgbStatus = "green";
    } else {
      setBlueLight();
      rgbStatus = "blue";
    }


    if (fanAutoMode) {
      if (temperature >= 33) {
        turnFanOn();
      } else {
        turnFanOff();
      }
    }
    


    if (fan2AutoMode) {
      if (temperature >= 35) {  
        turnFan2On();
      } else {
        turnFan2Off();
      }
    }
  }

  if (millis() - lastEventUpdate >= 1000) {
    events.send(getDeviceStates().c_str(), "status", millis());
    lastEventUpdate = millis();
  }
}

and Im using Output DC12V = 2A AC/DC Adapter

this is the ESP32 i used

So if fan2AutoMode == true and temperature > 35 the fan is still off, correct?

Are both the enable A and B jumpers on

Hey sorry for the late reply,

No, i have an interface in web, when i try to manually turning on one motor, it works but when i run it both manually at the same time, it will not work and the motor that is currently running will stop.

Yes

How do you run them manually?
Is there extra switches ?