/*
How it works :
Click on the play button to start the simulation
1. The switch on the left is the float switch, click on it to change between high and low position,
when it's in the low position that means water tank is full and when high, tank is empty
2. Click on any of the sensors (DHT22 for Temperature / pH sensor / Soil sensor) you should see a
slider appears on the top, you can change the slider position to interact with the sensor values
The resulting sensor values will be shown on the Display
3. The pump will be connected to the relay. When the relay is on, the pump activates when the relay
off, the pump turns off as well
4. The red led is used to alart signal a drought
*/
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,20,4);
#include <DHT.h>
#define DHTPIN 9
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
#define Serial Serial1
// display pinout
#define pinSDA 20
#define pinSCL 21
// sensor pinout
#define soil_pin 27
#define ph_pin 26
#define float_pin 14
// Outputs pinout
#define pump_pin 18
#define led_pin 13
// variables
float tempHigh = 30.0;
float drought = 50.0;
int moistureLow = 40;
float phLow = 6.5;
float phHigh = 8.4;
bool hasWater = false;
void setup() {
Serial.begin(115200);
analogReadResolution(12);
Wire.setSDA(pinSDA);
Wire.setSCL(pinSCL);
Wire.begin();
lcd.init();
lcd.backlight();
pinMode(soil_pin, INPUT);
pinMode(ph_pin, INPUT);
pinMode(float_pin, INPUT_PULLUP);
pinMode(pump_pin, OUTPUT);
pinMode(led_pin, OUTPUT);
dht.begin();
lcd.setCursor(6, 0);
lcd.print("Automatic");
delay(500);
lcd.setCursor(5, 1);
lcd.print("Irrigation");
delay(500);
lcd.setCursor(7, 2);
lcd.print("System");
delay(2000);
lcd.clear();
}
void loop() {
delay(1000);
float temperature = dht.readTemperature();
//Serial.print("Temperature : ");
//Serial.print(temperature);
//Serial.println("°C");
int moisture = map(analogRead(soil_pin),1680,3620,0,100);
//Serial.print("Moisture : ");
//Serial.print(moisture);
//Serial.println("%");
float phVal = ((float) analogRead(ph_pin) / 4095.0) * 14;
//Serial.print("PH : ");
//Serial.println(phVal);
lcd.setCursor(0, 0);
lcd.print(" ");
lcd.setCursor(0, 0);
lcd.print("T:");
lcd.print(temperature,1);
lcd.print((char) 223);
lcd.print("C");
lcd.print(" ");
lcd.print("pH:");
lcd.print(phVal,1);
lcd.print(" ");
lcd.print(moisture);
lcd.print("%");
if(digitalRead(float_pin) == HIGH){
hasWater = true;
lcd.setCursor(0, 1);
lcd.print("Water Tank full ");
}
else{
hasWater = false;
lcd.setCursor(0, 1);
lcd.print("Refill Water ");
}
// when temperature exceeds 30C turn on the pump
if(temperature > tempHigh && hasWater){
digitalWrite(pump_pin,1);
lcd.setCursor(0, 3);
lcd.print("Pump Running ");
}
// when temperature exceeds 30C turn on the red LED
if(temperature > drought){
digitalWrite(led_pin,1);
}
else{
digitalWrite(led_pin,0);
}
// when soil moisture is below 40% turn on the pump
if(moisture < moistureLow && hasWater){
digitalWrite(pump_pin,1);
lcd.setCursor(0, 3);
lcd.print("Pump Running ");
}
if(phVal < phLow || phVal > phHigh){
lcd.setCursor(0, 2);
lcd.print("poor pH level ");
}
else{
lcd.setCursor(0, 2);
lcd.print("pH level good ");
}
// pump turns off when temperature and moisture are within the range
if((temperature < tempHigh) && (moisture > moistureLow)){
digitalWrite(pump_pin,0);
lcd.setCursor(0, 3);
lcd.print("Pump Off ");
}
}