#include <Arduino.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Define the soil moisture sensor pin
const int soilMoistureSensorPin = 15 ;
const int lcdAddr = 0x27; // I2C address of the LCD X
const int relayPin = 13; // Replace with your relay pin X
// Create an LCD object
LiquidCrystal_I2C lcd(lcdAddr, 20, 4); // X
void setup() {
Serial.begin(115200); // Initialize serial communication
pinMode(soilMoistureSensorPin, INPUT);
pinMode(relayPin, OUTPUT); // X
// Initialize the LCD
lcd.init(); // X
lcd.backlight(); // X
}
void loop() {
// Read the soil moisture sensor value
int sensorValue = analogRead(soilMoistureSensorPin);
// Calculate the soil moisture percentage (adjust the formula as needed)
float soilMoisturePercent = map(sensorValue, 0, 4095, 100, 0);
// Display the moisture percentage on the LCD X
lcd.setCursor(0, 0); //X
lcd.print("Lemah Teles:"); //X
lcd.setCursor(13, 0); //X
lcd.print(soilMoisturePercent); //X
lcd.print("%"); //X
// Print the moisture percentage to the serial monitor X
Serial.print("Soil Moisture: "); //X
Serial.print(soilMoisturePercent); //X
Serial.println("%"); //X
// Check if soil moisture is below a threshold X
if (soilMoisturePercent < 30) { // X
digitalWrite(relayPin, HIGH); // Turn on the relay and water pump X
lcd.setCursor(0, 1); // X
lcd.print("garing"); // X
} else { // X
digitalWrite(relayPin, LOW); // Turn off the relay and water pump X
lcd.setCursor(0, 1); //X
lcd.print("basahlek"); //X
}
// You can also log the data to an SD card or send it to a cloud platform here
// Example:
// if (SD_available()) {
// // Log data to SD card
// } else if (WiFi.isConnected()) {
// // Send data to cloud platform (e.g., ThingSpeak, Adafruit IO)
// }
delay(1000); // Delay for 1 seconds
}