Capacitive soil moisture sensor, 4 relay

this program works when I disconnect the relay before uploading the code and then reconnect them. if i then unplug my arduino uno and then plug it back in it doesn't work, the relays turn on and off all the time. not until I disconnect the relays and then reconnect them does it work. What is wrong?

#define RELAY_PIN 3          // Pin controlling the relay
#define RELAY_PIN1 5         // Pin controlling the relay
#define RELAY_PIN2 7         // Pin controlling the relay
#define MOISTURE_PIN A0      // Analog pin connected to the moisture sensor
#define THRESHOLD 350        // Adjust this value based on your soil conditions
#define MOISTURE_PIN1 A1     // Arduino pin that connects to AOUT pin of moisture sensor
#define THRESHOLD1 400       // Adjust this value based on your soil conditions
#define MOISTURE_PIN2 A2     // Arduino pin that connects to AOUT pin of moisture sensor
#define THRESHOLD2 400       // Adjust this value based on your soil conditions
#define MOISTURE_PINtorr A3  // Arduino pin that connects to AOUT pin of moisture sensor
#define THRESHOLD3 530       // Adjust this value based on your soil conditions

void setup() {
  Serial.begin(9600);
  pinMode(MOISTURE_PIN, INPUT);
  pinMode(MOISTURE_PIN1, INPUT);
  pinMode(MOISTURE_PIN2, INPUT);
  pinMode(MOISTURE_PINtorr, INPUT);
  pinMode(RELAY_PIN, OUTPUT);
  pinMode(RELAY_PIN1, OUTPUT);
  pinMode(RELAY_PIN2, OUTPUT);
}

void loop() {
  int moistureLevel = analogRead(MOISTURE_PIN);
  int moistureLevel1 = analogRead(MOISTURE_PIN1);
  int moistureLevel2 = analogRead(MOISTURE_PIN2);
  int moistureLeveltorr = analogRead(MOISTURE_PINtorr);

  if (moistureLevel > THRESHOLD && moistureLeveltorr < THRESHOLD3) {
    Serial.println("Soil moisture is DRY => activating pump");
    digitalWrite(RELAY_PIN, LOW);  // Turn on the pump

    // Decide how long the pump should run (e.g., 5-10 seconds)
    int pumpRunTime = random(15, 19);  // Random time between 5 and 10 seconds
    delay(pumpRunTime * 1000);         // Convert to milliseconds

    digitalWrite(RELAY_PIN, HIGH);  // Turn off the pump
    Serial.println("Pump turned off.");
  }
  if (moistureLevel1 > THRESHOLD1 && moistureLeveltorr < THRESHOLD3) {
    Serial.println("Soil moisture1 is DRY => activating pump");
    digitalWrite(RELAY_PIN1, LOW);  // Turn on the pump

    // Decide how long the pump should run (e.g., 5-10 seconds)
    int pumpRunTime = random(15, 19);  // Random time between 5 and 10 seconds
    delay(pumpRunTime * 1000);         // Convert to milliseconds

    digitalWrite(RELAY_PIN1, HIGH);  // Turn off the pump
    Serial.println("Pump turned off.");
  }
  if (moistureLevel2 > THRESHOLD2 && moistureLeveltorr < THRESHOLD3) {
    Serial.println("Soil moisture2 is DRY => activating pump");
    digitalWrite(RELAY_PIN2, LOW);  // Turn on the pump

    // Decide how long the pump should run (e.g., 5-10 seconds)
    int pumpRunTime = random(15, 19);  // Random time between 5 and 10 seconds
    delay(pumpRunTime * 1000);         // Convert to milliseconds

    digitalWrite(RELAY_PIN2, HIGH);  // Turn off the pump
    Serial.println("Pump turned off.");
  } else {
    Serial.println("Soil moisture is WET => pump deactivated");
    digitalWrite(RELAY_PIN, HIGH);  // Turn off the pump
    Serial.println("Soil moisture1 is WET => pump deactivated");
    digitalWrite(RELAY_PIN1, HIGH);  // Turn off the pump
    Serial.println("Soil moisture2 is WET => pump deactivated");
    digitalWrite(RELAY_PIN2, HIGH);  // Turn off the pump
  }
  Serial.print(" (");
  Serial.print(moistureLevel);
  Serial.println(")");
  delay(1000);

  Serial.print(" (");
  Serial.print(moistureLevel1);
  Serial.println(")");
  delay(1000);

  Serial.print(" (");
  Serial.print(moistureLevel2);
  Serial.println(")");
  delay(1000);

  Serial.print(" (");
  Serial.print(moistureLeveltorr);
  Serial.println(")");

  delay(10000);  // Wait for a short interval before checking moisture again
}

