yahboom
HomeLearn
Solution
Blog
Online Store
About Us
EN/中文
ESP32 Core Board📖
1. ESP32 Introduction
  • 1. ESP32S3 Overview
  • 2. Environment Setup Introduction
  • 3. Dev Board Introduction
2. ESP-IDF Development Tutorial
1. Before Development
  • 1. Environment Setup (Windows)
  • 2. Environment Setup (Linux)
  • 3. Build and Flash
2. Basic Peripherals
  • 1. LED Control
  • 2. Button Detection
  • 3. Peripheral Interrupts
  • 4. ESP_TIMER
  • 5. GPTIMER
  • 6. Watchdog Timer
  • 7. DMA
  • 8. ADC
  • 9. PWM Breathing LED
  • 10. RGB LED Effects
3. Serial Communication and Storage System
  • 1. Serial UART Communication Experiment
  • 2. I2C_OLED Display Experiment
  • 3. SPI Bus Communication Experiment
  • 4. PSRAM External Memory Experiment
  • 5. Flash Read and Write Experiment
  • 6. USB Emulated Mass Storage Experiment
4. FreeRTOS operating system
  • 1.FreeRTOS Core Concepts Theory
  • 2.Task Creation and Management
  • 3.Queue
  • 4.Binary Semaphore
  • 5.Mutex
  • 6.Event Groups
  • 7.Software Timer
  • 8.Task Scheduler
  • 9.Low Power Mode
  • 10.Deadlock
  • 11.Priority Inversion
5. Network Protocols and Wireless Communication
  • 1. WIFI Network Basics
  • 2. WIFI STA Mode
  • 3. WIFI AP Mode
  • 4. UDP Client Experiment
  • 5. UDP Server Experiment
  • 6. TCP Client Experiment
  • 7. TCP Server Experiment
  • 8. Socket Programming Basics
  • 9. HTTP Client Experiment
  • 10. HTTP Server Experiment
  • 11. MQTT IoT Communication Protocol
  • 12. WIFI OTA Upgrade
  • 13. Bluetooth Fundamentals
  • 14. Bluetooth Slave Mode
  • 15. Bluetooth Master Mode
6. Audio Applications
  • 1.I2S Microphone Recording and U-Disk Playback
  • 2.I2S Audio Playback Experiment
  • 3.Repeater Experiment
7. Xiaozhi AI Voice
  • 1. Burn Xiaozhi Firmware
  • 2. Network and Configuration
  • 3. AI Agent Settings
  • 4. Program Analysis
  • 5. Free Chat
3. Arduino Development Course
1. Before Development
  • 1. Environment Setup
  • 2. Build and Flash
2. Basic Peripherals
  • 1. LED Control
  • 2. Button Detection
  • 3. Peripheral Interrupts
  • 4. ESP_TIMER
  • 5. GPTIMER
  • 6. Watchdog Timer
  • 7. ADC
  • 8. PWM Breathing LED
  • 9. RGB LED Effects
3. Serial Communication and Storage System
  • 1. Serial UART Communication
  • 2. I2C OLED Display
  • 3. SPI Bus Communication
  • 4. Flash Read and Write
  • 5. USB Emulated Mass Storage
4. Network Protocols and Wireless Communication
  • 1. WIFI Network Basics
  • 2. WIFI STA Mode
  • 3. WIFI AP Mode
  • 4. UDP Server Experiment
  • 5. UDP Client Experiment
  • 6. TCP Client Experiment
  • 7. TCP Server Experiment
  • 8. HTTP Client Experiment
  • 9. HTTP Server Experiment
  • 10. MQTT IoT Communication Protocol
  • 11. WIFI OTA Upgrade
  • 12. Bluetooth Fundamentals
  • 13. Bluetooth Slave Mode
  • 14. Bluetooth Master Mode
5. Audio Applications
  • 1.I2S Microphone Recording and U-Disk Playback
  • 2.I2S Audio Playback Experiment
  • 3.Repeater Experiment
4. MicroPython Development Course
1. Before Development
  • 1. Environment Setup
  • 2. Build and Flash
  • 3. Thonny IDE
2. Basic Peripherals
  • 1. LED Control
  • 2. Button Detection
  • 3. Peripheral Interrupts
  • 4. TIMER_IT
  • 5. Watchdog Timer
  • 6. ADC
  • 7. PWM Breathing LED
  • 8. RGB LED Effects
3. Serial Communication and Storage System
  • 1. Serial UART Communication Experiment
  • 2. I2C_OLED Display Experiment
  • 3. SPI Bus Communication Experiment
  • 4. Flash Read and Write Experiment
  • 5. Print Storage
4. Network Protocols and Wireless Communication
  • 1. WIFI Network Basics
  • 2. WIFI STA Mode
  • 3. WIFI AP Mode
  • 4. UDP Client Experiment
  • 5. UDP Server Experiment
  • 6. TCP Client Experiment
  • 7. TCP Server Experiment
  • 8. HTTP Client Experiment
  • 9. HTTP Server Experiment
  • 10. MQTT
  • 11. Bluetooth Fundamentals
  • 12. Bluetooth Slave Mode
  • 13. Bluetooth Master Mode
