1# 源码来源 http://www.1zlab.com/wiki/micropython-esp32/pwm/
2
3import time, math
4from mpython import *
5
6
7def pulse(switch, period, gears):
8 # 呼吸灯核心代码
9 # 借用sin正弦函数,将PWM范围控制在 23 - 1023范围内
10 # switch 开关对象
11 # period 呼吸一次的周期 单位/毫秒
12 # gears 呼吸过程中经历的亮度档位数
13
14 for i in range(2 * gears):
15 switch.write_analog(int(math.sin(i / gears * math.pi) * 500) + 523)
16 # 延时
17 time.sleep_ms(int(period / (2 * gears)))
18
19
20# led 灯对象
21switch_led = MPythonPin(0, PinMode.PWM)
22
23# 呼吸十次
24for i in range(10):
25 pulse(switch_led, 2000, 100)