Problems with L298N

Hi I am new to building robots I am making a bluetooth controlled robot using l298n and HC-05. It failed to work and I narrowed down the issue to the L298N.
I used this video to check if my motor driver was working: https://www.youtube.com/watch?v=E2sTbpFsvXI
This is the code:

int motor1pin1 = 2;
int motor1pin2 = 3;

int motor2pin1 = 4;
int motor2pin2 = 5;

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);

  digitalWrite(motor2pin1, HIGH);
  digitalWrite(motor2pin2, LOW);
  delay(1000);

  digitalWrite(motor1pin1, LOW);
  digitalWrite(motor1pin2, HIGH);

  digitalWrite(motor2pin1, LOW);
  digitalWrite(motor2pin2, HIGH);
  delay(1000);
}

Only one motor was spinning and that too only for a few seconds before it stopped working.The motor only works when i plug in both the battery and then the arduino to the laptop. Im using a 9v battery. Please advise on what to do next.

Hi! Welcome to the Forum.

Congratulations for using code format in your very first post.

This use to be the first issue. 9V batteries are not a good choice because they can't provide enough current and get drained very fast. Change it for a 6x AA battery pack or a 2x 18650.

Posting the schematics of your project also helps.

1 Like

Thanks a lot for the help, but is it only a power issue or is something else wrong?

I can't see "something else wrong" without the schematics. It seems to me that it is only a power issue.
Your code from post #1 is a simple test for turning the motors back and forth for 1 sec and I don't see anything wrong with it.

1 Like

Thanks a lot, the power supply seemed to be the issue. You just saved me I have to turn in this project tmrw. Keep up the good work.

I'm happy to help! :wink:
Good luck tomorrow!

Can you answer this as well?