A production-ready template for controlling WS2812 RGB LEDs on ESP32-C6 using ESP-IDF with non-blocking operation and robust error handling.
- π Non-blocking LED control - Won't freeze your application
- π‘οΈ Production-ready - Comprehensive error handling and resource management
- βοΈ Easily configurable - Simple defines for hardware customization
- π Scalable design - Supports single LEDs or LED strips
- π Well documented - Doxygen-style comments and examples
- π§ Template ready - Copy-paste into your projects
- PlatformIO installed
- ESP32-C6 board (tested on Waveshare ESP32-C6-LCD-1.47)
- VS Code with PlatformIO extension (recommended)
git clone https://github.com/yourusername/esp32-c6-ws2812-template.git
cd esp32-c6-ws2812-template
pio run --target upload --target monitorI (123) WS2812_RGB: ESP32-C6 WS2812 RGB LED Demo Starting...
I (124) WS2812_RGB: Board: Waveshare ESP32-C6-LCD-1.47
I (125) WS2812_RGB: LED GPIO: 8
I (126) WS2812_RGB: WS2812 driver initialized successfully on GPIO8
I (127) WS2812_RGB: Starting color cycling demo...
I (128) WS2812_RGB: Setting color: Red (R:255 G:0 B:0)
I (1129) WS2812_RGB: Setting color: Green (R:0 G:255 B:0)
I (2130) WS2812_RGB: Setting color: Blue (R:0 G:0 B:255)
The template cycles through these colors every second:
| Color | RGB Values | Hex |
|---|---|---|
| π΄ Red | (255, 0, 0) | #FF0000 |
| π’ Green | (0, 255, 0) | #00FF00 |
| π΅ Blue | (0, 0, 255) | #0000FF |
| π‘ Yellow | (255, 255, 0) | #FFFF00 |
| π£ Purple | (255, 0, 255) | #FF00FF |
| π΅ Cyan | (0, 255, 255) | #00FFFF |
| βͺ White | (255, 255, 255) | #FFFFFF |
| β« Off | (0, 0, 0) | #000000 |
#define WS2812_GPIO_PIN GPIO_NUM_8 // Onboard LED pin
#define WS2812_LED_COUNT 1 // Single LED// For external LED strip on GPIO18
#define WS2812_GPIO_PIN GPIO_NUM_18
#define WS2812_LED_COUNT 8 // 8-LED strip
// Color order configuration (choose the correct one for your LED)
#define WS2812_COLOR_ORDER_RGB // Use if Red/Green are swapped
// #define WS2812_COLOR_ORDER_GRB // Standard WS2812B (default)#include "main.h" // Include the template
void my_application() {
// Initialize driver
esp_err_t ret = ws2812_init();
if (ret != ESP_OK) {
ESP_LOGE(TAG, "Init failed: %s", esp_err_to_name(ret));
return;
}
// Set colors (non-blocking)
ws2812_set_color(255, 0, 0); // Red
do_other_work(); // Your code runs immediately
ws2812_set_color(0, 255, 0); // Green
do_more_work(); // Non-blocking operation
// Wait for transmission if needed
ws2812_wait_done(100); // 100ms timeout
// Cleanup when done
ws2812_deinit();
}// Modify configuration for LED strip
#define WS2812_LED_COUNT 8
// In your code - each LED needs separate control
for (int i = 0; i < WS2812_LED_COUNT; i++) {
ws2812_set_pixel(i, 255, 0, 0); // Set pixel i to red
}
ws2812_refresh(); // Update all LEDsesp32-c6-ws2812-template/
βββ π src/
β βββ π main.cpp # Main application code
βββ π docs/
β βββ π project-documentation.md # Detailed technical docs
βββ π platformio.ini # PlatformIO configuration
βββ π README.md # This file
βββ π LICENSE # MIT License
| Function | Description | Returns |
|---|---|---|
ws2812_init() |
Initialize WS2812 driver | esp_err_t |
ws2812_set_color(r, g, b) |
Set LED color (non-blocking) | esp_err_t |
ws2812_wait_done(timeout) |
Wait for transmission | esp_err_t |
ws2812_deinit() |
Cleanup and free resources | void |
| Define | Default | Description |
|---|---|---|
WS2812_GPIO_PIN |
GPIO_NUM_8 |
LED data pin |
WS2812_LED_COUNT |
1 |
Number of LEDs |
WS2812_COLOR_ORDER_RGB |
defined |
Use RGB color order |
WS2812_COLOR_ORDER_GRB |
undefined |
Use GRB color order (standard) |
RMT_RESOLUTION_HZ |
10000000 |
RMT timing precision |
COLOR_CHANGE_INTERVAL_MS |
1000 |
Demo color interval |
- Data Format: GRB (Green-Red-Blue) 24-bit per LED
- Timing: 800kHz data rate with precise pulse widths
- Reset: 50ΞΌs+ low pulse between frames
- Hardware-accelerated timing generation
- Non-blocking operation with DMA
- Precise timing down to 100ns resolution
- β‘ Transmission time: ~24ΞΌs per LED
- π§ CPU overhead: Minimal (hardware-accelerated)
- πΎ Memory usage: 3 bytes per LED + driver overhead
# Build only
pio run
# Build and flash
pio run --target upload
# Flash and monitor
pio run --target upload --target monitor
# Clean build
pio run --target cleanEnable verbose logging:
esp_log_level_set("WS2812_RGB", ESP_LOG_VERBOSE);- Copy the driver functions to your project
- Modify configuration defines for your hardware
- Include in your main application
- Initialize once, use everywhere
- β Check GPIO pin number in configuration
- β Verify power supply (3.3V recommended)
- β
Ensure
ws2812_init()returnsESP_OK
- β
Red/Green swapped? Use
#define WS2812_COLOR_ORDER_RGB - β
Colors still wrong? Try
#define WS2812_COLOR_ORDER_GRB - β Try lower brightness values (50 instead of 255)
- β Check if LED is WS2812B variant vs clone
- β Update to ESP-IDF 5.0+
- β
Check board setting in
platformio.ini - β Verify all includes are available
- Project Documentation - Detailed technical guide
- ESP-IDF Programming Guide - ESP32 development
- WS2812 Datasheet - LED specifications
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- Espressif for the ESP32-C6 and ESP-IDF framework
- Waveshare for the ESP32-C6-LCD-1.47 development board
- WorldSemi for the WS2812 LED design
- PlatformIO for the excellent development environment
- π Issues: GitHub Issues
- π¬ Discussions: GitHub Discussions
- π§ Email: your.email@example.com
β Star this repository if you found it helpful! β
Made with β€οΈ for the ESP32 community