一.点亮单LED
from machine import Pin
from utime import sleep
print("Hello, ESP32!")
led = Pin(15, Pin.OUT)
while True:
led.on()
sleep(0.5)
led.off()
sleep(0.5)
实现呼吸灯
from machine import Pin,PWM
from time import sleep_ms
led_pin=Pin(4,Pin.OUT)#引脚控制对象
pwm_pin=PWM(led_pin,freq=1000,duty=0)#模拟信号对象,频率1023,占空比0
while True:
times=0
while times<1023:
pwm_pin.duty(times)
times+=1
sleep_ms(2)
while times>0:
pwm_pin.duty(times)
times-=1
sleep_ms(2)
'''优化1.
for duty_ts in range(0,1024):
pwm_pin.duty(duty_ts)
sleep_ms(3)
for duty_ts in range(1023,-1,-1):
pwm_pin.duty(duty_ts)
sleep_ms(3