1
2
3
4
5
6
7
8
9#define GREEN_LED 2
10#define YELLOW_LED 3
11#define RED_LED 4
12#define SENSOR_PIN A5
13
14
15
16
17#define DRY_THRESHOLD 800
18#define WET_THRESHOLD 500
19
20void setup() {
21 pinMode(GREEN_LED, OUTPUT);
22 pinMode(YELLOW_LED, OUTPUT);
23 pinMode(RED_LED, OUTPUT);
24
25 digitalWrite(GREEN_LED, LOW);
26 digitalWrite(YELLOW_LED, LOW);
27 digitalWrite(RED_LED, LOW);
28
29 Serial.begin(9600);
30}
31
32void loop() {
33 int sensorValue = analogRead(SENSOR_PIN);
34
35 Serial.print("Soil moisture sensor value: ");
36 Serial.println(sensorValue);
37
38 if(sensorValue > 0 && sensorValue <= WET_THRESHOLD) {
39
40 digitalWrite(GREEN_LED, HIGH);
41 digitalWrite(YELLOW_LED, LOW);
42 digitalWrite(RED_LED, LOW);
43 } else if (sensorValue > WET_THRESHOLD && sensorValue <= DRY_THRESHOLD) {
44
45 digitalWrite(GREEN_LED, LOW);
46 digitalWrite(YELLOW_LED, HIGH);
47 digitalWrite(RED_LED, LOW);
48 } else {
49
50 digitalWrite(GREEN_LED, LOW);
51 digitalWrite(YELLOW_LED, LOW);
52 digitalWrite(RED_LED, HIGH);
53 }
54}