- ESP32-S3-Matrix: https://www.waveshare.com/esp32-s3-matrix.htm
- Arduino IDE: https://www.arduino.cc/
FastLED library is a robust and user-friendly Arduino third-party library designed for controlling LED light strips such as WS2812 and LPD8806. It has become a favorite among Arduino developers for its extensive capabilities and is widely utilized in various commercial products.
Utilizing FastLED library simplifies the process of programming LED displays. You can easily specify the number of LEDs and their data pins, initialize them using the "FastLED.addLeds" function, and then set the color of each LED with "leds[index] = CRGB::Color". The "FastLED.show()" function then updates the LED display.
Beyond basic functionality, FastLED offers advanced features like full HSV color support, adjustable master brightness, and optimized math and memory operations. The library also benefits from an active community of users who provide support and share innovative ideas.
This tutorial will use Waveshare ESP32-S3 Matrix as a case study to demonstrate FastLED library's capabilities. We will guide you through how to harness the power of FastLED to create stunning RGB light effects.
Enhanced by FastLED library, ESP32-S3 Matrix can reach its full potential, producing impressive LED display effects. Ideal for educational purposes, artistic presentations, or commercial applications, the ESP32-S3 Matrix offers powerful performance and adaptability to satisfy a wide range of user requirements. With the comprehensive resources and technical support from Waveshare, users can concentrate on creativity and innovation without being burdened by the intricacies of the hardware.
Installing the LibraryStep 1: Install Arduino IDE and open it after installation.
Step 2: Go to Board Manager and search for "esp32" to install it.
Step 3: Go to Library Manager and search for "FastLED" library to install it.
Step 4: After installation, restart Arduino IDE to complete the installation.
Flashing the Program1. Create a new project in Arduino.
2. Below, I will provide the code for two effects. Simply paste the code into Arduino IDE:
BlinkRGB:
#include <FastLED.h>
#define LED_PIN 14
#define NUM_LEDS 64
#define BRIGHTNESS 10
#define LED_TYPE WS2812
#define COLOR_ORDER GRB
CRGB leds[NUM_LEDS];
const uint16_t bands = 8; // Number of frequency bands
uint8_t bar_values[bands]; // Store the height of each band
CRGB colors[bands]; // Store the color of each band
void setup() {
FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS);
FastLED.setBrightness(BRIGHTNESS);
}
void loop() {
static uint8_t lastBarValues[bands]; // Store the last height of the bands
fill_solid(leds, NUM_LEDS, CRGB::Black); // Turn off all LEDs first
// Generate new band heights and colors
for (int i = 0; i < bands; i++) {
bar_values[i] = random(0, 9); // Generate a random value between 0 and 8
colors[i] = CHSV(random8(), 255, 255); // Generate a random color
}
// Draw each band
for (int i = 0; i < bands; i++) {
for (int j = 0; j < bar_values[i]; j++) {
leds[i * 8 + j] = colors[i]; // Light up each bar of the band
}
}
// Draw the descending effect
for (int i = 0; i < bands; i++) {
if (bar_values[i] < lastBarValues[i]) {
for (int j = bar_values[i]; j < lastBarValues[i]; j++) {
leds[i * 8 + j] = CRGB::Black; // Turn off each bar of the band
}
lastBarValues[i] = bar_values[i]; // Update the last band height
} else {
lastBarValues[i] = bar_values[i]; // If there is no change, update directly
}
}
FastLED.show(); // Update the LED display
delay(50); // Delay for 50 milliseconds
// Update the last band height
for (int i = 0; i < bands; i++) {
lastBarValues[i] = bar_values[i];
}
}
Effect as shown in the video:
LED Spectrum:
#include <FastLED.h>
#define LED_PIN 14
#define NUM_LEDS 64
#define BRIGHTNESS 100
#define LED_TYPE WS2812
#define COLOR_ORDER GRB
CRGB leds[NUM_LEDS];
const uint8_t matrixWidth = 8;
const uint8_t matrixHeight = 8;
int radius = matrixWidth / 2; // Initial radius set to maximum value
bool expanding = false; // Initial state is contracting
CRGB historyColor[matrixHeight][matrixWidth];
void setup() {
FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS);
FastLED.setBrightness(BRIGHTNESS);
}
void loop() {
fill_solid(leds, NUM_LEDS, CRGB::Black);
// Update radius based on expanding state
if (!expanding) {
radius--;
if (radius < 1) { // When contracted to 1, prepare to expand
radius = 1;
expanding = true; // Switch to expanding state
}
} else {
radius++;
if (radius > matrixWidth / 2) {
radius = matrixWidth / 2;
expanding = false; // Switch to contracting state
}
}
uint8_t hue = 0;
for (int i = 0; i < matrixHeight; i++) {
for (int j = 0; j < matrixWidth; j++) {
int dx = abs(j - matrixWidth / 2);
int dy = abs(i - matrixHeight / 2);
int dist = max(dx, dy); // Manhattan distance
// Light up LEDs based on distance and expanding state
if ((expanding && dist == radius) || (!expanding && dist <= radius)) {
hue = (millis() / 10 + i * matrixWidth + j) % 360; // Color changes over time
CRGB color = CHSV(hue, 255, BRIGHTNESS);
if (expanding) {
leds[matrixWidth * i + j] = color;
historyColor[i][j] = color;
} else {
leds[matrixWidth * i + j] = historyColor[i][j];
}
}
}
}
FastLED.show();
delay(100);
}
Effect as shown in the video:
3. Choose appropriate development board and port, as well as upload method, according to the following image:
4. Next click the Arrow button to upload, Arduino IDE will compile and burn the program.
5. Once the upload is complete, you can see the light effects on the development board.
ConclusionFastLED library truly revolutionizes the way we interact with LED light strips, making the process of creating vibrant and dynamic displays more accessible than ever. Its robust feature set, combined with the powerful ESP32-S3 Matrix from Waveshare, opens up a world of possibilities for both hobbyists and professionals alike.
As you embark on your journey with FastLED, you'll discover a community of enthusiasts and experts who are eager to share their knowledge and passion. Whether you're new to the world of LED programming or a seasoned developer looking to push the boundaries of what's possible, FastLED offers the tools and support you need to bring your visions to life.
So, go ahead and dive into the exciting realm of LED animation. Explore the endless creative potential that FastLED offers, and enjoy the joy of transforming your ideas into dazzling light shows. With FastLED at your fingertips, the only limit is your imagination. Happy creating!
More ResourcesFor an in-depth look at Waveshare's offerings, consider the following resources:
● Waveshare Website: Visit the official Waveshare website to explore their extensive range of electronic modules, including displays, sensors, and more. https://www.waveshare.com/wiki/ESP32-S3-Matrix#Working_with_Arduino
Comments
Please log in or sign up to comment.