Board Malfunctions if connected to External Power but works fine with USB Power

The question is for the arduino Nano for which the code works fine when connected to the Laptop but works weird as soon as external power is used.

The code is being used to switch off a water pump as soon as water is full in the tank along with a 30min timer in case required to switch off early.
Parts being used apart from Nano: 1 Relay , 2 LED and a switch. All are able to work with the laptop supply which means the power consumption is < 500mA.

As soon as the circuit is run on External power (9V Vin 1.5A Linear power supply) it malfunctions. Since only 1 power supply is being used I have used the board’s 5V supply for relay, LEDs and GND to prevent any common GND problems.

I tried using an adapter 12V DC but result is the same.
I tried connecting the GND of the board to the GND of Home supply but results never differ.
Also same code used in Arduino UNO and a breadboard gives same response on both Laptop and external power(malfunction).
I tried powering the board from the USB using a mobile charger but again malfunction.
Please help me if this is something related to code I have missed.
If the code was wrong it should not have worked with Laptop as well in both Nano as well as UNO.

unsigned long startMillis;
const byte D7=7;
const byte D8=8;
const byte D9=9;
const byte D5=5;
int n=0;

void setup() {
// put your setup code here, to run once:

pinMode(D7,OUTPUT); // Output at Pin D7 for the relay
pinMode(D8,OUTPUT);// OUTPUT at pin D8 for LED indication
pinMode(D9,OUTPUT); // Output at Pin D9 for LED 30 min timer

digitalWrite(D7,HIGH);
digitalWrite(D9,LOW);

}

void loop() {
// put your main code here, to run repeatedly:
digitalWrite(D7,HIGH);

int sensorValue = analogRead(A1);// Input A1 input analog pin
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
float voltage = sensorValue * (5.0 / 1023.0);
// Check the value of voltage if tank full
if (voltage>2)
{
digitalWrite(D7,LOW);
delay(30000);
}

startMillis = millis();

if(startMillis<20000)
{
while(startMillis<8000)
{
int val = analogRead(A3);
float vol = val * (5.0 / 1023.0);
if(vol > 4)
{
digitalWrite(D9,HIGH);
int n=1;
}
startMillis = millis();

}// End of While body

digitalWrite(D8,HIGH);
delay(200);
digitalWrite(D8,LOW);

}// ending ifstartmillis body
digitalWrite(D7,HIGH);

if(startMillis>1800000)
{
digitalWrite(D8,HIGH);
if(n==1)
{
digitalWrite(D7,LOW);
delay(3000);
}// end of IF
}//end of if millis 1800000
digitalWrite(D7,HIGH);
if(startMillis>2400000)
{
digitalWrite(D8,LOW);
delay(10);
digitalWrite(D8,HIGH);
delay(10);
digitalWrite(D8,LOW);
}// end of IF start millis 2400000

digitalWrite(D7,HIGH);
}// closure of the void loop