from machine import Pin, ADC, PWM
import time
ldr = ADC(Pin(34))
ldr.atten(ADC.ATTN_0DB)
led = Pin(15, Pin.OUT)
buzzer = PWM(Pin(17), freq=300, duty=512)
threshold = 500
def read_ldr():
return ldr.read()
while True:
light_level = read_ldr()
print("LDR Value (Lux):", light_level)
if light_level < threshold:
led.on()
buzzer.duty(512) # Activate the buzzer
time.sleep(0.5)
buzzer.duty(0)
else:
led.off()
buzzer.duty(0) # Deactivate the buzzer
time.sleep(1)