//0-1023 light value/wokwi 8-1015/lower the darker//
// without diode, not available on wokwi
const int pirPin = 2; // PIR sensor output pin const
int ldrPin = A0; // LDR analog input pin const
int relayPin = 13; // LED pin
// Threshold values
int ldrThreshold = 300; // Adjust this value based on the ambient light level
int pirState = LOW; // Initial state of PIR sensor
int pirValue = 0; // PIR sensor value
void setup() {
pinMode(pirPin, INPUT);
pinMode(relayPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
// Read PIR sensor value
pirValue = digitalRead(pirPin);
// Read LDR value (light level)
int ldrValue = analogRead(ldrPin);
Serial.print("LDR Value: ");
Serial.print(ldrValue);
Serial.print(" | PIR Value: ");
Serial.println(pirValue);
// If PIR detects motion and ambient light level is low, turn on the LED
if (pirValue == HIGH && ldrValue < ldrThreshold) {
digitalWrite(relayPin, HIGH); // Turn on the LED
Serial.println("LED ON");
} // If no motion or ambient light is high, turn off the LED
else {
digitalWrite(relayPin, LOW); // Turn off the LED
Serial.println("LED OFF");
}
delay(500); // Short delay to avoid bouncing
}