Issue controlling motors with a power relay

I am using a 5v power relay connected to an arduino and a 9v battery in order to power a 5v submersible water pump. My general plan with the design is that when the soil moisture sensor detects a water content of lower than 30%, the pump will activate. However, I am having issues getting it functional, as the relay seems to be unresponsive to high/low power changes, remaining on at all times no matter what in my main code. I ported the main segments to a unit testing version and it works perfectly fine with the exact same components, but upon testing in the main code, the relay is unresponsive. In both segments, I only had the relay and soil moisture sensor attached to the arduino to avoid voltage issues.

I also provided multiple serial print statements that show my logic works perfectly fine for the arduino and it does recognise adjustments in the soil moisture content. However, it still is unable to change the power output to the relay. I tested this additionally with a multimeter and the data pin's voltage did not fluctuate from ~3v at any point, whether it was supposed to be outputting HIGH or LOW data. I assumed it was an error with another pin interfering with the outputs since even removing the "digitalWrite(motorpin, HIGH);" statement would not turn off the relay in the main code, but I cannot find a single instance of extra outputs. Main code is provided below, and the unit test version is just the motor control section taken out of the main code (copy and pasted to avoid accidental rewrite changes)

#include <DHT.h>
#include <RBD_LightSensor.h>
#include <Adafruit_NeoPixel.h>
#include <Arduino.h>

#define DHTPIN 13      // Pin where the DHT sensor is connected
#define DHTTYPE DHT11  // Change to DHT22 if you are using DHT22
#define VIN 5
#define R 10000
#define VIN 5
#define R 10000

DHT dht(DHTPIN, DHTTYPE);  // Initialize DHT sensor

int ledPin = 9;
int NUMPIXELS = 24;
int fanpin1 = 10;
RBD::LightSensor Photopin(A5);
const int motorpin = 0;
bool photoDisable = false;
bool fanDisable = false;
bool motorDisable = false;
const int AirValue = 790;
const int WaterValue = 420;
int soilMoistureValue = 0;
int soilMoisturePercent = 0;
int soilpin = A0;

Adafruit_NeoPixel pixels(24, ledPin, NEO_GRB + NEO_KHZ800);

void setup() {
  Serial.begin(4800);  // Start the Serial communication
  {
    pixels.begin();
  }

  dht.begin();  // Initialize the DHT sensor
  pinMode(ledPin, OUTPUT);
  pinMode(fanpin1, OUTPUT);
  pinMode(soilpin, INPUT);
  mainprints();
}

