|
LED幻彩灯带上面装有小型LED模块。 WS2812B是这些LED模块之一,它具有级联端口传输机制,只需要一根线。
这意味着多个WS2812B可以级联,并且可以通过单个连接单独控制以进行数据传输。级联多个WS2812B模块仅需要VCC、GND和数据三根线。因此,WS2812B模块非常适合LED幻彩灯带。如今,基于WS2812B的LED像素条具有各种变体。通常,每米可购买30至144个WS2812B模块的灯带。每米144个模块时,模块之间几乎没有空间。在本篇文章中,我们将主要介绍如何对Arduino进行编程,以使用FastLED库控制基于WS2812B的LED幻彩灯带。 FastLED是一个库,可以控制所有类型的LED灯条(WS2810、WS2811、LPD8806、Neopixel等)。
所需的材料清单: – Arduino Nano – 跳线 – 面包板 – 基于WS2812B的LED幻彩灯带
LED幻彩灯带的连接: 接线设置非常容易,因为LED幻彩灯带只有三个必须连接的输入引脚。通常,这些输入引脚代表GND、Vdd 5V和数据连接。我的LED幻彩灯带有三根线:白色(GND)、红色(Vdd)和绿色(Data)。输入引脚的颜色在厂商之间可能有所不同。因此,请查看购买的LED幻彩灯带的描述或数据表。 Arduino可以为带有三十个WS2812B模块的LED灯带提供足够的电源。因此,我们可以将GND引脚和5V引脚直接直接连接到白线和红线。根据一些论坛帖子的介绍,您可以从Arduino的5V引脚获得大约400mA的电流。最后但并非最不重要的一点是,我们将数据引脚(绿线)连接到Arduino的引脚3,您可以使用任何支持PWM信号的引脚。
将Arduino Nano连接至LED幻彩灯带的示意图。
示例源代码: 如前所述,该程序利用FastLED库来控制LED幻彩灯带。首先,包含FastLED头文件。在setup函数中,初始化LED幻彩灯带。由于FastLED支持很多的LED灯条(不同的LED模块,不同的长度等),因此初始化需要LED模块类型、用于数据传输的Arduino引脚号、颜色顺序、对应表示LED的数组的引用以及编号的LED。也可以设置颜色校正以便改善颜色的保真度。 接下来,有一个关闭所有像素的函数(“ showProgramCleanUp”)。此函数对于清空LED灯条颜色非常有用。 示例源代码包含三个不同的“灯光程序”。每个灯光程序均作为一个功能实现。第一个程序以随机选择的颜色(“ showProgramRandom”)打开所有LED。下一个程序显示单色像素从LED灯带的开始移到结尾(“ showProgramShiftSinglePixel”)。最后一个程序显示了多个像素如何从头移动到结尾(“ showProgramShiftMultiPixel”)。在此灯光程序中,每个像素都有一个随机选择的颜色。 在loop函数中,每个灯光程序使用不同的参数调用两次。在对调用灯光程序功能的函数之前,通过调用特殊程序“ showProgramCleanUp”来关闭LED灯带的所有像素。 - #include "FastLED.h"
- #define DATA_PIN 3
- #define LED_TYPE WS2812B
- #define COLOR_ORDER GRB
- #define NUM_LEDS 30
- #define BRIGHTNESS 96
- CRGB leds[NUM_LEDS];
- void setup() {
- delay(3000); // initial delay of a few seconds is recommended
- FastLED.addLeds<LED_TYPE,DATA_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip); // initializes LED strip
- FastLED.setBrightness(BRIGHTNESS);// global brightness
- }
- // switches off all LEDs
- void showProgramCleanUp(long delayTime) {
- for (int i = 0; i < NUM_LEDS; ++i) {
- leds[i] = CRGB::Black;
- }
- FastLED.show();
- delay(delayTime);
- }
- // switches on all LEDs. Each LED is shown in random color.
- // numIterations: indicates how often LEDs are switched on in random colors
- // delayTime: indicates for how long LEDs are switched on.
- void showProgramRandom(int numIterations, long delayTime) {
- for (int iteration = 0; iteration < numIterations; ++iteration) {
- for (int i = 0; i < NUM_LEDS; ++i) {
- leds[i] = CHSV(random8(),255,255); // hue, saturation, value
- }
- FastLED.show();
- delay(delayTime);
- }
- }
- // Shifts a single pixel from the start of strip to the end.
- // crgb: color of shifted pixel
- // delayTime: indicates how long the pixel is shown on each LED
- void showProgramShiftSinglePixel(CRGB crgb, long delayTime) {
- for (int i = 0; i < NUM_LEDS; ++i) {
- leds[i] = crgb;
- FastLED.show();
- delay(delayTime);
- leds[i] = CRGB::Black;
- }
- }
- // Shifts multiple pixel from the start of strip to the end. The color of each pixel is randomized.
- // delayTime: indicates how long the pixels are shown on each LED
- void showProgramShiftMultiPixel(long delayTime) {
- for (int i = 0; i < NUM_LEDS; ++i) {
- for (int j = i; j > 0; --j) {
- leds[j] = leds[j-1];
- }
- CRGB newPixel = CHSV(random8(), 255, 255);
- leds[0] = newPixel;
- FastLED.show();
- delay(delayTime);
- }
- }
- // main program
- void loop() {
- showProgramCleanUp(2500); // clean up
- showProgramRandom(100, 100); // show "random" program
-
- showProgramCleanUp(2500); // clean up
- showProgramRandom(10, 1000); // show "random" program
-
- showProgramCleanUp(2500); // clean up
- showProgramShiftSinglePixel(CRGB::Blue, 250); // show "shift single pixel program" with blue pixel
-
- showProgramCleanUp(2500); // clean up
- showProgramShiftSinglePixel(CRGB::Maroon, 100); // show "shift single pixel program" with maroon pixel
-
- showProgramCleanUp(2500); // clean up
- showProgramShiftMultiPixel(500); // show "shift multi pixel" program
-
- showProgramCleanUp(2500); // clean up
- showProgramShiftMultiPixel(50); // show "shift multi pixel" program
- }
复制代码
编译源代码并将其传输到Arduino后,LED灯带应会根据活动的灯光程序显示彩色像素。
|