Help needed with controlling DC motors with the L298N

Trying to run two DC motors whose ends are connected to 2 wheels, with the L298N motor driver, but dont have an external power supply, so I connected a 12V AC to DC adapter to the barrel plug of my Arduino Mega 2560 R3, and connected 2 wires from the 5V and GND pins of the arduino, to the 5V and GND of the L298N.

Diagram of the setup and code is attached as below.

What am I doing wrong? I don't really have alot of microcontroller experience and this is the start of a university project.

//motor without speed control test

int motor1pin1 = 3;
int motor1pin2 = 4;

int motor2pin1 = 5;
int motor2pin2 = 6;

void setup() {
  // put your setup code here, to run once:
  pinMode(motor1pin1, OUTPUT);
  pinMode(motor1pin2, OUTPUT);
  pinMode(motor2pin1, OUTPUT);
  pinMode(motor2pin2, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:   
  digitalWrite(motor1pin1, HIGH);
  digitalWrite(motor1pin2, LOW); //rotate in 1 direction -a

  digitalWrite(motor2pin1, HIGH);
  digitalWrite(motor2pin2, LOW); //rotate in 1 direction -b
  delay(1000);  

  digitalWrite(motor1pin1, LOW);
  digitalWrite(motor1pin2, HIGH); //rotate in direction opposite to a

  digitalWrite(motor2pin1, LOW);
  digitalWrite(motor2pin2, HIGH); //rotate in direction opposite to b
  delay(1000);
}

You cannot power two motors from the 5V output pin. You will eventually burn out the MEGA

but dont have an external power supply

You need one

Thank you for your reply.
May I know why using an external power supply to the motor controller, and passing 12V to the mega and connecting the 5V pins on the mega and motor controller together not the same thing?

First I did not say to connect a 12V supply the the motor driver board, that would be way too much voltage for the motors.

It is different because the 5V output of the Arduino cannot supply enough Current to drive the motors. So you need a separate power supply that can supply enough current for the motors.

The driver board will decrease the voltage to the motors by up to 2V. A 6V, 2A supply will be OK but the motors may run a little slower.
Most people use a 2S Li-ion battery which is perfect.

2 Likes

This is a good tutorial

1 Like

See next post. Striking this post.
If you supply power to the L298N from the 12vdc power supply, and you insert the "regulator" jumper, you can supply power to the Arduino from the L298N's 5v output (you are using that pin as an input).

2 Likes

I will agree, and strike my post. I neglected to recognize the motors as 6v motors. I measured my power supply and it shows 8 vdc.

I already deleted my post because I assumed all these motors had max 6V, but it seems some can take 3V to 12V. We do not know what motors @kaulsay has.

Hi, @kaulsay
Welcome to the forum.

Can you please post a copy of your circuit, a picture of a hand drawn circuit in jpg, png?
Hand drawn and photographed is perfectly acceptable.
Please include ALL hardware, power supplies, component names and pin labels.

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

They are DC brush motors with an operating voltage of 3V to 6V.

Do NOT use the Arduino 5V output to power motors. That can damage the Arduino, or cause it to malfunction.

That is the nominal voltage range, and it is OK to exceed that, especially if you use PWM to control the motor speed.

Apply 12V to the driver motor input. Keep in mind that up to about 4V is wasted as heat in the ancient, very inefficient L298 driver, so the full 12V is never applied to the motors.

2 Likes

After dropping 7V (12 - 5), the Arduino's 5 volt regulator can only supply about 175mA without overheating and shutting down or burning out.

Wouldn't it work to put 12V on the module's input, use the module's 7805 to get 5V, then waste another 2V through the L298N, and deliver 3V to the motors at the bottom end of their 3-6V range?

Or use a different power supply:

I think you will find that Vcc = 12V is the supply directly to the L298 for motor supply, so 12 - 2 = 10V to the motors.

Tom.. :smiley: :+1: :coffee: :australia:

In that case here is the post I deleted before:

The motors are not powered from the module's 5V regulator. If they were, that regulator would burn out almost as quickly as the Arduino's regulator would.

The L298 will drop around 2V, exposing those 6V motors to around 10V.

Power the L298 module with 12V and hold the output voltage power down with PWM to the enable pins (ENA & ENB).

Ah. That looks correct. I was mistaken in reading whatever module schematic I had googled up.

It quite clear from your photo how you have it connected the schematic was not necessary.
However as I already pointed out it is wrong.
Did you review the tutorial?

Yes, thank you