from machine import Pin, ADC
import time
# Set up the PIR sensor (change GP16 if needed)
pir_sensor = Pin(16, Pin.IN) # PIR output connected to GP16
# Set up the LDR (using ADC0, which is GP26 on Raspberry Pi Pico)
ldr_sensor = ADC(26) # LDR connected to ADC0 (GP26)
# Set up the LED or relay
light = Pin(15, Pin.OUT) # LED or relay connected to GP15
# Define the threshold values
ldr_threshold = 10000 # Adjust based on testing
def read_ldr():
"""Reads the LDR value and returns True if it's dark, False otherwise."""
light_level = ldr_sensor.read_u16()
print("LDR Light Level:", light_level)
return light_level < ldr_threshold
try:
while True:
motion_detected = pir_sensor.value()
is_dark = read_ldr()
if motion_detected and is_dark:
print("Motion detected in the dark, turning on light.")
light.value(1) # Turn on light
else:
print("No motion or it's bright enough, turning off light.")
light.value(0) # Turn off light
time.sleep(1) # Adjust the delay as needed
except KeyboardInterrupt:
print("Program stopped")
finally:
light.value(0) # Ensure the light is off when the program stops