00_ESP32 WS2812控制

00_ESP32_WS2812控制

Arduino 编程语言基于 C/C++,专为 Arduino 及其兼容的微控制器平台设计。它通过一套简单易懂的函数库和核心结构,极大地简化了微控制器的编程,让初学者和专业人士都能轻松地操控硬件。

Arduino IDE 安装教程:芯路城

img

bba8ef86be4ca62f751d05bc9eb07dff.png

示例程序,板载RGB灯闪烁:

/*
  Blink

  Turns an LED on for one second, then off for one second, repeatedly.

  Most Arduinos have an on-board LED you can control. On the UNO, MEGA and ZERO
  it is attached to digital pin 13, on MKR1000 on pin 6. LED_BUILTIN is set to
  the correct LED pin independent of which board is used.
  If you want to know what pin the on-board LED is connected to on your Arduino
  model, check the Technical Specs of your board at:
  https://docs.arduino.cc/hardware/

  modified 8 May 2014
  by Scott Fitzgerald
  modified 2 Sep 2016
  by Arturo Guadalupi
  modified 8 Sep 2016
  by Colby Newman

  This example code is in the public domain.

  https://docs.arduino.cc/built-in-examples/basics/Blink/
*/

// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_BUILTIN, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(LED_BUILTIN, HIGH);  // change state of the LED by setting the pin to the HIGH voltage level
  delay(1000);                      // wait for a second
  digitalWrite(LED_BUILTIN, LOW);   // change state of the LED by setting the pin to the LOW voltage level
  delay(1000);                      // wait for a second
}

板载WS2812控制程序

数据手册:WS2812 -PDF数据手册-参考资料-立创商城

image-20260519115248570

image-20260519115315284

WS2812 控制原理

WS2812 是单线、串行、级联的 RGB LED,用纳秒级电平宽度表示 0/1,不需要时钟线。

1)数据编码(一个 bit)

  • 逻辑 0:高~350ns + 低~800ns
  • 逻辑 1:高~700ns + 低~600ns
  • 复位 RESET:低电平 > 280µs(表示一帧结束)

2)一帧数据格式

每个灯珠 24bit = G8 + R8 + B8GRB 顺序,不是 RGB)。

多颗灯珠时:先发第 1 颗 → 第 2 颗 → … → 最后复位。

3)为什么 ESP32 能驱动

ESP32 有 RMT 外设,硬件自动生成精准纳秒波形,不占用 CPU,时序稳定。

注意:WS2812 供电是5V,必须和ESP32共地,使用GPIO48控制

image-20260519120017323

Arduino 示例

1)安装库

Arduino IDE → 项目 → 加载库 → 管理库 → 搜索 FastLED 安装。

2)完整代码

#include <FastLED.h>

// ---------- 配置 ----------
#define LED_PIN     48       // 数据脚
#define NUM_LEDS    1        // 灯珠数量
#define BRIGHTNESS  128      // 亮度 0-255
#define LED_TYPE    WS2812
#define COLOR_ORDER GRB      // WS2812 是 GRB

CRGB leds[NUM_LEDS];

void setup() {
  FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS);
  FastLED.setBrightness(BRIGHTNESS);
  FastLED.clear();
  FastLED.show();
}

void loop() {
  // 1. 全红 1秒
  fill_solid(leds, NUM_LEDS, CRGB::Red);
  FastLED.show();
  delay(1000);

  // 2. 全绿 1秒
  fill_solid(leds, NUM_LEDS, CRGB::Green);
  FastLED.show();
  delay(1000);

  // 3. 全蓝 1秒
  fill_solid(leds, NUM_LEDS, CRGB::Blue);
  FastLED.show();
  delay(1000);

  // 彩虹流水
  static uint8_t hue = 0;
  for (int i = 0; i < NUM_LEDS; i++) {
     leds[i] = CHSV(hue + i*30, 255, 255);
  }
  hue += 5;
  FastLED.show();
  delay(500);
}

3)关键 API 说明

  • FastLED.addLeds(...):初始化引脚、数量、类型
  • fill_solid():统一设为同一颜色
  • leds[i] = CRGB(R,G,B):单独设置第 i 颗颜色
  • FastLED.show()把数据发出去,生效
    • CRGB (红,绿,蓝) → 直接给红绿蓝值
    • CHSV (色相,饱和度,亮度) → 用彩虹色环生成颜色(0~255 自动红→橙→黄→绿→青→蓝→紫)

关于 FastLED.h介绍:

FastLED : 一个专门控制【可编程 RGB 灯珠 / 灯条】的强大、通用、稳定的 Arduino 库

它支持的灯条包括:

  • WS2812 / WS2812B / WS2813(最常见)
  • WS2811
  • SK6812(RGBW、RGBWW)
  • APA102(时钟 + 数据双线)
  • TM1814、LPD8806、NEOPIXEL、UCS1903 等几十种
// 调用方式
leds[0] = CRGB::Red;
FastLED.show();
posted @ 2026-05-19 12:21  Q&25  阅读(36)  评论(0)    收藏  举报