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.



