I'm having driving a 12dc motor with the l298n motor driver with atmega 2560

Hi Everyone

im having problems driving a 12v motor with the l298n, when 12v supply is supplied to the l298n and the arduino is powered up the motor spins for just a second not even 2 seconds then stops. when i switch the supply off the motor spins the opposite direction for just a milli second then of. the motor voltage jumps from 0v to 4,12v then back to 0v. the code provided below is for arduino uno to test and practice using l298n but still the same thing.

int enA = 9;
int in1 = 8;
int in2 = 7;

void setup() {
// put your setup code here, to run once:
pinMode(enA,OUTPUT);
pinMode(in1,OUTPUT);
pinMode(in2,OUTPUT);

digitalWrite(in1,LOW);
digitalWrite(in2,LOW);

}

void loop() {

DirectionControl();
delay(1000);
speedControl();
delay(1000);

}

void DirectionControl()
{
analogWrite(enA,150);

digitalWrite(in1,HIGH);
digitalWrite(in2,LOW);
delay(2000);

digitalWrite(in1,LOW);
digitalWrite(in2,HIGH);
delay(2000);

digitalWrite(in1,LOW);
digitalWrite(in2,LOW);
delay(2000);
}

void speedControl()
{
digitalWrite(in1,LOW);
digitalWrite(in2,HIGH);

for(int i = 0; i < 256;i++)
{
analogWrite(enA,i);
delay(20);
}
for(int i = 255; i >= 0;i--)
{
analogWrite(enA,i);
delay(20);
}

digitalWrite(in1,LOW);
digitalWrite(in2,LOW);
}


this is the picture of the circuit

This is a question asked many times before. If I remember, add pull downs to the controller outputs.
During startup the outputs are set in tristate until the boat routine us finished.

1 Like

Did you look at this?"
https://projecthub.arduino.cc/lakshyajhalani56/l298n-motor-driver-arduino-motors-motor-driver-l298n-7e1b3b

1 Like

thank you i will try it

i will take a look at it thank you for the suggestion

1 Like

No problem, bud

const int enaPin = 4; // PWM pin for motor speed control
const int in1Pin = 24; // Motor control pin 1
const int in2Pin = 26; // Motor control pin 2

void setup() {
// Set motor control pins as outputs with pull-down resistors
pinMode(enaPin, OUTPUT);
pinMode(in1Pin, OUTPUT);
pinMode(in2Pin, OUTPUT);
digitalWrite(enaPin, LOW);
digitalWrite(in1Pin, LOW);
digitalWrite(in2Pin, LOW);

// Add pull-down resistors to ensure a defined state during startup
digitalWrite(enaPin, LOW);
digitalWrite(in1Pin, LOW);
digitalWrite(in2Pin, LOW);

}

void loop() {

enableMotorControl();
}

void enableMotorControl() {

analogWrite(enaPin, 100); // duty cycle PWM
digitalWrite(in1Pin, HIGH);
digitalWrite(in2Pin, LOW);
}

I have tried to set the tristate but Im still getting the same results, i have used the above code.

I did try it as suggest on the link and the youtube video but still getting the same results. when I do troubleshooting on the circuit, the voltage regulator for l298n motor driver gives the same voltage as the supply voltage which is 11.64v supply is 12v. The +5V pin of l298n also gives 11,68v.

Add 10k external physical pull down resistors, not a second write LOW!

To avoid glitches, do the write LOW first and then the define the outputs.

thank you, I'm on to it, the pull down resistor should be connected to the input pin1 off the l298n to ground or output pins? can you draw a diagram illustrating this.

What is your motor's current or wattage rating?

What is your 12 volt supply's current rating?

If You have reasonably short wires it doesn't matter. Basic theory: It's the output pin behaviour You want to correct.

HKD 795 MOTOR 12-24V HI SPD/TORQ
Voltage: DC12V -24V, No Load AMPS: 2.05A, NO Load Speed: 12V(10 000RPM) - 24V(20 000RPM). D-Type 5mm Shaft.
That is the motor im trying to drive

i just measured it is 4.25Amp. At which may be the issue.

okay thank you.

WHOA! That's not right, check that again. 4.25Amps is about twice what the L298 is rated for, you COULD parallel the A and B outputs and get twice the current but it's a little tricky, you MUST be certain the polarity is right or you will kill the L298.

I will try a different power supply, how would I tell if the L298n is damaged?

On the front side of the L298 module, there is a 3 screw terminal block for power connection. The one on the far left is for motor supply voltage (12V in your case), the middle one is GND for the motor power supply and Arduino, the right end is the 5V terminal for the module's logic circuits. Put your black meter probe on the center terminal and red probe on the right terminal. You should have about 5V there, NOT 11 something.

L293