Capacitive Moisture Sensor with a Relay

Hello,

I have currently configured an Uno with a capacitive moisture sensor, a single relay module, and breadboard with an LED attached to test the relay.

I have successfully triggered the relay with some code, a bounce program that is fairly simple.

I have successfully pulled serial data off of the moisture sensor with a different piece of code.

The end goal is to be able to program a moisture range from the analog serial data from the moisture sensor and use it to activate the relay, which will power a pump.

I have attached the code I started below. Any suggestions would be greatly appreciated.

// Define pins.
int soil = A0;
int power = 12;
int relay = 7;

// Define timer.
const int TIMER = 5; // in seconds
const int RESET = TIMER - 1;
const int SENSOR_DELAY = 100; // in milliseconds
int counter;

int mval = analogRead(A0);

void setup() {
pinMode(soil, INPUT);
pinMode(power, OUTPUT);
pinMode(relay, OUTPUT);

// Start with sensor OFF
digitalWrite(power, LOW);

// Setup timer.
counter = RESET;

// Setup serial plotter.
Serial.begin(9600);
}

void loop() {
// If the counter is at the end of a cycle
// take a new reading and reset counter.
if (counter <= 0) {
// Turn sensor ON and wait a moment.
digitalWrite(power, HIGH);
delay(SENSOR_DELAY);

digitalRead(mval);
if (mval = 400);
digitalWrite(relay, LOW);

// Take reading, send to plotter.
Serial.println(analogRead(soil));

// Turn sensor OFF again.
digitalWrite(power, LOW);

// Start new countdown.
counter = RESET;
}

// If counter isn't at the end yet, pause for
// the same amount of time as if the sensor
// had been activated to keep things in sync.
else {
delay(SENSOR_DELAY);
}

// Decrement counter and delay until next second.
counter--;
delay(1000 - SENSOR_DELAY);
}

Moisture_Sensor.ino (1.27 KB)

When posting you should put your code in a code block like below. I see a couple of issues in your code, even though it compiles. I commented the issues below:

// Define pins.
int soil = A0;
int power = 12;
int relay = 7;

// Define timer.
const int TIMER = 5; // in seconds
const int RESET = TIMER - 1;
const int SENSOR_DELAY = 100; // in milliseconds
int counter;

int mval = analogRead(A0);

void setup() {
  pinMode(soil, INPUT);
  pinMode(power, OUTPUT);
  pinMode(relay, OUTPUT);

  // Start with sensor OFF
  digitalWrite(power, LOW);

  // Setup timer.
  counter = RESET;

  // Setup serial plotter.
  Serial.begin(9600);
}


void loop() {
  // If the counter is at the end of a cycle
  // take a new reading and reset counter.
  if (counter <= 0) {
    // Turn sensor ON and wait a moment.
    digitalWrite(power, HIGH);
    delay(SENSOR_DELAY);

    digitalRead(mval);               // <--- Does not make sense.  Did you mean 'mval = analogRead(soil)'???
    if (mval = 400);                  // <--- this semicolon should not be here.  Also you should probably check for a range of values.  It is unlikely you will get exactly 400.
    digitalWrite(relay, LOW);     // <--- is relay active LOW?  If so you need to initialize to HIGH in setup() so the pump doesn't start out being on.

    // Take reading, send to plotter.
    Serial.println(analogRead(soil)); // <--- use mval here.  Reading an analog too fast after the last read can cause an unstable reading.

    // Turn sensor OFF again.
    digitalWrite(power, LOW);

    // Start new countdown.
    counter = RESET;
  }

  // If counter isn't at the end yet, pause for
  // the same amount of time as if the sensor
  // had been activated to keep things in sync.
  else {
    delay(SENSOR_DELAY);
  }

  // Decrement counter and delay until next second.
  counter--;
  delay(1000 - SENSOR_DELAY);
}