esp8266-microPython呼吸灯实例

microPython 版本 ESP8266_GENERIC-20240105-v1.22.1.bin

呼吸灯实例

#boot.py

from machine import Pin, PWM  
import time  
  
# 初始化PWM对象  
led = PWM(Pin(2))  
  
# 设置PWM频率  
led.freq(1000)  
  
# 循环控制LED灯的亮度  
while True:  
    # 从0逐渐增加亮度  
    for brightness in range(0, 256):  
        led.duty_u16(brightness * 1024)  # 设置PWM占空比,范围为0-1023  
        time.sleep(0.01)  # 延时0.01秒,以实现呼吸效果  
    # 从255逐渐降低亮度到0  
    for brightness in range(255, -1, -1):  
        led.duty_u16(brightness * 1024)  # 设置PWM占空比,范围为0-1023  
        time.sleep(0.01)  # 延时0.01秒,以实现呼吸效果

 

posted @ 2024-01-28 02:27  好咯莫讲咯  阅读(119)  评论(0)    收藏  举报