1# Source code from http://www.1zlab.com/wiki/micropython-esp32/pwm/
2
3import time, math
4from mpython import *
5
6
7def pulse(switch, period, gears):
8 # Core code of the breathing light
9 # Using the sin function to control the PWM range within 23 - 1023
10 # switch: the switch object
11 # period: the period of one breath, unit / milliseconds
12 # gears: the number of brightness levels experienced during breathing
13
14 for i in range(2 * gears):
15 switch.write_analog(int(math.sin(i / gears * math.pi) * 500) + 523)
16 # Delay
17 time.sleep_ms(int(period / (2 * gears)))
18
19
20# LED light object
21switch_led = MPythonPin(0, PinMode.PWM)
22
23# Breathe ten times
24for i in range(10):
25 pulse(switch_led, 2000, 100)