WS2812B彩灯条接Arduino:单线点亮逐颗寻址

2026-11-30 13:24:52 14阅读 所属项目:ESP32 开发板

普通RGB灯条整条同色,WS2812B是每颗灯珠独立寻址:一颗5050封装里集成了一颗WS2811驱动芯片,数据线串起来逐颗传色,想哪颗亮什么颜色就亮什么颜色。市面上叫NeoPixel的基本都是同一套协议。这篇用Arduino加FastLED库把它点亮,顺便记下供电、剪切和级联这几个动手时绕不开的点。

WS2812B可寻址灯条整卷实物

原理一句话:单线串行传色

WS2812B只用一根数据线。第一颗灯珠吃掉前24位(GRB各8位)把颜色留下,剩下的数据整形后转发给下一颗,像击鼓传花一样串下去。所以协议对时序要求很严:一位数据在1.25微秒左右完成,高电平宽窄区分0和1。Arduino UNO上FastLED用精准汇编出时序,别自己用delayMicroseconds硬写,稳定性差很多。

接线:三根线加一颗电阻

灯条三个焊盘:5V、GND、DIN。5V接外接电源或Arduino的5V引脚,DIN串一颗330到470欧电阻后接数字口5号,电阻的作用是抑制数据线振铃,减少第一颗灯珠花屏。灯条越长这个电阻越关键。GND必须和Arduino共地。

WS2812B灯条接Arduino UNO的接线图

装库与代码

库管理器里装FastLED。下面是官方示例,14颗灯珠、亮度64,主循环里做了呼吸和跑马几个效果。有一处写法要说明:示例里addLeds那行本来是模板调用,模板参数是WS2812B、数据口5、GRB色序(把代码里LED_TYPE宏改成WS2812B即可),因为站点会把尖括号当标签剥掉,下面贴的是去掉尖括号的等价版本,实际编译时把参数补回模板位置就能跑:

#include "FastLED.h"

#define LED_PIN     5
#define NUM_LEDS    14
#define BRIGHTNESS  64
#define LED_TYPE    WS2811
#define COLOR_ORDER GRB
CRGB leds[NUM_LEDS];

#define UPDATES_PER_SECOND 100

// This example shows several ways to set up and use 'palettes' of colors
// with FastLED.
//
// These compact palettes provide an easy way to re-colorize your
// animation on the fly, quickly, easily, and with low overhead.
//
// USING palettes is MUCH simpler in practice than in theory, so first just
// run this sketch, and watch the pretty lights as you then read through
// the code.  Although this sketch has eight (or more) different color schemes,
// the entire sketch compiles down to about 6.5K on AVR.
//
// FastLED provides a few pre-configured color palettes, and makes it
// extremely easy to make up your own color schemes with palettes.
//
// Some notes on the more abstract 'theory and practice' of
// FastLED compact palettes are at the bottom of this file.



CRGBPalette16 currentPalette;
TBlendType    currentBlending;

extern CRGBPalette16 myRedWhiteBluePalette;
extern const TProgmemPalette16 myRedWhiteBluePalette_p PROGMEM;