void clearscreen() {
  Serial.println("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
}

void mainprints() {
  Serial.println("1. Sensor Data");
  Serial.println("2. System Status");
  Serial.println("3. Toggle Light Functionality");
  Serial.println("4. Toggle Pump Functionality");
  Serial.println("5. Toggle Fans Functionality");
  Serial.println("  ");
}


void loop() {

  // Wait a few seconds between measurements
  delay(250);
  float temperature = dht.readTemperature();
  // Read humidity
  float humidity = dht.readHumidity();
  //Read Photoresistor. Outputs in % of voltage from 1-1023. Conversion to Lux is (Voltage/5)*10^(-3)
  int Photovalue = Photopin.getValue();
  //Read the Capacitive Soil Moisture Sensor. Convert to percentage using the air and water values that were precalibrated by me
  soilMoistureValue = analogRead(soilpin);  //put Sensor insert into soil
  soilMoisturePercent = map(soilMoistureValue, AirValue, WaterValue, 0, 100);
  Serial.println(soilMoistureValue);
  Serial.println(soilMoisturePercent);


  // Output the results to the Serial Monitor

  while (Serial.available() > 0) {
    clearscreen();
    static char message[12];
    static unsigned int message_pos = 0;

    char inByte = Serial.read();
    if (inByte != '\n' && (message_pos < 12 - 1)) {
      message[message_pos] = inByte;
      message_pos++;

    } else {
      message[message_pos] = "\0";

      int number = atoi(message);

      if (number == 1) {
        mainprints();
        Serial.print("Temperature: ");
        Serial.print(temperature);
        Serial.println(" °C");
        Serial.println("  ");

        Serial.print("Humidity: ");
        Serial.print(humidity);
        Serial.println(" %");
        Serial.println("  ");

        Serial.print("Analog  Value: ");
        Serial.println(Photovalue);

        Serial.println("  ");
        Serial.print("Soil Moisture Content: ");
        Serial.print(soilMoisturePercent);
        Serial.println(" %");
      } else if (number == 2) {
        mainprints();
        if (!photoDisable) {
          if (Photovalue > 450) {
            Serial.println("Lights Enabled and Active");
          } else {
            Serial.println("Lights Enabled and Inactive");
          }
        } else {
          Serial.println("Lights Disabled");
        }

        if (!motorDisable) {
          if (soilMoisturePercent <= 30) {
            Serial.println("Pump Enabled and Active");
          } else {
            Serial.println("Pump Enabled and Inactive");
          }
        } else {
          Serial.println("Pump Disabled");
        }

        if (!fanDisable) {
          if (temperature >= 30) {
            Serial.println("Fans Enabled and Active");
          } else {
            Serial.println("Fans Enabled and Inactive");
          }
        } else {
          Serial.println("Fans Disabled");
        }

        if (humidity < 50) {
          Serial.println("  ");
          Serial.println("Interior Humidity is below recommended level of 50-90%");
          Serial.println("Please place this device in a more humid environment if the humidity drops below 30%!");
        } else {
          Serial.println("Humidity is within normal levels");
        }
      } else if (number == 3) {
        mainprints();
        if (photoDisable == false) {
          photoDisable = true;
          Serial.println("Light functions Disabled");
        } else {
          photoDisable = false;
          Serial.println("Light functions Enabled");
        }

      } else if (number == 4) {
        mainprints();
        if (motorDisable == false) {
          motorDisable = true;
          Serial.println("Pump functions Disabled");
        } else {
          motorDisable = false;
          Serial.println("Pump functions Enabled");
        }

      } else if (number == 5) {
        mainprints();
        if (fanDisable == false) {
          fanDisable = true;
          Serial.println("Fan functions Disabled");
        } else {
          fanDisable = false;
          Serial.println("Fan functions Enabled");
        }

      } else {
        mainprints();
        Serial.println("Error - Please enter a valid number");
      }

      message_pos = 0;
    }
  }
  /*
  Serial.print("Temperature: ");
  Serial.print(temperature);
  Serial.println(" °C");
  //----------------------//
  Serial.print("Humidity: ");
  Serial.print(humidity);
  Serial.println(" %");
  //---------------------//
  Serial.print("Analog  Value: ");
  Serial.println(Photovalue);
  Serial.println("----------------------------------------");
  */

  // Control the LED PIN with resistance values from the photoresistor
  if (photoDisable == false) {
    if (Photovalue > 450) {
      int i;
      for (int i = 0; i < 23; i++) {  // For each pixel in strip...
        pixels.setPixelColor(i, pixels.Color(255, 255, 255));
      }
      pixels.show();  // Update strip with new contents
      delay(10);      // Pause for a moment
    } else {
      int v;
      for (int v = 0; v < 23; v++) {
        //Set values to 0 when resistor value below 450
        pixels.setPixelColor(v, pixels.Color(0, 0, 0));
      }
      pixels.show();
      delay(10);
    }
  } else {
    int v;
    for (int v = 0; v < 23; v++) {
      //Set values to 0 when resistor value below 450
      pixels.setPixelColor(v, pixels.Color(0, 0, 0));
    }
    pixels.show();
    delay(10);
  }


  //Control the water pump using soil moisture percentages from the capacitive soil moisture sensor;

  if (motorDisable == false) {
    if (soilMoisturePercent <= 30) {
      digitalWrite(motorpin, HIGH);
    } else {
      digitalWrite(motorpin, LOW);
      Serial.println("MOTOR OFF");
    }
  } else {
    digitalWrite(motorpin, LOW);
  }

  //Control the Fan relay using temperature values from the main DHT sensor
  if (fanDisable == false) {
    if (temperature >= 30) {
      digitalWrite(fanpin1, HIGH);
    } else {
      digitalWrite(fanpin1, LOW);
    }
  } else {
    digitalWrite(fanpin1, LOW);
  }
}

Any help would be appreciated.

which type of 9V battery ? if this is something like this
image
it 's not suitable for motors or even powering your arduino on the long run. These are good for smoke detectors but not anything requiring stronger current.

That is exactly the 9v battery I'm using haha. Do you have any reccomendations for what I could use over a longer period of time? And would this affect the control of my relay? I have a secondary relay system running off one that seems to be working fine which was why the issues with my first relay confused me so much (they're running at the same time with the same current and voltage but only one is working despite both working in seperate unit tests)

you need to calculate what's the current draw on average (say for a day and when the pump needs to work) for your system and then based on the autonomy you want (how often do you need to recharge / replace the battery) you'll have a number that you can use to find an appropriate power system.

Alright, I'll take a look into that for extended runtimes.

As for my current issue, I should also mention that the indicator LED is functional on the relay, and it turns on and off from all control options in my code (manual disable through serial, putting the water sensor in water), but it is significantly dimmed compared to the operational fan control relay and it doesn't "click" on, despite being provided with more than adequate voltage and resistance

Could you post a hand drawn schema of your system ?

It's not an amazing image by any means but it's a basic description. For reference I colour coded positive wires in red, negative in blue, and data in black. The fan segment works perfectly fine and responds to all controls, as do the leds. All systems are receiving appropriate power for their required functions.

I see

which Arduino are you using (pin 0 and 1 are for Serial on a UNO or MEGA for example)

Powering a 24 neopixels led strip through the Arduino 5V is an issue

On the really side, what you need to take into account is the current drawn by the relay if you actively drive the coil from the Arduino. Some draw between 70 and 120mA.

adding all the current draws together might just be over the top and the voltage of your Arduino 9V battery drops, possibly to a point where the Ardiuno would reboot or be under-powered and not work well.

Ah. I forgot to adjust the code after making more changes. The whole thing took a while to upload so I changed some parts around in the meantime. I'm no longer using a 24 neopixel strip and its now a chain of 6 leds and a 220-ohm resistor, and the motorpin I changed to pin11 (marked as PWD on my dfrobot gravity arduino shield). The relays I believe each draw around 110mA which I believe is still in the arduino UNO's output range for amps even when adding the LEDS.

the question is more "can the 9V battery deliver > 250mA in a sustained way...."

if you look at Duracell's spec , they don't give you anything beyond a 250mA draw

You probably need 7V in at minimum to build the 5V, look even for 250mA draw what it means in terms of lifetime of the battery

if you are above those 250mA, it will be even faster.

Error remains unknown and I still am yet to find the reason as to why it was inoperable on the digital pin.

However, I found that rewiring and recoding the pin from digital to instead analog would allow the relay to function.