RGB LED

  1. 示例代码

本示例会使用ESP32_WS2812_Lib库来实现板载RGB LED控制。

搜索并下载ESP32_WS2812_Lib库,安装到arduino IDE中,如下图。

../../../../_images/esp32_ws2812.png

注意

掌控板板载RGB LED数量:3,控制IO为P7.

示例-RainBow.ino
 1/**
 2 * Brief	A library for controlling ws2812 in esp32 platform.
 3 * Author	ZhentaoLin
 4 * Url    https://github.com/Zhentao-Lin/ESP32_WS2812_Lib
 5 * Date		2024-09-02
 6 */
 7#include "ESP32_WS2812_Lib.h"
 8
 9#define LEDS_COUNT 3
10#define LEDS_PIN   P7
11#define CHANNEL    0
12
13ESP32_WS2812 strip = ESP32_WS2812(LEDS_COUNT, LEDS_PIN, CHANNEL, TYPE_GRB);
14
15void setup() {
16  strip.begin();
17  strip.setBrightness(20);
18}
19
20void loop() {
21  for (int j = 0; j < 255; j += 2) {
22    for (int i = 0; i < strip.getLedCount(); i++) {
23      strip.setLedColorData(i, strip.Wheel((i * 256 / strip.getLedCount() + j) & 255));
24    }
25    strip.show();
26    delay(10);
27  }
28}

  1. 运行效果

代码上传后,板载3个灯显示幻彩灯效。