-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSmart_Irrigation_System.ino
More file actions
73 lines (59 loc) · 1.95 KB
/
Smart_Irrigation_System.ino
File metadata and controls
73 lines (59 loc) · 1.95 KB
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
67
68
69
70
71
72
73
#include <Wire.h>
#include <Adafruit_SSD1306.h>
#define SENSOR_PIN A0 // Soil moisture sensor
#define BUZZER_PIN D6 // Buzzer (GPIO12)
const int moistureThreshold = 30; // Dry soil threshold
// OLED display setup
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
void setup() {
Serial.begin(115200);
pinMode(BUZZER_PIN, OUTPUT);
digitalWrite(BUZZER_PIN, LOW);
// OLED initialization
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println("OLED initialization failed!");
for (;;);
}
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 10);
display.println("Smart Irrigation");
display.display();
delay(2000);
}
void loop() {
int sensorValue = analogRead(SENSOR_PIN);
float moisturePercent = (1 - (sensorValue / 1024.0)) * 100; // Convert to percentage
Serial.print("Soil Moisture Level: ");
Serial.print(moisturePercent);
Serial.println("%");
display.clearDisplay();
display.setTextSize(1);
display.setCursor(0, 0);
display.print("Moisture: ");
display.print(moisturePercent);
display.println("%");
// Draw moisture level bar
int barWidth = map(moisturePercent, 0, 100, 0, 100); // Scale moisture to bar width
display.drawRect(10, 30, 100, 10, WHITE); // Outline
display.fillRect(10, 30, barWidth, 10, WHITE); // Fill based on moisture level
if (moisturePercent < moistureThreshold) {
Serial.println("Soil is too dry! Activating Buzzer...");
// Activate Buzzer
digitalWrite(BUZZER_PIN, HIGH);
delay(1000);
digitalWrite(BUZZER_PIN, LOW);
display.setCursor(0, 50);
display.println("Dry Soil! Buzzer ON");
} else {
Serial.println("Soil moisture is sufficient.");
display.setCursor(0, 50);
display.println("Soil Moisture OK");
}
display.display();
Serial.println("-----------------------------");
delay(2000); // Read every 2 seconds
}