# LED Strip Driver
[![Component Registry](https://components.espressif.com/components/espressif/led_strip/badge.svg)](https://components.espressif.com/components/espressif/led_strip)
This driver is designed for addressable LEDs like [WS2812](http://www.world-semi.com/Certifications/WS2812B.html), where each LED is controlled by a single data line.
## Backend Controllers
### The [RMT](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/peripherals/rmt.html) Peripheral
This is the most economical way to drive the LEDs because it only consumes one RMT channel, leaving other channels free to use. However, the memory usage increases dramatically with the number of LEDs. If the RMT hardware can't be assist by DMA, the driver will going into interrupt very frequently, thus result in a high CPU usage. What's worse, if the RMT interrupt is delayed or not serviced in time (e.g. if Wi-Fi interrupt happens on the same CPU core), the RMT transaction will be corrupted and the LEDs will display incorrect colors. If you want to use RMT to drive a large number of LEDs, you'd better to enable the DMA feature if possible [^1].
#### Allocate LED Strip Object with RMT Backend
```c
#define BLINK_GPIO 0
led_strip_handle_t led_strip;
/* LED strip initialization with the GPIO and pixels number*/
led_strip_config_t strip_config = {
.strip_gpio_num = BLINK_GPIO, // The GPIO that connected to the LED strip's data line
.max_leds = 1, // The number of LEDs in the strip,
.led_pixel_format = LED_PIXEL_FORMAT_GRB, // Pixel format of your LED strip
.led_model = LED_MODEL_WS2812, // LED strip model
.flags.invert_out = false, // whether to invert the output signal (useful when your hardware has a level inverter)
};
led_strip_rmt_config_t rmt_config = {
#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 0, 0)
.rmt_channel = 0,
#else
.clk_src = RMT_CLK_SRC_DEFAULT, // different clock source can lead to different power consumption
.resolution_hz = 10 * 1000 * 1000, // 10MHz
.flags.with_dma = false, // whether to enable the DMA feature
#endif
};
ESP_ERROR_CHECK(led_strip_new_rmt_device(&strip_config, &rmt_config, &led_strip));
```
You can create multiple LED strip objects with different GPIOs and pixel numbers. The backend driver will automatically allocate the RMT channel for you if there is more available.
### The [SPI](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/peripherals/spi_master.html) Peripheral
SPI peripheral can also be used to generate the timing required by the LED strip. However this backend is not as economical as the RMT one, because it will take up the whole **bus**, unlike the RMT just takes one **channel**. You **CANT** connect other devices to the same SPI bus if it's been used by the led_strip, because the led_strip doesn't have the concept of "Chip Select".
Please note, the SPI backend has a dependency of **ESP-IDF >= 5.1**
#### Allocate LED Strip Object with SPI Backend
```c
#define BLINK_GPIO 0
led_strip_handle_t led_strip;
/* LED strip initialization with the GPIO and pixels number*/
led_strip_config_t strip_config = {
.strip_gpio_num = BLINK_GPIO, // The GPIO that connected to the LED strip's data line
.max_leds = 1, // The number of LEDs in the strip,
.led_pixel_format = LED_PIXEL_FORMAT_GRB, // Pixel format of your LED strip
.led_model = LED_MODEL_WS2812, // LED strip model
.flags.invert_out = false, // whether to invert the output signal (useful when your hardware has a level inverter)
};
led_strip_spi_config_t spi_config = {
.clk_src = SPI_CLK_SRC_DEFAULT, // different clock source can lead to different power consumption
.flags.with_dma = true, // Using DMA can improve performance and help drive more LEDs
.spi_bus = SPI2_HOST, // SPI bus ID
};
ESP_ERROR_CHECK(led_strip_new_spi_device(&strip_config, &spi_config, &led_strip));
```
The number of LED strip objects can be created depends on how many free SPI buses are free to use in your project.
## FAQ
* Which led_strip backend should I choose?
* It depends on your application requirement and target chip's ability.
```mermaid
flowchart LR
A{Is RMT supported?}
A --> |No| B[SPI backend]
B --> C{Does the led strip has \n a larger number of LEDs?}
C --> |No| D[Don't have to enable the DMA of the backend]
C --> |Yes| E[Enable the DMA of the backend]
A --> |Yes| F{Does the led strip has \n a larger number of LEDs?}
F --> |Yes| G{Does RMT support DMA?}
G --> |Yes| E
G --> |No| B
F --> |No| H[RMT backend] --> D
```
* How to set the brightness of the LED strip?
* You can tune the brightness by scaling the value of each R-G-B element with a **same** factor. But pay attention to the overflow of the value.
[^1]: The RMT DMA feature is not available on all ESP chips. Please check the data sheet before using it.
weixin_55487763
- 粉丝: 44
- 资源: 6
最新资源
- SMPTE标准下Quad-Link 3Gb/s串行接口的影像格式与辅助数据映射规范及应用场景解析
- Excel文件合并工具64位
- 毕业设计基于springboot流浪动物救助平台的设计与实现源码+论文PDF
- CMake-3.28.3安装包
- 课程大作业-医疗数据分析平台基于机器学习C++实现+源码+数据+报告及完整资料.zip
- cmake-3.26.5-windows-x86-64
- 污水处理 有wincc7.0项目带西门子300程序带注释,有 工艺流程图,报表,wincc里的所有画面脚本都带中文注释,是一套完整的项目
- 基于SpringBoot+Vue的流浪动物救助平台设计与实现源码+数据库+论文PDF(高分毕设)
- Socket tcp服务器端通信源码,C#编写,服务器端部分,这是从一个商业级的物联网项目分离出来的核心代码,让你绕过最难写的Socket管理,内置两个Socket,有两个数据缓冲队列,,仅一个静态类
- cmake-3.26.4-windows-x86-64
- 昆仑通态(MCGS)样板程序,包含历史实时报表查询导出、画面动画风扇转动、历史实时报警查询导出、U盘导出、各种通信样板程序、工程上应用样板程序(污水厂、泵站、增压站、锅炉、产线、搅拌站)、各种触摸屏独
- cmake-3.30.3-windows-x86-64
- 西门子S7-200SMART型PLC使用PROFINET通讯控制G120变频器程序,带相关手册和固件升级包西门子1200PLC通过PROFINET通讯协议控制G120变频器例子程序,可以实现频率设定和
- cmake-3.29.4-windows-x86-64.msi
- 西门子S7-200PLC与MM420变频器数字量模拟量USS 通讯控制西门子224XP型PLC和MCGS触摸屏控制MM420变频器(三种控制方式); 只卖程序,不卖硬件,百度网盘链接发送; 主要功能:
- PCS储能逆变并网模型,包括: (1)逆变侧采用背靠背三电平设计,SVPWM控制算法,中点平衡算法,马鞍波 LC滤波 (2)DCDC采用BUCK BOOST电路 控制方法采用双闭环控制,电压环电