External power ruins my project!

I am building a automatic hand sanitizer dispensor with an arduino nano. Basically, there is this motor which pushes down on the dispenser and the dispenser dispenses the sanitizer. The motor is controlled by the ultrasonic sensor. I added an LCD just to show off.
Part List:

  1. Arduino Nano
  2. LCD
  3. L298N Motor Driver module to control the motor
  4. Motor
  5. Ultrasonic Sensor

Now to the main part.

I completed coding and building the project and was at the last stage, connecting it to external power source. I connected the +ve pin of a 9V battery to the +12V pin of the driver module and -ve pin of the 9V battery to the GND pin of the driver module (Which was connected to the GND pin of the Arduino). I had connected a jumper wire from the +12V pin to the Vin pin of the arduino. Now the results were a bit strange and varied. I have attached the videos of the outputs.
The strange part was that the project worked perfectly fine when connected to a computer with a USB port. Just adding external power ruined it all! I have attached the expected output (When it was connected with a USB port).

The Connections:

  • L298N Driver Input 1 - Arduino D9
  • L298N Driver Input 2 - Arduino D10
  • L298N Driver +12V - Arduino Vin and +ve terminal of 9V Battery
  • L298N Driver +5V - Arduino 5V
  • L298N Driver GND - Arduino GND
  • Ultrasonic Sensor VCC - Arduino 5V
  • Ultrasonic Sensor GND - Arduino GND
  • Ultrasonic Sensor Trig Pin - Arduino D11
  • Ultrasonic Sensor Echo Pin - Arduino D12
  • LCD I2C SDA Pin - Arduino A4
  • LCD I2C SCL Pin - Arduino A5
  • LCD I2C VCC Pin - Arduino 5V
  • LCD I2C GND Pin - Arduino GND

Here’s the code:

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <LCD.h>

#define I2C_ADDR          0x27        //Define I2C Address where the PCF8574A is
#define BACKLIGHT_PIN      3
#define En_pin             2
#define Rw_pin             1
#define Rs_pin             0
#define D4_pin             4
#define D5_pin             5
#define D6_pin             6
#define D7_pin             7

LiquidCrystal_I2C      lcd(I2C_ADDR, En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);
const int echoPin = 12;
const int trigPin = 11;
const int motorPin1 = 9;
const int motorPin2 = 10;

long duration;
int distance;

void setup() {
    lcd.begin (16,2);
    lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
    lcd.setBacklight(HIGH);
    
    pinMode(motorPin1, OUTPUT);
    pinMode(motorPin2, OUTPUT);
    pinMode(trigPin, OUTPUT);
    pinMode(echoPin, INPUT);
    Serial.begin(9600);
    lcd.setCursor(1,0);
    lcd.print("Stay Calm and");
    lcd.setCursor(4,1);
    lcd.print("Sanitize");
}

void loop() {
    digitalWrite(trigPin, LOW);
    delayMicroseconds(2);
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin, LOW);
    duration = pulseIn(echoPin, HIGH);
    distance= duration*0.034/2;
    
    if (distance < 5.5) {
      Serial.println("Sanitizing...");
      digitalWrite(motorPin1, HIGH);
      digitalWrite(motorPin2, LOW);
      delay(1500);
      
      digitalWrite(motorPin1, LOW);
      digitalWrite(motorPin2, HIGH);
      delay(2000);
      
      digitalWrite(motorPin1, LOW);
      digitalWrite(motorPin2, LOW);
    }
}

I am very confused as to what is happening. What is the problem here?
Please help.

Thanks.
(Here are all of the attachments because the forum only allows 2mb of files)

Inadequate power from an inadequate battery. If you insist on using a 9v PP3, you deserve all the grief you will surely get. The fact that it cannot even power the LCD's backlight should be all the evidence you need. I don't suppose there is any point in asking why you connect a 9v battery to a 12v motor. In the unlikely event that the motor manufacturer says 9v is kosher, you can be sure they didn't mean a PP3. Put it back in the smoke detector you stole it from, and think seriously about the power supply. Surely there are some 5v motors that would suffice?

Nick_Pyner:
Inadequate power from an inadequate battery.

Ok... So the problem is with the power supply. I have changed the power supply to a 16V Battery Bank and I think it should be more than enough. Now it is working just fine. But I am still curious as to why the things were acting so strange when the power supply was low. And why were they working fine when connected using a cable.

Nick_Pyner:
Put it back in the smoke detector you stole it from

I have not stolen it from a smoke detector.

TheRoboticM:
But I am still curious as to why the things were acting so strange when the power supply was low. And why were they working fine when connected using a cable.

I thought that would be pretty clear by now. Power is (ahem!) what makes things go. It was acting strange when the power was low because the power was low, and it was fine with the cable because the power was not low.

I don't suppose there is any point in asking how you powered the motors when you ran this gear off the USB port but, if you are now using 16v to get the job done, it might pay to learn a little about watts, volts, and Amps

So after further research I got to know that 9V battery have a capacity of 500mAH while AA batteries (which are in my battery bank) have a capacity of 2400mAH. I guess the 9V battery could not compensate for the power usage of the arduino and other stuff. I also got to know that 6 1.5V AA cells connected in series (9V with 2400mAH) works with my project just fine. So I guess now I'm gonna use 9V instead of 16V.

Thanks!

TheRoboticM:
further research I got to know that 9V battery have a capacity of 500mAH

That is only part of the research. The bit you missed was the discharge rate. I believe about the best you can expect out of a PP3 is about 50 mA, about half of which is wasted as heat in the regulators, while USB delivered the voltage you really want, with about ten times the current.

While a hell of a lot better than a PP3, 6xAA is also wasteful and, if you eventually find them not up to scratch, you might look at 2x18650 4000 mA. You can even run Arduino with a single 18650 using a phone booster which delivers 5v into the USB socket.

I don't know how bad Nano's regulators actually are but, if you are using batteries, using a decent regulator is usually the first step.