import board
import pwmio
ledpin = pwmio.PWMOut(board.LED, frequency=25000, duty_cycle=0)
ledpin.duty_cycle = 32768 # mid-point 0-65535 = 50 % duty-cycle
可通过调整 PWM 占空比控制 LED 亮度。占空比范围为 0%~100%,采用 16 位精度,对应十进制数值 0~65535、十六进制数值 0~FFFF。在 REPL 中输入以下代码:
import board
import pwmio
ledpin = pwmio.PWMOut(board.LED, frequency=25000, duty_cycle=0)
ledpin.duty_cycle = 32768 # mid-point 0-65535 = 50 % duty-cycle
在 REPL 中再次输入最后一行代码,将 PWM 占空比设为最大值,使 LED 达到最高亮度:
ledpin.duty_cycle = 65535
使用 while 和 for 循环制作呼吸灯:
import board
import pwmio
import time
ledpin = pwmio.PWMOut(board.LED, frequency=25000, duty_cycle=0)
while True:
for i in range(0, 65535, 1):
ledpin.duty_cycle = i
for i in range(65535, 0, -1):
ledpin.duty_cycle = i