const int AirValue = 564; // Analog Value when Sensor is exposed to dry air
const int WaterValue = 298; // Analog Value when Sensor is kept in water
const int Pin1 = A0; // Analog pin where the sensor is connected
const int Pin2 = A1;
// Variables to hold sensor values
int soilMoistureValue = 0;
int soilmoisturepercent = 0;
int IN1 = 2; // Pin where the relay is connected
int IN2 = 3;
void setup()
{
Serial.begin(9600);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(Pin1, INPUT);
pinMode(Pin2, INPUT);
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
delay(500);
}
void loop()
{
Serial.print("plant 1_");
soilMoistureValue = analogRead(Pin1); // Read the value from the soil moisture sensor
Serial.println(soilMoistureValue); // Print the sensor value to the serial monitor
soilmoisturepercent = map(soilMoistureValue, AirValue, WaterValue, 0, 100); // Map the sensor value to a soil moisture percentage
soilmoisturepercent = constrain(soilmoisturepercent, 0, 100); // Ensure soilmoisturepercent is between 0 and 100
Serial.println(soilmoisturepercent);
if (soilmoisturepercent < 10){ // Control the relay based on the soil moisture
digitalWrite(IN1, HIGH);
delay(10000);
digitalWrite(IN1, LOW);
}
else{
digitalWrite(IN1, LOW);
}
;delay(10000);
Serial.print("plant 2_");
soilMoistureValue = analogRead(Pin2); // Read the value from the soil moisture sensor
Serial.println(soilMoistureValue); // Print the sensor value to the serial monitor
soilmoisturepercent = map(soilMoistureValue, AirValue, WaterValue, 0, 100); // Map the sensor value to a soil moisture percentage
soilmoisturepercent = constrain(soilmoisturepercent, 0, 100); // Ensure soilmoisturepercent is between 0 and 100
Serial.println(soilmoisturepercent);
if (soilmoisturepercent < 10){ // Control the relay based on the soil moisture
digitalWrite(IN2, HIGH);
}
else{
digitalWrite(IN2, LOW);
}
delay(10000);
}