5. Audio Applications
  • 1. I2S Audio Playback Experiment
  • 2. Repeater Experiment
Download
  • Code
  • Hardware_Info
  • 3D_Model_File
  • Software_Tools
  • VMware
  • VM_File
Download ZIP

Welcome to ESP32 Core Board repository

9. RGB LED Effects

RGB LED Effects


RGB LED Effects1. RGB LED Effects Overview2. Core Concepts2.1 WS2812B Protocol Key Points2.2 Adafruit NeoPixel Library2.3 Linear Interpolation (Lerp)3. Hardware Dependencies4. Project Architecture and Flow5. Source Code5.1 Header Files and Global Variables5.2 setup() — Initialize NeoPixel5.3 loop() — Color Loop5.4 fadeToColor() — Linear Interpolation Fade6. Operation Flow6.1 Build and Flash6.2 Normal Phenomena6.3 Verification Methods7. Common Problems


1. RGB LED Effects Overview

WS2812B is a single-wire cascadable addressable RGB LED that can control color and brightness through a single data line. This experiment drives the on-board WS2812B (GPIO48), smoothly transitioning between 7 rainbow colors in a loop. The Adafruit NeoPixel library encapsulates the underlying RMT timing, and calling setPixelColor + show updates the color.

Typical Application Scenarios:

Application TypeExample
Consumer ElectronicsAmbient lights, gaming peripherals RGB, chassis lighting effects
Industrial IndicatorsDevice status lights (Green=Normal, Red=Fault, Yellow=Standby)
Commercial DisplayLED strips, pixel matrix screens, advertising light boxes
Smart HomePanel backlight, scene lighting, notification reminder lights

2. Core Concepts

2.1 WS2812B Protocol Key Points

  • Single-wire NZR zero-code: Each LED requires 24 bits of data (G→R→B, 8 bits each)

  • Communication Rate: 800kHz (NEO_KHZ800), each bit is approximately 1.25μs

  • Data Encoding: 1 code = High 0.8μs + Low 0.45μs; 0 code = High 0.4μs + Low 0.85μs

  • Cascade Method: Each LED consumes the first 24 bits, and the remaining signal is reshaped and forwarded to the next one

  • Color Order: GRB (Green-Red-Blue), different from common RGB order

ESP32 generates precise pulse timing through the RMT (Remote Control) peripheral without bit-banging to occupy the CPU.

2.2 Adafruit NeoPixel Library

​x
Adafruit_NeoPixel pixels(LED_NUM, PIN, NEO_GRB + NEO_KHZ800);

pixels.setPixelColor(0, pixels.Color(R, G, B));  // Write frame buffer
pixels.show();                                    // Send via RMT serial
pixels.setBrightness(100);                        // Global brightness 0~255

setPixelColor only writes to the RAM buffer, and show() actually sends data to the LED. When batch updating multiple LEDs, call setPixelColor individually first, then call show() only once at the end.

2.3 Linear Interpolation (Lerp)

Smooth transition between two colors, interpolating RGB three channels separately:

xxxxxxxxxx
r = r1 + (r2 - r1) × i / steps
g = g1 + (g2 - g1) × i / steps
b = b1 + (b2 - b1) × i / steps

Where i ranges from 0 to steps, and i/steps is the interpolation ratio (0.0 ~ 1.0)

3. Hardware Dependencies

PinPeripheralConfigurationDescription
GPIO48WS2812BData OutputSingle-wire NZR protocol, 800kHz

Requires installing the Adafruit NeoPixel library: Arduino IDE → Library Manager → Search "Adafruit NeoPixel" → Install.


4. Project Architecture and Flow

xxxxxxxxxx
setup()
  ├─ Serial.begin(115200)       // Serial debug
  ├─ pixels.begin()             // Initialize RMT peripheral
  ├─ pixels.setBrightness(100)  // Global brightness 100/255
  └─ pixels.clear() + show()    // Turn off all LEDs

loop()
  ├─ fadeToColor(current, target, 100 steps, 20ms)  // Fade for 2 seconds
  ├─ current_index advances
  └─ target_index = (target + 1) % 7             // Loop to next color

fadeToColor(from, to, steps, delay)
  └─ for i=0..steps:
        ├─ r = r1 + (r2-r1)*i/steps  // RGB three channel interpolation
        ├─ setPixelColor(0, r, g, b)  // Write frame buffer
        ├─ show()                     // Send data
        └─ delay(20ms)

5. Source Code

Complete source code: Source Code → Arduino → 1.Basic Peripherals → 9.RGB → 9.RGB.ino

5.1 Header Files and Global Variables

When constructing the Adafruit_NeoPixel object, specify the LED count, pin, and color format. Predefine 7 rainbow colors for loop switching.

xxxxxxxxxx
#include <Adafruit_NeoPixel.h>

#define BLINK_GPIO  48
#define LED_NUM     1
#define FADE_STEPS  100
#define FADE_DELAY  20

