L298n Motor driver does not work with 2 motors

I have a L298n motor driver with an arduino mega clone. My motors are taken out of a servo motor. I kept the gearbox but got rid of the circuits. The arduino drives one motor just fine but when I drive 2 at a time it doesn't work and I have to switch arduino pins to get it to work again.

#include <configureBT.h>

#include <Ultrasonic.h>

// Motor A 
int enA = 13;
int in1 = 10;
int in2 = 9;
// Motor B 
int enB = 12;
int in3 = 8;
int in4 = 7;
//Varibles and Object declaration
long durationRight;
int distanceRight;
long durationLeft;
int distanceLeft;
long durationFront;
int distanceFront;
long durationBack;
int distanceBack;
const int trigPinRight = 4;
const int echoPinRight = 5;
const int trigPinLeft = 6;
const int echoPinLeft = 3;
const int buzzer = 49;

// Setup start
void setup() {
Serial.begin(9600);
Serial.println("Click this link to control bot or use serial console");
Serial.println("https://8f7b90ec-54c0-49e4-8bcd-92e62f519007-00-177zzsekh1f87.spock.replit.dev/https://8f7b90ec-54c0-49e4-8bcd-92e62f519007-00-177zzsekh1f87.spock.replit.dev/");

pinMode(trigPinRight, OUTPUT); 
pinMode(echoPinRight, INPUT);
pinMode(trigPinLeft, OUTPUT); 
pinMode(echoPinLeft, INPUT);
  // motor setup
	pinMode(enA, OUTPUT);
	pinMode(enB, OUTPUT);
	pinMode(in1, OUTPUT);
	pinMode(in2, OUTPUT);
	pinMode(in3, OUTPUT);
	pinMode(in4, OUTPUT);
  // turn off motors
  	digitalWrite(in1, LOW);
	digitalWrite(in2, LOW);
	digitalWrite(in3, LOW);
	digitalWrite(in4, LOW);
// Set max speed
  	analogWrite(enA, 255);
	analogWrite(enB, 255);
Serial.println("Setup complete");
}


int readDistanceRight() {
digitalWrite(trigPinRight, LOW);
delayMicroseconds(2);
digitalWrite(trigPinRight, HIGH);
delayMicroseconds(10);
digitalWrite(trigPinRight, LOW);
durationRight = pulseIn(echoPinRight, HIGH, 50000);
distanceRight = durationRight * 0.034 / 2;
if (durationRight == 0) {
distanceRight = 500;
}
return distanceRight;
}

int readDistanceLeft() {
digitalWrite(trigPinLeft, LOW);
delayMicroseconds(2);
digitalWrite(trigPinLeft, HIGH);
delayMicroseconds(10);
digitalWrite(trigPinLeft, LOW);
durationLeft = pulseIn(echoPinLeft, HIGH, 50000);
distanceLeft = durationLeft * 0.034 / 2;
if (durationLeft == 0) {
distanceLeft = 500;
}
return distanceLeft;
}



void loop () {
  Servotick ();
}


// Servo & wheel control 
void Servotick () {
while (Serial.available() == 0) {}     //wait for data available
String input = Serial.readString();  //read until timeout
input.trim();  
input.toUpperCase();

if (input == "F") {
	digitalWrite(in1, HIGH);
	digitalWrite(in2, LOW);
	digitalWrite(in3, HIGH);
	digitalWrite(in4, LOW);
}
    if (input == "B") {
	digitalWrite(in1, LOW);
	digitalWrite(in2, HIGH);
	digitalWrite(in3, LOW);
	digitalWrite(in4, HIGH);
};


if (input == "R") {
digitalWrite(in1, LOW);
	digitalWrite(in2, LOW);
	digitalWrite(in3, HIGH);
	digitalWrite(in4, LOW);
};
if (input == "L") {
  	digitalWrite(in1, HIGH);
	digitalWrite(in2, LOW);
	digitalWrite(in3, LOW);
	digitalWrite(in4, LOW);
};
if (input == "S") {
	digitalWrite(in1, LOW);
	digitalWrite(in2, LOW);
	digitalWrite(in3, LOW);
	digitalWrite(in4, LOW);
};
};



  • Always show us a good schematic of your proposed circuit.
    Show us good images of your ‘actual’ wiring.
1 Like

That inefficient dinosaur can handle only 1 Ampere per channel (continuous duty) and wastes about 4V internally in the process. It is totally unsuitable for motors that draw more current than that.

Pololu offers a good selection of modern MOSFET motor drivers.

1 Like

What is the motors' voltage and current ratings?
Which servo did they come from?

5v 800mA MG996r servo motor


5 volt power source

Going by your diagram, you appear to be trying to power the L298N (and the 2 servo motors) from the Mega's 5V pin. I'm sure someone will be along very soon to explain why that's a terrible idea. (And can you even run an L298N on just the 5V pin? I haven't used that dinosaur in ages so I don't remember if that's even possible.)

In the meantime, I'll just give you some questions to think about.

If you're powering the Mega via tha USB cable, how much current is your computer's USB port capable of providing? And how much current is the Mega capable of passing before blowing its polyfuse?

And if you're powering the Mega via the barrel connector, how much current is the Mega's onboard regulator capable of supplying? How many volts are you asking it to step down, and how many watts of power as you asking the linear regulator to dissipate?

  • Your Arduino should never be used to power inductive loads.

I have also tried using a 1000mA 5v battery pack to power the motor driver.
I also tried powering the motor driver with a 9v battery and using the 12v pin. The driver did not even turn on. The motors I am using work when powered by the 9v battery directly.

It has already been told you at post #5 that it won't work. The motor driver would waste at least 2V, giving only 3V or less to the motors.

That could work if:
a) the battery is brand new and the regulator jumper is in place; OR
b) the battery is brand new and regulator jumper is out, but you´re providing 5V to the driver, to feed the logic part (as in your post #8)

Bad idea. If the motors are rated to 6V max they could have burned out.

I suggest 5xAA batteries wired to 12V pin and the jumper on place. That would give your motor voltage around 5.5V (something within the specs of your motor 4.8-6.6V) .

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