// Define pin numbers
const int ldrPin = A0; // LDR connected to analog pin A0
const int relayPin = 3; // Relay control connected to digital pin 3
// Set a light threshold (adjust this value as needed)
const int lightThreshold = 500; // Change this value based on your environment
void setup() {
// Initialize the relay pin as an output
pinMode(relayPin, OUTPUT);
// Start with relay off
digitalWrite(relayPin, HIGH);
// Start serial communication for debugging
Serial.begin(9600);
}
void loop() {
// Read the value from the LDR
int ldrValue = analogRead(ldrPin);
// Print the LDR value for debugging
Serial.print("LDR Value: ");
Serial.println(ldrValue);
// Check if the LDR value is below the threshold
if (ldrValue < lightThreshold) {
// Activate the relay
digitalWrite(relayPin, LOW);
} else {
// Deactivate the relay
digitalWrite(relayPin, HIGH);
}
// Small delay before the next reading
delay(500);
}