只需一支數位訊號即可各別控制每一顆 LED,可串接多個燈環,但需注意電流量。
光線色彩由三原色(紅、綠、藍)組成,每一種顏色可控制的值為 0 ~ 255,因此總共有 256 × 256 × 256 = 16,777,216 種顏色。
外觀尺寸:外徑 50、內徑 36 公厘(mm)
螺絲鎖孔:直徑 2 公厘(mm)
工作電壓:直流(DC)5V
工作電流:最大 720 豪安培(mA)
此模組需要自行焊接,這邊提供簡單焊接步驟:模組背面焊接點先上一點焊錫、電線剝線約 3 公厘(mm)上錫,再將電線剝線端貼著LED上的焊接點再將接觸點用焊槍加熱,而電線另外一端使用壓線鉗將杜邦端子簧片及膠殼加工上去。
使用跳線參考下圖所示連接,跳線的顏色選用通常正極為紅色,負極為黑色以利辨識避免短路,訊號線依個人習慣即可。
務必確認正負極是否正確,接反可能導致電路板燒壞!
1、開啟 Arduino IDE ,並使用滑鼠左鍵點擊圖中標示處,草稿碼 → 匯入程式庫 → 管理程式庫...。
2、圖中標示處輸入「Adafruit NeoPixel」,英文大小寫不影響搜尋結果。
3、找到「Adafruit NeoPixel by Adafruit」,並使用滑鼠左鍵點擊右方「安裝」,安裝完畢即可關閉視窗。
1、使用滑鼠左鍵點擊圖中標示處,檔案 → 範例 → Adafruit NeoPixel → simple。
若無顯示「Adafruit NeoPixel」請重新確認上一個步驟「安裝程式庫」是否有誤。
2、範例內容如下,建議先做完下一步驟看一下 LED 亮的方式,再回頭來看程式碼會比較容易理解。
將 NUMPIXELS 的值改為 12 如下圖,並上傳程式碼,上傳成功後可以看到燈環從編號 0 的燈珠開始每隔 500 毫秒多亮 1 顆。
將腳位編號前加上「D」、NUMPIXELS 的值改為 12 如下圖,修改好後上傳程式碼,上傳成功後可以看到燈環從編號 0 的燈珠開始每隔 500 毫秒多亮 1 顆。
作者已經使用註解將程式內容解釋得很清楚,小編這裡大約解釋一下。
(1)前置處理引入「Adafruit_NeoPixel.h」標頭檔
#include <Adafruit_NeoPixel.h>
(2)針對 AVR 處理器、Adafruit Trinket 開發板引入「avr/power.h」標頭檔
#ifdef __AVR__
#include <avr/power.h> // Required for 16 MHz Adafruit Trinket
#endif
(3)設定開發板連接 LED 訊號腳位的接腳、LED燈珠的數量
// Which pin on the Arduino is connected to the NeoPixels?
#define PIN 6 // On Trinket or Gemma, suggest changing this to 1
// How many NeoPixels are attached to the Arduino?
#define NUMPIXELS 16 // Popular NeoPixel ring size
(4)宣告 Adafruit_NeoPixel 物件,物件名稱為「pixels」,宣告的同時將 LED 燈珠數量、使用的腳位以及針對燈類型的設定
// When setting up the NeoPixel library, we tell it how many pixels,
// and which pin to use to send signals. Note that for older NeoPixel
// strips you might need to change the third parameter -- see the
// strandtest example for more information on possible values.
Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
(5)定義「DELAYVAL」為「500」,後續用來燈珠間暫停的時間(單位為毫秒)。
#define DELAYVAL 500 // Time (in milliseconds) to pause between pixels
(6)這一段是針對「Adafruit Trinket 5V 16 MHz」開發板,如使用其他開發板可將這段刪除。
// These lines are specifically to support the Adafruit Trinket 5V 16 MHz.
// Any other board, you can remove this part (but no harm leaving it):
#if defined(__AVR_ATtiny85__) && (F_CPU == 16000000)
clock_prescale_set(clock_div_1);
#endif
// END of Trinket-specific code.
(7)將「pixels」物件初始化。
pixels.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
(8)將「pixels」的所有燈珠關閉。
pixels.clear(); // Set all pixel colors to 'off'
(9)將「pixels」的所有燈珠關閉。
pixels.clear(); // Set all pixel colors to 'off'
(10)燈條、燈環上的第一顆燈珠編號都從 0 開始,因此這段迴圈從「i = 0」到「i = 11」。
// The first NeoPixel in a strand is #0, second is 1, all the way up
// to the count of pixels minus one.
for(int i=0; i<NUMPIXELS; i++) { // For each pixel...
}
(11)這裡用到兩個函式,
① void setPixelColor(uint16_t n, uint32_t c);
設定編號為 n 燈珠的顏色 c。
② uint32_t Color(uint8_t r, uint8_t g, uint8_t b);
此函式的參數分別為紅色 r、綠色 g、藍色 b 的數值(0 ~ 255),將參數帶入 Color 函式,它將會回傳 uint32_t 資料型態的數值。
上述的顏色數值也就是該顏色的亮度,而燈珠的顏色就是由這三原色組成。
// pixels.Color() takes RGB values, from 0,0,0 up to 255,255,255
// Here we're using a moderately bright green color:
pixels.setPixelColor(i, pixels.Color(0, 150, 0));