#include <ESP32Servo.h>
int soilPin = 34; // Soil sensor (potentiometer)
int servoPin = 18; // Servo
int ledPin = 19; // LED
int pirPin = 21; // PIR sensor
int buzzerPin = 5; // Buzzer
Servo myServo;
int soilValue = 0;
void setup() {
Serial.begin(115200);
// Servo
myServo.attach(servoPin);
// LED + Buzzer
pinMode(ledPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
// PIR sensor
pinMode(pirPin, INPUT);
}
void loop() {
// --- Soil Moisture Section ---
soilValue = analogRead(soilPin); // ESP32 ADC range 0–4095
Serial.print("Soil Moisture: ");
Serial.println(soilValue);
if (soilValue < 1500) { // Threshold
Serial.println("Soil Dry → Servo + LED ON");
digitalWrite(ledPin, HIGH);
myServo.write(90);
} else {
Serial.println("Soil Wet → Servo + LED OFF");
digitalWrite(ledPin, LOW);
myServo.write(0);
}
// --- PIR + Buzzer Section ---
int pirState = digitalRead(pirPin);
if (pirState == HIGH) {
Serial.println("Motion Detected → Buzzer ON");
digitalWrite(buzzerPin, HIGH); // Buzzer ON while motion detected
} else {
Serial.println("No Motion → Buzzer OFF");
digitalWrite(buzzerPin, LOW); // Buzzer OFF when no motion
}
delay(200); // small delay for stability
}