本站改版新增arduino频道
esp32 彩色rgb灯珠 变化的颜色呼吸灯
from machine import Pin, PWM import time import os # 创建引脚对象 led_r = Pin(0, Pin.OUT) led_b = Pin(1, Pin.OUT) led_g = Pin(2, Pin.OUT) # 创建PWM对象 pwm_led_r = PWM(led_r) pwm_led_r.freq(1000) # 注意,占空比如果设置为1023可以理解为全部是高电平,0表示全部是低电平,又因为对应LED引脚输出0即低电平才量,所以要用1023减去 pwm_led_r.duty(1023 - int(255 / 255 * 1023)) pwm_led_g = PWM(led_g) pwm_led_g.freq(1000) pwm_led_g.duty(1023 - int(206 / 255 * 1023)) pwm_led_b = PWM(led_b) pwm_led_b.freq(1000) pwm_led_b.duty(1023 - int(70 / 255 * 1023)) print("sleep 1...") time.sleep(1) # ------------ 添加如下代码 --------------- pwm_led_r.duty(1023) pwm_led_g.duty(1023) pwm_led_b.duty(1023) # 创建列表,存储pwm对象 leds = [pwm_led_r, pwm_led_g, pwm_led_b] while True: for led_pwm_obj in leds: for i in range(1023, -1, -1): led_pwm_obj.duty(i) time.sleep_ms(1) for i in range(1024): led_pwm_obj.duty(i) time.sleep_ms(1) led_pwm_obj.duty(1023)
效果:
Copyright © 2014 ESP56.com All Rights Reserved
晋ICP备14006235号-22 晋公网安备14108102001165号
执行时间: 0.0095920562744141 seconds