const int ldrPin = 34; // ESP32 analog pin for LDR
const int relayPin = 26; // ESP32 digital pin for relay control
int threshold = 1800; // Adjust based on your LDR & wiring (0-4095 for 12-bit ADC)
void setup() {
Serial.begin(115200);
pinMode(ldrPin, INPUT);
pinMode(relayPin, OUTPUT);
digitalWrite(relayPin, LOW); // Start with relay off
}
void loop() {
int ldrValue = analogRead(ldrPin);
Serial.print("LDR Value: ");
Serial.println(ldrValue);
// If it's dark (below threshold), turn on relay (LED glows)
if (ldrValue < threshold) {
digitalWrite(relayPin, HIGH);
} else {
digitalWrite(relayPin, LOW);
}
delay(500);
}