Automatic Night Light

PHOTO EMBED

Fri Jan 30 2026 03:47:40 GMT+0000 (Coordinated Universal Time)

Saved by @iliavial

// Pin Definitions
const int ldrPin = A0;      // Light Dependent Resistor
const int potPin = A1;      // Potentiometer for sensitivity
const int relayPin = 8;     // Relay Control Pin

void setup() {
  pinMode(relayPin, OUTPUT);
}

void loop() {
  // Read the sensors (0 - 1023)
  int lightLevel = analogRead(ldrPin);
  int threshold = analogRead(potPin);

   // Logic: If light is lower than threshold, turn on relay
  if (lightLevel < threshold) {
    digitalWrite(relayPin, HIGH); // Relay ON
  } else {
    digitalWrite(relayPin, LOW);  // Relay OFF
  }

  delay(100); // Small stability delay
}
content_copyCOPY