-
Notifications
You must be signed in to change notification settings - Fork 61
/
Soil_moisture_sensor_with_Dht_11_Code.ino
66 lines (54 loc) · 1.51 KB
/
Soil_moisture_sensor_with_Dht_11_Code.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>
// Define your sensor and LCD
#define DHTPIN 2 // DHT11 data pin
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal_I2C lcd(0x27, 16, 2); // Address 0x27 for the 2x16 LCD
// Soil moisture sensor
int soilMoisturePin = A0;
int soilMoistureThreshold = 700; // Adjust this threshold as needed
int relayPin = 8;
void setup() {
// Initialize sensors and LCD
dht.begin();
lcd.init();
lcd.backlight();
// Relay pin setup
pinMode(relayPin, OUTPUT);
digitalWrite(relayPin, LOW); // Turn off the relay initially
}
void loop() {
// Read soil moisture
int soilMoisture = analogRead(soilMoisturePin);
// Read temperature and humidity
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
// Display on the LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Soil Moisture:");
lcd.setCursor(0, 1);
lcd.print(soilMoisture);
delay(2000); // Wait for a moment to read the display
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(temperature);
lcd.print("C");
lcd.setCursor(0, 1);
lcd.print("Humidity: ");
lcd.print(humidity);
lcd.print("%");
// Check soil moisture level and control the relay
if (soilMoisture < soilMoistureThreshold) {
// Soil is too dry, turn on the relay
digitalWrite(relayPin, HIGH);
} else {
// Soil is wet enough, turn off the relay
digitalWrite(relayPin, LOW);
}
delay(2000); // Update every 2 seconds
}