1# Source: http://www.1zlab.com/wiki/micropython-esp32/pwm/
2
3import time, math
4from mpython import *
5
6
7def pulse(switch, period, gears):
8 # Breathing LED core code
9 # Using sine function to control PWM range within 23-1023
10 # switch: toggle object
11 # period: one breath cycle in milliseconds
12 # gears: number of brightness levels 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: LED object
21switch_led = MPythonPin(0, PinMode.PWM)
22
23# Breathe ten times
24for i in range(10):
25 pulse(switch_led, 2000, 100)