Sensors and I/O

RMT LED strip

WS2812-class strips. RMT for timing; EMI for certification.

Sensors and I/O LED strip ESP32 family

Official path: examples/peripherals/rmt/led_strip

01 Overview

The RMT LED strip example demonstrates how to drive WS2812-class single-wire protocol strips using the ESP32's RMT peripheral. RMT generates precise timing waveforms in hardware, freeing the CPU, making it ideal for large numbers of LEDs.

LED strips are commonly used for status rings, accent lighting, decorative lights, and any application requiring multi-color, programmable, dynamic effects.

This example is not for simple single-color GPIO lamps; use Blink or LEDC for those. It is also not for high refresh rates or complex protocols like SPI strips.

WS2812 uses a single-wire return-to-zero protocol with 24-bit GRB data per LED, requiring strict timing. RMT handles this easily.

Power and ground design are critical when driving strips; poor design leads to flicker, color errors, and other issues.

During RF certification, the strip itself can become an EMI source, so attention to suppression is necessary.

02 Hardware

  • Strip power: Strips often need 5V; verify if LEDs are 3.3V tolerant, otherwise level shift data line.
  • Level shifting: If LEDs are not 3.3V tolerant, add a level shifter (e.g., 74HCT245) or use 3.3V-tolerant LEDs.
  • Decoupling: Add 100-1000µF electrolytic capacitor at strip power input and 0.1µF ceramic near the strip.
  • Grounding: Return current and ground plane decide flicker; ensure adequate trace width and low impedance.
  • Connectors: Common JST or dupont; check current rating.
  • EMI: Strips are interference sources in RF tests; may need ferrite beads or shielding.
  • Layout: Keep data lines short and away from power lines to avoid crosstalk.

03 Software flow

1. Configure RMT channel and GPIO, set clock source and divider.

2. Initialize strip driver, allocate GRB buffer (size = number of LEDs × 3).

3. Fill buffer with color values for each LED.

4. Call refresh function; RMT sends waveform automatically.

5. Avoid recomputing colors in ISR; update buffer first, then trigger refresh.

6. For dynamic effects, update colors in main loop and refresh.

7. Error handling: check RMT transmission completion, retry if needed.

04 Core points

  • Power, not code, usually causes sparkle. Insufficient voltage or poor grounding leads to color errors or flicker.
  • Timing margin: Different LEDs have slight timing variations; configure RMT with margin.
  • Don't copy across chips: RMT peripheral differs slightly across ESP32 series; refer to respective TRM.
  • Custom work: effects, level shift, layout, power design.
  • Thermal: High current over long periods may heat up; consider heat dissipation.

中文

传感器与外设

RMT 灯带

WS2812 类灯带。时序走 RMT,认证时要注意骚扰。

传感器与外设 灯带 ESP32 family

官方路径: examples/peripherals/rmt/led_strip

01 项目概述

RMT 灯带示例展示如何使用 ESP32 的 RMT 外设驱动 WS2812 等单线协议灯带。RMT 硬件生成精确的时序波形,无需 CPU 干预,适合大量灯珠的场景。

灯带常用于状态指示、氛围照明、装饰灯等,需要多色、可编程、动态效果的应用。

本示例不适用于普通单色 GPIO 灯,那应使用 Blink 或 LEDC 实现。也不适用于需要高刷新率或复杂协议(如 SPI 灯带)的场景。

WS2812 灯带的数据协议是单线归零码,每个灯珠 24 位 GRB 数据,时序要求严格。RMT 可以轻松满足。

驱动灯带时,电源和地线设计至关重要,否则会出现闪烁、颜色错误等问题。

在射频认证中,灯带本身可能成为干扰源,需要关注 EMI 抑制。

02 项目硬件描述

  • 灯带供电: 常见 5V 供电,需确认灯珠是否支持 3.3V 逻辑,否则数据线需电平转换。
  • 电平转换: 若灯珠不兼容 3.3V,需加电平转换芯片(如 74HCT245)或使用 3.3V 容忍的灯珠。
  • 电源去耦: 在灯带电源输入端加 100-1000µF 电解电容,靠近灯带处加 0.1µF 陶瓷电容。
  • 地线设计: 大电流回路和地线决定闪不闪,确保地线足够粗,避免共地阻抗。
  • 连接器: 常见 JST 或杜邦线,注意电流容量。
  • EMI: 灯带是射频认证中的干扰源,可能需加磁珠或屏蔽。
  • 布局: 数据线尽量短,远离电源线,避免串扰。

03 项目软件流程描述

1. 配置 RMT 通道和 GPIO,设置时钟源和分频。

2. 初始化灯带驱动,分配 GRB 缓冲区,大小等于灯珠数×3。

3. 填充缓冲区,设置每个灯珠的颜色。

4. 调用刷新函数,RMT 自动发送波形。

5. 避免在中断中重计算颜色,可先更新缓冲区,再触发刷新。

6. 如需动态效果,可在主循环中更新颜色并刷新。

7. 错误处理:检查 RMT 发送是否完成,必要时重试。

04 项目核心点

  • 电源比代码更容易让灯带花屏。 电压不足或地线不良会导致颜色错误或闪烁。
  • 时序裕量: 不同灯珠对时序要求略有差异,RMT 配置需留有余量。
  • 不要跨芯片复制: 不同 ESP32 系列 RMT 外设略有不同,需参考对应 TRM。
  • 可定制: 灯效、电平转换、布局、电源设计。
  • 注意散热: 大电流长时间工作可能发热,需考虑散热。