ArduinoVN
Đăng nhập Tham gia
Thư viện Code /u/linucat /23/05/2026

FastLED vs Adafruit NeoPixel: so sánh & ví dụ cho LED WS2812

Thảo luận

LED WS2812 (NeoPixel) — mỗi LED có chip điều khiển riêng, chỉ cần 1 dây data — là chuẩn vàng cho dải LED RGB. Hai thư viện chính: Adafruit NeoPixelFastLED. Bài này so sánh và cho code mẫu cả hai.

1. So sánh nhanh

Tiêu chíAdafruit NeoPixelFastLED
Đơn giảnRất dễ họcPhức tạp hơn
Hiệu ứng có sẵnÍt (tự code)Hàng chục (palette, blur, fire)
Hiệu năngOKTốt hơn ~2×
Chip hỗ trợWS2811/12/12BWS2811/12, APA102, LPD8806, nhiều
RAM3 byte/LED3 byte/LED + struct CRGB
Cộng đồngLớn (Adafruit tutorial)Lớn (maker, demoscene)

Quy tắc: NeoPixel cho demo đơn giản, FastLED cho hiệu ứng đẹp.

2. Đấu nối WS2812

  • VCC: 5V. Mỗi LED tối đa 60mA full white → 30 LED = 1.8A. Dùng nguồn riêng.
  • GND: chung với MCU.
  • DIN: bất kỳ digital. Khuyến nghị thêm điện trở 470Ω giữa MCU và LED đầu để giảm reflect.
  • Tụ 1000µF giữa VCC-GND ở đầu dải để chống sụt áp đột ngột.

WS2812 5V logic — ESP32 3.3V có thể bị skip frame ở dải dài. Dùng level shifter 74HCT245.

3. Adafruit NeoPixel — hello world

#include 

#define PIN_LED  6
#define N_LEDS   30

Adafruit_NeoPixel strip(N_LEDS, PIN_LED, NEO_GRB + NEO_KHZ800);

void setup() {
  strip.begin();
  strip.setBrightness(50);   // 0..255, GIẢM để tiết kiệm pin/nhiệt
  strip.show();              // tắt tất cả
}

void loop() {
  for (int i = 0; i < N_LEDS; i++) {
    strip.setPixelColor(i, strip.Color(255, 0, 0));   // đỏ
  }
  strip.show();
  delay(500);
}

Lưu ý strip.show() sau mỗi thay đổi mới gửi data lên LED.

4. NeoPixel — color và brightness

strip.Color(R, G, B);                    // 0..255 mỗi kênh
strip.Color(R, G, B, W);                 // có white nếu RGBW
strip.ColorHSV(hue);                     // 0..65535 hue, full S/V
strip.setBrightness(50);                 // global brightness
strip.fill(strip.Color(0,255,0), 0, 10); // 10 LED đầu màu xanh

5. NeoPixel — rainbow đơn giản

void rainbow(uint16_t speed = 5) {
  for (uint16_t i = 0; i < 65536; i += speed) {
    for (int j = 0; j < N_LEDS; j++) {
      uint16_t h = (i + j * 65536L / N_LEDS) & 65535;
      strip.setPixelColor(j, strip.gamma32(strip.ColorHSV(h)));
    }
    strip.show();
    delay(10);
  }
}

6. FastLED — hello world

#include 

#define PIN_LED 6
#define N_LEDS  30
CRGB leds[N_LEDS];

void setup() {
  FastLED.addLeds(leds, N_LEDS);
  FastLED.setBrightness(50);
}

void loop() {
  fill_solid(leds, N_LEDS, CRGB::Blue);
  FastLED.show();
  delay(500);
}

7. FastLED — hiệu ứng có sẵn

// 1. Palette
CRGBPalette16 pal = RainbowColors_p;
for (int i = 0; i < N_LEDS; i++) {
  leds[i] = ColorFromPalette(pal, (i * 256 / N_LEDS) & 0xFF);
}
FastLED.show();

// 2. Fade
fadeToBlackBy(leds, N_LEDS, 20);

// 3. Sinelon (1 chấm chạy + đuôi mờ)
fadeToBlackBy(leds, N_LEDS, 20);
int pos = beatsin16(13, 0, N_LEDS - 1);
leds[pos] += CHSV(millis() / 10, 255, 192);
FastLED.show();

FastLED có cả hàm beat8(), beatsin16(), blur1d(), random8() dành cho hiệu ứng nhịp nhàng — code 5 dòng ra effect đẹp.

8. CRGB và CHSV

leds[0] = CRGB::Red;
leds[1] = CRGB(255, 100, 0);   // orange
leds[2] = CHSV(96, 255, 200);  // hue 96 (green), max sat, dim
leds[3].r = 100;
leds[3].setRGB(50, 100, 150);

9. Power management — FastLED tự lo

FastLED.setMaxPowerInVoltsAndMilliamps(5, 1500);   // 5V, 1.5A max

FastLED tự giảm brightness để không vượt giới hạn — tránh cháy nguồn USB.

10. ESP32 — RMT cho 100+ LED

Cả 2 thư viện trên ESP32 dùng RMT peripheral — gửi data WS2812 không block CPU. Có thể chạy 500+ LED với refresh 60Hz mượt.

11. Lỗi thường gặp

  • LED đầu sai màu, các LED sau OK: timing issue, thêm điện trở 470Ω và tụ.
  • LED nháy ngẫu nhiên: dây data quá dài (>1m), tín hiệu nhiễu. Dùng dây xoắn (twisted pair) hoặc thêm buffer 74HCT245.
  • Nguồn yếu: bật full white 30 LED = 1.8A, USB chỉ 500mA. Sụt áp → MCU reset.
  • Màu sai (G và R đổi): WS2812 đôi khi là GRB, đôi khi RGB. Đổi NEO_GRBNEO_RGB, hoặc GRBRGB trong FastLED template.

Khi nào dùng cái nào?

  • Adafruit NeoPixel: project đơn giản, vài LED, code clean.
  • FastLED: dải dài, animation đẹp, có thời gian học API.

Liên quan

Áp dụng vào dự án Điều khiển LED strip NeoPixel WS2812LED ma trận WS2812 8x32.

Thảo luận (0)

Đăng nhập để tham gia thảo luận.
Chưa có bình luận nào. Hãy là người đầu tiên!