from machine import Pin, ADC
import time
#LDR pin configuration
ldr_pin = ADC(Pin(34))
ldr_pin.atten(ADC.ATTN_11DB) # Set the attenuation to 11dB
ldr_threshold = 2000 # Set the LDR threshold
#PIR pin configuration
pir_pin = Pin(32, Pin.IN) # PIR motion sensor is connected to GPIO 32
led_pin = Pin(23, Pin.OUT)
while True:
# Read the LDR sensor value
ldr_value = ldr_pin.read()
# Check if the LDR value is above the threshold
if ldr_value > ldr_threshold:
# If the LDR value is above the threshold, start PIR detection
pir_detected = pir_pin.value()
if pir_detected:
print("Motion detected!")
led_pin.value(1)
time.sleep(1) # Wait for 1 second to avoid multiple detections
else:
# If the LDR value is below the threshold, stop PIR detection
print("LDR value is below the threshold. PIR detection stopped.")
time.sleep(1) # Wait for 1 second before checking again