from machine import Pin, ADC
import time
# Pin setup for LEDs
led1 = Pin(2, Pin.OUT)
led2 = Pin(3, Pin.OUT)
led3 = Pin(4, Pin.OUT)
# Pin setup for PIR sensors
pir1 = Pin(5, Pin.IN)
pir2 = Pin(6, Pin.IN)
pir3 = Pin(7, Pin.IN)
# LDR setup
ldr = ADC(Pin(26)) # LDR connected to GPIO 26 (ADC1)
ldr.width(ADC.WIDTH_12BIT) # Set ADC resolution to 12 bits (0-4095)
ldr.atten(ADC.ATTN_0DB) # No attenuation (0-3.3V)
# Threshold for the LDR to turn LEDs on
threshold = 1500
while True:
# Read the LDR value (light level)
ldr_value = ldr.read()
# Read the PIR sensors (motion detection)
motion1 = pir1.value()
motion2 = pir2.value()
motion3 = pir3.value()
# Check if the LDR value exceeds threshold and any PIR sensor detects motion
if ldr_value > threshold:
if motion1:
led1.on() # Turn on LED1 if PIR1 detects motion
else:
led1.off() # Turn off LED1 if no motion detected
if motion2:
led2.on() # Turn on LED2 if PIR2 detects motion
else:
led2.off() # Turn off LED2 if no motion detected
if motion3:
led3.on() # Turn on LED3 if PIR3 detects motion
else:
led3.off() # Turn off LED3 if no motion detected
else:
# If LDR value is below threshold, turn off all LEDs
led1.off()
led2.off()
led3.off()
time.sleep(0.1) # Delay to avoid excessive CPU usage