from machine import Pin, PWM, ADC
import time
LED_PIN = 2
BUZZER_PIN = 26
LIGHT_SENSOR_PIN = 34
led = Pin(LED_PIN, Pin.OUT, value=0)
buzzer = PWM(Pin(BUZZER_PIN), freq=440, duty=0)
light_sensor = ADC(Pin(LIGHT_SENSOR_PIN))
light_sensor.atten(ADC.ATTN_11DB)
light_threshold = 1500
check_interval = 200
def read_light():
return light_sensor.read()
while True:
current_light = read_light()
#光线过暗时开启LED灯和蜂鸣器;光线过亮时关闭LED灯和蜂鸣器
if current_light > light_threshold:
led.on()
buzzer.duty(512)
else:
led.off()
buzzer.duty(0)
time.sleep_ms(check_interval)