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

呼吸灯实例

esp8266-microPython呼吸灯实例_初始化

#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秒,以实现呼吸效果
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.