#include <Wire.h>
#include <DHT.h>
#include <LiquidCrystal.h>
// Pin Definitions
#define DHT_PIN 21 // GPIO21 for DHT22 sensor data pin
#define RELAY_PIN 5 // GPIO5 for relay control
#define SOIL_MOISTURE_PIN1 34 // GPIO34 for Joystick (X-axis)
#define SOIL_MOISTURE_PIN2 35 // GPIO35 for Potentiometer (SIG)
// DHT Sensor Configuration
#define DHT_TYPE DHT22
DHT dht(DHT_PIN, DHT_TYPE);
// LCD Display
LiquidCrystal lcd(17, 16, 5, 18, 19, 23); // RS, E, D4, D5, D6, D7 pins
// Moisture threshold
int moistureThreshold = 40; // Adjust this threshold as needed
void setup() {
// Start Serial Monitor
Serial.begin(115200);
// Initialize DHT sensor
dht.begin();
delay(1000); // Allow DHT sensor to stabilize
// Initialize LCD
lcd.begin(16, 2); // Initialize the 16x2 LCD
lcd.print("Smart Plant Care System");
delay(2000); // Hold the message for 2 seconds
// Initialize Relay Pin
pinMode(RELAY_PIN, OUTPUT);
digitalWrite(RELAY_PIN, LOW); // Initially off
}
void loop() {
// Read soil moisture from Joystick (X-axis) and Potentiometer (SIG)
int joystickMoisture = analogRead(SOIL_MOISTURE_PIN1); // Joystick
int potMoisture = analogRead(SOIL_MOISTURE_PIN2); // Potentiometer
// Map the readings to a percentage (0-100%)
int soilMoisture = map(joystickMoisture, 0, 4095, 0, 100);
int potMoistureVal = map(potMoisture, 0, 4095, 0, 100);
// Read DHT22 data (Temperature and Humidity)
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
// Check if any read failed
if (isnan(temperature) || isnan(humidity)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
// Log data to the Serial Monitor
Serial.print(F("Temperature: "));
Serial.print(temperature);
Serial.println(F("°C"));
Serial.print(F("Humidity: "));
Serial.print(humidity);
Serial.println(F("%"));
Serial.print(F("Soil Moisture (Joystick): "));
Serial.print(soilMoisture);
Serial.println(F("%"));
Serial.print(F("Soil Moisture (Potentiometer): "));
Serial.print(potMoistureVal);
Serial.println(F("%"));
// Control Relay based on soil moisture level
if (soilMoisture < moistureThreshold || potMoistureVal < moistureThreshold) {
digitalWrite(RELAY_PIN, HIGH); // Turn on water pump
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Watering Plants...");
delay(1000);
} else {
digitalWrite(RELAY_PIN, LOW);
// Turn off water pump
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Soil Moisture OK");
delay(1000);
}
// Display Temperature, Humidity, and Soil Moisture on LCD
lcd.clear();
lcd.setCursor(0, 1);
lcd.print("Temp: " + String(temperature) + "C");
lcd.setCursor(0, 0);
lcd.print("Soil Moist: " + String(soilMoisture) + "%");
delay(1000); // Wait for 2 seconds before the next update
}