// Define the pin connections
const int sensorPin = A0; // Pin untuk sensor kelembaban tanah
const int pumpPin = 7; // Pin untuk relay yang mengontrol pompa air
// Define the threshold for soil moisture
const int threshold = 500; // Nilai ambang batas kelembaban tanah
void setup() {
// Initialize serial communication
Serial.begin(9600);
// Initialize the pump pin as an output
pinMode(pumpPin, OUTPUT);
// Ensure the pump is off initially
digitalWrite(pumpPin, LOW);
}
void loop() {
// Read the value from the soil moisture sensor
int sensorValue = analogRead(sensorPin);
// Print the sensor value to the serial monitor
Serial.print("Soil Moisture Value: ");
Serial.println(sensorValue);
// Check if the soil moisture is below the threshold
if (sensorValue < threshold) {
// If the soil is dry, turn on the pump
Serial.println("Soil is dry. Turning on the pump.");
digitalWrite(pumpPin, HIGH);
} else {
// If the soil is wet enough, turn off the pump
Serial.println("Soil is wet enough. Turning off the pump.");
digitalWrite(pumpPin, LOW);
}
// Wait for a short period before taking another reading
delay(2000); // Delay for 2 seconds
}