0x02 NeoPixel
本篇主要簡介micropython中的neopixel
模塊
neopixel
為針對WS2812B系列的RGB+内置IC的智能LED所用模塊。
用法如下
- 新建實例:
np = neopixel.NeoPixel(pin, number, timing=1)
pin
為與LED的DIN pin相連的GPIO pin,number
為串聯的LED數目,timing
為刷新頻率,1
為800KHz,0
為400KHz - 設置所有LED的顔色:
np.fill(color)
,color
為RGB順序的3元素Tuple,如(255, 255, 255)
則為白光,(255, 0, 0)
紅,(0, 255, 0)
綠,(0, 0, 255)
藍 - 設置單顆LED顔色:
np[i] = (0, 255, 255)
- 最後記得刷新LED的顔色狀態:
np.write()
AIR與LED鏈接方式如下圖
!
新建文檔main.py
内容如下:
import machine
import neopixel
PIN = 4 # GPIO 4
LED = 5 # 5顆LED
RATE = 1 # 800KHz,防止LED閃爍
np = neopixel.NeoPixel(machine.Pin(pin), leds, timing=RATE)
def fillColor():
for i in range(np.n):
np[i] = (255, 0, 0) # 為每顆LED寫入紅色
np.write() # 刷新LED顔色狀態
try:
while True:
fillColor() #循環執行部分
except KeyboardInterrupt:
print('stop running, cleaning the neopixels')
finally:
np.fill((0, 0, 0))
np.write()
將文檔上傳至AIR,按reset重啓即可看見LED變成紅色。
謝謝!