Connect LEDs (with series resistors) instead of the relays. Does the code work as expected then?

If it does, the problem is with your circuit, so please post a schematic.

While you are testing with the LEDs, I will work on improving your code. We don't know yet if the code has a problem, but your code can certainly be improved.

const byte channels = 3;
const byte relayPin[channels] = {3, 5, 7}; // Pins controlling the relays
const byte moisturePin[channels] = {A0, A1, A2}; // Analog pins connected to the moisture sensors
const int threshold[channels] = {350, 400, 400}; // Adjust these values based on your soil conditions
const byte moisturePinTorr = A3; // Analog pins connected to the moisture sensors
const int thresholdTorr = 530; // Adjust these values based on your soil conditions

void setup() {
  Serial.begin(9600);
  for (byte c=0; c<channels; c++) {
    pinMode(moisturePin[c], INPUT);
    pinMode(relayPin[c], OUTPUT);
  }
  pinMode(moisturePinTorr, INPUT);
}

void loop() {
  int moistureLeveltorr = analogRead(moisturePinTorr);
  for (byte c=0; c<channels; c++) {
    int moistureLevel = analogRead(moisturePin[c]);

    if (moistureLevel > threshold[c] && moistureLeveltorr < thresholdTorr) {
      Serial.print("Soil moisture ");
      Serial.print(c);
      Serial.println(" is DRY => activating pump");
      digitalWrite(relayPin[c], LOW);  // Turn on the pump

      // Decide how long the pump should run (e.g., 5-10 seconds)
      int pumpRunTime = random(15, 19);  // Random time between 5 and 10 seconds
      delay(pumpRunTime * 1000);         // Convert to milliseconds

      digitalWrite(relayPin[c], HIGH);  // Turn off the pump
      Serial.println("Pump turned off.");
      }
    else {
      Serial.print("Soil moisture ");
      Serial.print(c);
      Serial.println(" is WET => pump deactivated");
      //digitalWrite(relayPin[c], HIGH);  // Turn off the pump
      }

    Serial.print(" (");
    Serial.print(moistureLevel);
    Serial.println(")");
    delay(1000);
  }

  Serial.print(" (");
  Serial.print(moistureLeveltorr);
  Serial.println(")");

  delay(10000);  // Wait for a short interval before checking moisture again
}

it works with led but not with water pump. the relay switches on and off all the time. I have drawn a schematic but there was not the 4 ch relay that I use nor the soil moisture sensor that I have but I hope it still works.



the second picture is the relay and soil sensor and water pump that I use.

3 pumps and 3 relay coils are too much load on USB supply, you need a separate 5 volt supply of at least 2 Amps. Also, you should have a kickback suppression diode in parallel with each pump, cathode (end with band) toward + (positive) voltage.

ok thanks i will try it

Don't do the pinMode on pins to be read as analog inputs. That's for setting up digital inputs.

Using those capacitive sensors.
I found they tend to vary all over the map on what value means what moisture. Set up your stuff so that you can easily calibrate each one. x1 = dry, x2 = mud. Then you can use the mapping function to get a percentage.

Also, don't calibrate lying on wood, they read the moisture in the wood.

They need to be sealed up so the moisture doesn't get onto their electrics or they go bananas. I removed the silly plug on the ends and soldered wires to them. Then coated the top section with liquid electrical tape as a sealant. Worked really well.

Good luck!

ok thank you

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.