void setup() {
    delay( 3000 ); // power-up safety delay
    // WS2812B板条请把上一行的LED_TYPE宏定义改为WS2812B
    // 模板写法原为 addLeds 加 WS2812B, 5, GRB 尖括号参数(站点渲染剥尖括号,此处用注释陈述等价参数)
    FastLED.addLeds(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
    FastLED.setBrightness(  BRIGHTNESS );
    
    currentPalette = RainbowColors_p;
    currentBlending = LINEARBLEND;
}


void loop()
{
    ChangePalettePeriodically();
    
    static uint8_t startIndex = 0;
    startIndex = startIndex + 1; /* motion speed */
    
    FillLEDsFromPaletteColors( startIndex);
    
    FastLED.show();
    FastLED.delay(1000 / UPDATES_PER_SECOND);
}

void FillLEDsFromPaletteColors( uint8_t colorIndex)
{
    uint8_t brightness = 255;
    
    for( int i = 0; i < NUM_LEDS; i++) {
        leds[i] = ColorFromPalette( currentPalette, colorIndex, brightness, currentBlending);
        colorIndex += 3;
    }
}


// There are several different palettes of colors demonstrated here.
//
// FastLED provides several 'preset' palettes: RainbowColors_p, RainbowStripeColors_p,
// OceanColors_p, CloudColors_p, LavaColors_p, ForestColors_p, and PartyColors_p.
//
// Additionally, you can manually define your own color palettes, or you can write
// code that creates color palettes on the fly.  All are shown here.

void ChangePalettePeriodically()
{
    uint8_t secondHand = (millis() / 1000) % 60;
    static uint8_t lastSecond = 99;
    
    if( lastSecond != secondHand) {
        lastSecond = secondHand;
        if( secondHand ==  0)  { currentPalette = RainbowColors_p;         currentBlending = LINEARBLEND; }
        if( secondHand == 10)  { currentPalette = RainbowStripeColors_p;   currentBlending = NOBLEND;  }
        if( secondHand == 15)  { currentPalette = RainbowStripeColors_p;   currentBlending = LINEARBLEND; }
        if( secondHand == 20)  { SetupPurpleAndGreenPalette();             currentBlending = LINEARBLEND; }
        if( secondHand == 25)  { SetupTotallyRandomPalette();              currentBlending = LINEARBLEND; }
        if( secondHand == 30)  { SetupBlackAndWhiteStripedPalette();       currentBlending = NOBLEND; }
        if( secondHand == 35)  { SetupBlackAndWhiteStripedPalette();       currentBlending = LINEARBLEND; }
        if( secondHand == 40)  { currentPalette = CloudColors_p;           currentBlending = LINEARBLEND; }
        if( secondHand == 45)  { currentPalette = PartyColors_p;           currentBlending = LINEARBLEND; }
        if( secondHand == 50)  { currentPalette = myRedWhiteBluePalette_p; currentBlending = NOBLEND;  }
        if( secondHand == 55)  { currentPalette = myRedWhiteBluePalette_p; currentBlending = LINEARBLEND; }
    }
}

// This function fills the palette with totally random colors.
void SetupTotallyRandomPalette()
{
    for( int i = 0; i < 16; i++) {
        currentPalette[i] = CHSV( random8(), 255, random8());
    }
}

// This function sets up a palette of black and white stripes,
// using code.  Since the palette is effectively an array of
// sixteen CRGB colors, the various fill_* functions can be used
// to set them up.
void SetupBlackAndWhiteStripedPalette()
{
    // 'black out' all 16 palette entries...
    fill_solid( currentPalette, 16, CRGB::Black);
    // and set every fourth one to white.
    currentPalette[0] = CRGB::White;
    currentPalette[4] = CRGB::White;
    currentPalette[8] = CRGB::White;
    currentPalette[12] = CRGB::White;
    
}

// This function sets up a palette of purple and green stripes.
void SetupPurpleAndGreenPalette()
{
    CRGB purple = CHSV( HUE_PURPLE, 255, 255);
    CRGB green  = CHSV( HUE_GREEN, 255, 255);
    CRGB black  = CRGB::Black;
    
    currentPalette = CRGBPalette16(
                                   green,  green,  black,  black,
                                   purple, purple, black,  black,
                                   green,  green,  black,  black,
                                   purple, purple, black,  black );
}


// This example shows how to set up a static color palette
// which is stored in PROGMEM (flash), which is almost always more
// plentiful than RAM.  A static PROGMEM palette like this
// takes up 64 bytes of flash.
const TProgmemPalette16 myRedWhiteBluePalette_p PROGMEM =
{
    CRGB::Red,
    CRGB::Gray, // 'white' is too bright compared to red and blue
    CRGB::Blue,
    CRGB::Black,
    
    CRGB::Red,
    CRGB::Gray,
    CRGB::Blue,
    CRGB::Black,
    
    CRGB::Red,
    CRGB::Red,
    CRGB::Gray,
    CRGB::Gray,
    CRGB::Blue,
    CRGB::Blue,
    CRGB::Black,
    CRGB::Black
};



// Additionl notes on FastLED compact palettes:
//
// Normally, in computer graphics, the palette (or "color lookup table")
// has 256 entries, each containing a specific 24-bit RGB color.  You can then
// index into the color palette using a simple 8-bit (one byte) value.
// A 256-entry color palette takes up 768 bytes of RAM, which on Arduino
// is quite possibly "too many" bytes.
//
// FastLED does offer traditional 256-element palettes, for setups that
// can afford the 768-byte cost in RAM.
//
// However, FastLED also offers a compact alternative.  FastLED offers
// palettes that store 16 distinct entries, but can be accessed AS IF
// they actually have 256 entries; this is accomplished by interpolating
// between the 16 explicit entries to create fifteen intermediate palette
// entries between each pair.
//
// So for example, if you set the first two explicit entries of a compact 
// palette to Green (0,255,0) and Blue (0,0,255), and then retrieved 
// the first sixteen entries from the virtual palette (of 256), you'd get
// Green, followed by a smooth gradient from green-to-blue, and then Blue.

代码开头三个宏是关键:LED_PIN对应接线的数字口,NUM_LEDS是灯珠数量,BRIGHTNESS是全局亮度0到255。实验阶段亮度别拉满——14颗白光全开大约要0.9安,整卷30颗就能把USB口压垮,板子会反复重启。

剪切、焊接与安装

灯条背面标着剪刀图案的地方就是剪切线,每三颗一组可以剪断,剪口两侧各有三个焊盘,用24到26号线对色焊接就能级联延长,注意DIN方向别接反,数据是从箭头方向流的。装进铝槽再压扩散罩,光斑会匀很多,桌面氛围灯的观感直接上一个档次。

沿剪切线剪断WS2812B灯条

灯条装入铝型材灯槽

三个高频故障

第一颗灯珠花屏:九成是数据线没串电阻或者线太长,330欧加上把线压到30厘米内。中途某段开始全绿全红乱闪:5V供电塌了,量一下灯条末端的电压,低于4.3伏就分段供电。数据传一半断了:焊接点虚焊或者方向接反,从第一颗往后逐段排查最快。玩明白UNO这套,移植到ESP32加RMT驱动也就改一行引脚的事。


作者:野生极客一枚
链接:https://www.aiddithome.com/p/123cbc193f9f47.html
本作版权归作者所有,未经允许请勿转载。若存在违规行为,您可以点击 “举报”。


登录 后发表评论
5条评论
拆开看看再说
1楼 · 1小时前

装铝槽加扩散罩这个建议好,光斑匀了不止一个档次

周五下班搞一搞
2楼 · 1小时前

ESP32那边RMT驱动确实是改一行的事,迁移无痛

学生党玩机
3楼 · 1小时前

14颗0.9A这个数字有用,之前USB直推一直重启

触底反弹的Tony
4楼 · 1小时前

330欧电阻真的别省,第一次没串直接首珠狂闪

板砖搬不动
5楼 · 1小时前

剪完记得焊回去,DIN方向反了后半条全不亮,亲测