// 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
}
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter