// Initialize the motion sensor and LDR pins
int motionSensor = 2;
//digital pin D2
int ldrSensor = A0;
//analog pin 0
// Initialize the LED pin
int ledPin = 13;
//led pin D13
// Initialize the threshold values
int motionThreshold = 500;
int ldrThreshold = 500;
void setup() {
// Set the motion sensor pin as an input
pinMode(motionSensor, INPUT);
// Set the LED pin as an output
pinMode(ledPin, OUTPUT);
// Serial communication for debugging
Serial.begin(115200);
}
void loop() {
// Read the motion sensor value
int motionValue = digitalRead(motionSensor);
//read pir from digital pin
// Read the LDR sensor value
int ldrValue = analogRead(ldrSensor);
//read the sensor value from analog pin
// output of the sensors
Serial.print("Motion Value: ");
//print motion value
Serial.print(motionValue);
Serial.println(" ");
Serial.print("LDR Value: ");
// print ldr value
Serial.println(ldrValue);
// Check if motion is detected or LDR value is below threshold
if (motionValue == HIGH || ldrValue < ldrThreshold)
//condition for motion and ldr
{
// Turn on the LED
digitalWrite(ledPin, HIGH);
//led HIGH
} else {
// Turn off the LED
digitalWrite(ledPin, LOW);
}
// Delay to prevent rapid LED toggling
delay(4000);
}