Adafruit_NeoPixel pixels(LED_NUM, BLINK_GPIO, NEO_GRB + NEO_KHZ800);

const uint32_t colors[] = {
  pixels.Color(255, 0, 0),        // Red
  pixels.Color(255, 128, 0),      // Orange
  pixels.Color(255, 255, 0),      // Yellow
  pixels.Color(0, 255, 0),        // Green
  pixels.Color(0, 255, 255),      // Cyan
  pixels.Color(0, 0, 255),        // Blue
  pixels.Color(128, 0, 255),      // Purple
};
#define COLOR_COUNT (sizeof(colors) / sizeof(uint32_t))

int current_index = 0;
int target_index  = 1;

COLOR_COUNT calculates the array length at compile time. Adding colors only requires modifying the colors[] array, and the loop automatically adapts.

5.2 setup() — Initialize NeoPixel

setBrightness(100) proportionally dims all colors to approximately 39%, protecting eyes and reducing power consumption.

xxxxxxxxxx
void setup() {
  Serial.begin(115200);
  pixels.begin();
  pixels.setBrightness(100);
  pixels.clear();
  pixels.show();
}

clear() writes all zeros to the frame buffer, and show() sends the all-zero data out, ensuring the LED is off at power-on.

5.3 loop() — Color Loop

Each transition to the next color uses fadeToColor for smooth transition. Index modulo arithmetic automatically loops.

xxxxxxxxxx
void loop() {
  Serial.print(" -> ");
  Serial.println(color_names[target_index]);

  fadeToColor(colors[current_index], colors[target_index], FADE_STEPS, FADE_DELAY);

  current_index = target_index;
  target_index = (target_index + 1) % COLOR_COUNT;  // Loop back to 0 at end
}

5.4 fadeToColor() — Linear Interpolation Fade

Extract RGB three channels from the merged uint32_t color, interpolate each independently, then merge and write back to the frame buffer.

xxxxxxxxxx
void fadeToColor(uint32_t from, uint32_t to, int steps, int stepDelay) {
  uint8_t r1 = (from >> 16) & 0xFF;  // Extract R component
  uint8_t g1 = (from >> 8)  & 0xFF;  // Extract G component
  uint8_t b1 = from         & 0xFF;  // Extract B component

  uint8_t r2 = (to >> 16) & 0xFF;
  uint8_t g2 = (to >> 8)  & 0xFF;
  uint8_t b2 = to         & 0xFF;

  for (int i = 0; i <= steps; i++) {
    uint8_t r = r1 + (r2 - r1) * i / steps;   // Linear interpolate R
    uint8_t g = g1 + (g2 - g1) * i / steps;   // Linear interpolate G
    uint8_t b = b1 + (b2 - b1) * i / steps;   // Linear interpolate B

    pixels.setPixelColor(0, pixels.Color(r, g, b));
    pixels.show();
    delay(stepDelay);
  }
}

(r2 - r1) * i multiplies first then divides to avoid integer division truncating to zero early. When extracting colors, >>16 & 0xFF corresponds to the high byte (G) of the GRB byte order, not the standard RGB order.


6. Operation Flow

6.1 Build and Flash

Arduino Flash Process: See Arduino → 1. Before Development → 2. Build and Flash.

Install library: Arduino IDE → Sketch → Include Library → Manage Libraries → Search "Adafruit NeoPixel" → Install

Open Library

Search and Download

6.2 Normal Phenomena

After power-on, the LED smoothly transitions in a loop between the seven rainbow colors, with each transition taking approximately 2 seconds.

Running Print

运行0

运行1

运行2

运行3

6.3 Verification Methods

OperationExpected Result
Power on and observeRed→Orange→Yellow→Green→Cyan→Blue→Purple→Red, looping endlessly
Change FADE_STEPS to 20Transition speeds up, step effect becomes obvious
Change FADE_DELAY to 50Transition slows down, smoother effect

7. Common Problems

PhenomenonCauseSolution
LED does not light upNeoPixel library not installed or GPIO errorInstall Adafruit NeoPixel in Library Manager, confirm GPIO48
Color display incorrect (red turns green)Color order reversedNEO_GRB is WS2812's color order, do not use NEO_RGB
Compilation error: cannot find Adafruit_NeoPixel.hLibrary not installedSearch and install in Library Manager
Fade looks choppysteps too small or stepDelay too largeIncrease FADE_STEPS or decrease FADE_DELAY
PWM Breathing LED
Serial UART Communication

COMPANY

  • About us
  • Team Style
  • Blog

LEARNING & EXPLORATION

  • Tutorials
  • Edge AI hardware
  • Educational robots
  • Embedded intelligent hardware

WHERE TO BUY

  • Yahboom Online Store
  • Amazon
  • Alibaba
  • AliExpress
  • Mercado

TECHNOLOGY SUPPORT

  • Email: support@yahboom.com

BE A DISTRIBUTOR

  • Email: sales@yahboom.com
QR Code

Copyright © 2017-2026 Yahboom. All Rights Reserved.

EN/中文