03 - Lab FastLED Library Control

Video

# Lab: FastLED Library Control ## **Objective** Learn how to install and use the FastLED library in Arduino IDE 2.x to control the TK33 WS2812 RGB LED Bar. This lab covers library installation, basic FastLED concepts, and creating various lighting effects. ## **Required Components** ![](https://cdn.shopify.com/s/files/1/0331/9994/7908/files/Pasted_image_20250722161321.png?v=1753856096) 1. **Lonely Binary UNO R3** - Main Arduino board 2. **TinkerBlock UNO R3 Shield** - Expansion shield that plugs onto the UNO R3 3. **TinkerBlock TK33** - WS2812 RGB LED Bar (5 LEDs) ## **Theory** ### **WS2812 LED Technology** - **Individual Control**: Each LED can be controlled independently - **24-bit Color**: 8-bit red + 8-bit green + 8-bit blue = 16.7 million colors - **Single Wire Protocol**: Uses one data line for communication - **Addressable**: Each LED has a unique address in the chain ### **FastLED Library** - **High Performance**: Optimized for WS2812 and similar LED strips - **Easy to Use**: Simple functions for color control and effects - **Cross Platform**: Works with various LED types and microcontrollers - **Rich Features**: Built-in effects, color palettes, and animation functions ### **Color Representation** - **RGB Values**: 0-255 for each color channel - **Hexadecimal**: 0xRRGGBB format (e.g., 0xFF0000 for red) - **HSV**: Hue (0-255), Saturation (0-255), Value (0-255) ## **Wiring Instructions** ### **TK33 WS2812 RGB LED Bar Pinout** - **GND** → Arduino GND - **VCC** → Arduino 5V - **NC** → No Connection - **DIN** → Arduino Digital Pin 6 (or any digital pin) ### **Connection Diagram** ![](https://cdn.shopify.com/s/files/1/0331/9994/7908/files/Pasted_image_20250723103237.png?v=1753856101) ## **FastLED Library Installation** ### **Method 1: Library Manager (Recommended)** 1. Open **Arduino IDE 2.x** 2. Go to **Tools** → **Manage Libraries...** (or **Sketch** → **Include Library** → **Manage Libraries...**) 3. In the search box, type **"FastLED"** 4. Find **"FastLED by Daniel Garcia"** 5. Click **"Install"** button 6. Wait for installation to complete 7. Click **"Close"** ### **Method 2: Manual Installation** 1. Download FastLED library from: https://github.com/FastLED/FastLED/releases 2. Extract the ZIP file 3. Copy the **FastLED** folder to your Arduino libraries directory: - **Windows**: `Documents\Arduino\libraries\` - **macOS**: `~/Documents/Arduino/libraries/` - **Linux**: `~/Arduino/libraries/` 4. Restart Arduino IDE ### **Verification** 1. Open Arduino IDE 2. Go to **Sketch** → **Include Library** 3. You should see **"FastLED"** in the list 4. If not, restart Arduino IDE ## **Basic Code** ```cpp #include <FastLED.h> // Configuration #define LED_PIN 5 // Digital pin connected to TK33 DIN #define NUM_LEDS 5 // Number of LEDs in TK33 bar #define BRIGHTNESS 50 // Brightness level (0-255) // LED array CRGB leds[NUM_LEDS]; void setup() { // Initialize FastLED FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS); FastLED.setBrightness(BRIGHTNESS); // Initialize serial communication Serial.begin(9600); Serial.println("TK33 WS2812 RGB LED Bar - FastLED Test"); Serial.println("====================================="); } void loop() { // Test 1: All LEDs Red Serial.println("Test 1: All LEDs Red"); fill_solid(leds, NUM_LEDS, CRGB::Red); FastLED.show(); delay(1000); // Test 2: All LEDs Green Serial.println("Test 2: All LEDs Green"); fill_solid(leds, NUM_LEDS, CRGB::Green); FastLED.show(); delay(1000); // Test 3: All LEDs Blue Serial.println("Test 3: All LEDs Blue"); fill_solid(leds, NUM_LEDS, CRGB::Blue); FastLED.show(); delay(1000); // Test 4: Rainbow effect Serial.println("Test 4: Rainbow Effect"); rainbowEffect(); // Test 5: Individual LED control Serial.println("Test 5: Individual LED Control"); individualLEDTest(); // Test 6: Fade effect Serial.println("Test 6: Fade Effect"); fadeEffect(); } // Rainbow effect function void rainbowEffect() { for (int i = 0; i < 256; i++) { fill_rainbow(leds, NUM_LEDS, i, 256 / NUM_LEDS); FastLED.show(); delay(50); } } // Individual LED control test void individualLEDTest() { // Turn off all LEDs fill_solid(leds, NUM_LEDS, CRGB::Black); FastLED.show(); delay(500); // Light up each LED individually for (int i = 0; i < NUM_LEDS; i++) { Serial.print("Lighting LED "); Serial.println(i); leds[i] = CRGB::White; FastLED.show(); delay(500); leds[i] = CRGB::Black; FastLED.show(); } } // Fade effect function void fadeEffect() { // Fade in for (int brightness = 0; brightness <= 255; brightness += 5) { FastLED.setBrightness(brightness); fill_solid(leds, NUM_LEDS, CRGB::Purple); FastLED.show(); delay(50); } // Fade out for (int brightness = 255; brightness >= 0; brightness -= 5) { FastLED.setBrightness(brightness); fill_solid(leds, NUM_LEDS, CRGB::Purple); FastLED.show(); delay(50); } // Reset brightness FastLED.setBrightness(BRIGHTNESS); } ``` ## **Code Explanation** ### **Library and Configuration** ```cpp #include <FastLED.h> #define LED_PIN 6 // Digital pin for data #define NUM_LEDS 5 // Number of LEDs in bar #define BRIGHTNESS 50 // Global brightness ``` ### **LED Array Declaration** ```cpp CRGB leds[NUM_LEDS]; // Array to store LED colors ``` ### **FastLED Initialization** ```cpp FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS); FastLED.setBrightness(BRIGHTNESS); ``` - **WS2812**: LED type - **LED_PIN**: Digital pin for data - **GRB**: Color order (Green, Red, Blue) - **leds**: LED array - **NUM_LEDS**: Number of LEDs ### **Basic Functions** - **`fill_solid(leds, NUM_LEDS, color)`**: Set all LEDs to same color - **`FastLED.show()`**: Update LED display - **`CRGB::ColorName`**: Predefined colors (Red, Green, Blue, White, etc.) - **`FastLED.setBrightness(value)`**: Set global brightness ## **Advanced Examples** ### **Example 1: Color Cycling** ```cpp void colorCycle() { CRGB colors[] = {CRGB::Red, CRGB::Orange, CRGB::Yellow, CRGB::Green, CRGB::Blue, CRGB::Purple, CRGB::Pink}; for (int i = 0; i < 7; i++) { fill_solid(leds, NUM_LEDS, colors[i]); FastLED.show(); delay(1000); } } ``` ### **Example 2: Breathing Effect** ```cpp void breathingEffect() { for (int brightness = 0; brightness <= 255; brightness += 2) { FastLED.setBrightness(brightness); fill_solid(leds, NUM_LEDS, CRGB::Cyan); FastLED.show(); delay(20); } for (int brightness = 255; brightness >= 0; brightness -= 2) { FastLED.setBrightness(brightness); fill_solid(leds, NUM_LEDS, CRGB::Cyan); FastLED.show(); delay(20); } } ``` ### **Example 3: Knight Rider Effect** ```cpp void knightRider() { // Forward sweep for (int i = 0; i < NUM_LEDS; i++) { fill_solid(leds, NUM_LEDS, CRGB::Black); leds[i] = CRGB::Red; FastLED.show(); delay(100); } // Backward sweep for (int i = NUM_LEDS - 1; i >= 0; i--) { fill_solid(leds, NUM_LEDS, CRGB::Black); leds[i] = CRGB::Red; FastLED.show(); delay(100); } } ``` ## **Troubleshooting** ### **Library not found:** - Verify FastLED is installed in Library Manager - Restart Arduino IDE after installation - Check library folder location ### **LEDs not lighting up:** - Check wiring connections (GND, VCC, DIN) - Verify digital pin number in code - Ensure power supply can handle current (5V, ~60mA per LED) - Check if LED_PIN is correct ### **Wrong colors displayed:** - Try different color order: `GRB`, `RGB`, `BRG` - Some WS2812 variants use different color orders - Test with: `FastLED.addLeds<WS2812, LED_PIN, RGB>(leds, NUM_LEDS);` ### **Flickering or unstable:** - Add capacitor (100-1000μF) between 5V and GND - Use shorter wires between Arduino and TK33 - Ensure stable 5V power supply - Add delay after `FastLED.show()` if needed ### **Only some LEDs work:** - Check data line connection - Verify NUM_LEDS is correct - Test with fewer LEDs first - Check for loose connections ## **Common FastLED Functions** ### **Color Functions** ```cpp CRGB::Red // Predefined colors CRGB(255, 0, 0) // RGB values CHSV(0, 255, 255) // HSV values ``` ### **LED Control** ```cpp leds[0] = CRGB::Red; // Set individual LED fill_solid(leds, 5, CRGB::Blue); // Fill all LEDs FastLED.show(); // Update display ``` ### **Effects** ```cpp fill_rainbow(leds, NUM_LEDS, startHue, deltaHue); fill_gradient(leds, NUM_LEDS, startColor, endColor); fadeToBlackBy(leds, NUM_LEDS, fadeAmount); ``` ### **Brightness Control** ```cpp FastLED.setBrightness(128); // Global brightness (0-255) leds[0].fadeToBlackBy(64); // Individual LED fade ``` ## **Next Steps** - Experiment with different colors and effects - Create custom animations - Combine with sensors for interactive lighting - Explore FastLED's advanced features like palettes and blending ## **Resources** - **FastLED Documentation**: https://fastled.io/ - **FastLED GitHub**: https://github.com/FastLED/FastLED - **Color Picker**: https://www.w3schools.com/colors/colors_picker.asp - **WS2812 Datasheet**: Available online for detailed specifications