06 - Blinking LED

# Your First Blinking LED: WS2812 RGB LED Guide ## Introduction Welcome to your first ESP32-S3 project! 🎉 You're about to make your **Lonely Binary ESP32-S3 N16R8** light up with beautiful colors. This is the "Hello World" of electronics - your first step into the amazing world of microcontroller programming! ![](https://cdn.shopify.com/s/files/1/0331/9994/7908/files/Pasted_image_20250723234257_ab7d8780-584d-4106-bc01-19307e1d54e2.png?v=1755945671) ## What You'll Learn - How to install the FastLED library - How to control the WS2812 RGB LED - How to create different lighting effects - How to upload your first program - How to troubleshoot common issues ## What is a WS2812 LED? ### Regular LED vs WS2812 LED **Regular LED:** - ❌ Only one color (usually red, green, or blue) - ❌ Simple on/off control - ❌ Basic blinking **WS2812 LED (What you have!):** - ✅ **16 million colors!** (Red, Green, Blue combinations) - ✅ **Smart control** (can change colors instantly) - ✅ **Beautiful effects** (rainbow, fade, pulse, etc.) - ✅ **Easy to program** (one wire controls everything) ### Think of it like this: - **Regular LED** = A simple light bulb (on/off) - **WS2812 LED** = A smart TV screen (millions of colors, effects, animations) ## Step 1: Install the FastLED Library ### What is FastLED? **FastLED** is a library that makes it super easy to control WS2812 LEDs. Think of it as a "remote control" for your LED. ### How to Install FastLED 1. **Open Arduino IDE** 2. **Go to Library Manager:** - Click **Sketch** → **Include Library** → **Manage Libraries** - Or press `Ctrl + Shift + I` (Windows) / `Cmd + Shift + I` (Mac) 3. **Search for FastLED:** - In the search box, type "FastLED" - Look for "FastLED by Daniel Garcia" - Click on it 4. **Install FastLED:** - Click the **"Install"** button - Wait for installation to complete - Click **"Close"** ✅ **Great!** FastLED is now installed and ready to use! ![](https://cdn.shopify.com/s/files/1/0331/9994/7908/files/Pasted_image_20250723234328_54df946d-cb93-4a85-ac53-c7825a0b8d78.png?v=1755945675) ## Step 2: Your First Blinking LED Program ### Create a New Sketch 1. **Open Arduino IDE** 2. **Create new sketch:** File → New 3. **Replace the code** with this: ```cpp // Your First WS2812 LED Program // Lonely Binary ESP32-S3 N16R8 #include <FastLED.h> // LED Configuration #define LED_PIN 48 // WS2812 LED is connected to GPIO 48 #define NUM_LEDS 1 // We have 1 LED #define BRIGHTNESS 50 // Brightness (0-255, lower = dimmer) // Create LED array CRGB leds[NUM_LEDS]; void setup() { // Initialize the LED FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS); FastLED.setBrightness(BRIGHTNESS); // Clear all LEDs (turn them off) FastLED.clear(); FastLED.show(); // Start serial communication (for debugging) Serial.begin(115200); Serial.println("WS2812 LED Test - Lonely Binary ESP32-S3"); Serial.println("LED should start blinking in different colors!"); } void loop() { // Red blink Serial.println("Red blink"); leds[0] = CRGB::Red; FastLED.show(); delay(1000); // Turn off leds[0] = CRGB::Black; FastLED.show(); delay(500); // Green blink Serial.println("Green blink"); leds[0] = CRGB::Green; FastLED.show(); delay(1000); // Turn off leds[0] = CRGB::Black; FastLED.show(); delay(500); // Blue blink Serial.println("Blue blink"); leds[0] = CRGB::Blue; FastLED.show(); delay(1000); // Turn off leds[0] = CRGB::Black; FastLED.show(); delay(500); } ``` ### What This Code Does **Setup Section:** - `#include <FastLED.h>` - Includes the FastLED library - `#define LED_PIN 48` - Tells the program the LED is on GPIO 48 - `#define NUM_LEDS 1` - We have 1 LED - `#define BRIGHTNESS 50` - Sets LED brightness (0-255) **Loop Section:** - **Red blink** → **Off** → **Green blink** → **Off** → **Blue blink** → **Off** - Repeats forever! ## Step 3: Upload Your Program ### Check Your Settings 1. **Board Selection:** - Tools → Board → ESP32 Arduino → ESP32S3 Dev Module 2. **Port Selection:** - Tools → Port → Select your ESP32-S3 port - Should show something like: - Windows: `COM3`, `COM4`, etc. - Mac: `/dev/cu.usbserial-XXXXXX` 3. **Upload Speed:** - Tools → Upload Speed → `460800` (or `115200` if you have issues) ### Upload the Code 1. **Click the Upload button** (→ arrow) in Arduino IDE 2. **Wait for upload** to complete 3. **You should see** "Upload complete" message ### What Should Happen After upload, your ESP32-S3 should: - **Blink red** for 1 second - **Turn off** for 0.5 seconds - **Blink green** for 1 second - **Turn off** for 0.5 seconds - **Blink blue** for 1 second - **Turn off** for 0.5 seconds - **Repeat forever!** ## Step 4: Test and Troubleshoot ### If the LED Doesn't Light Up **Check these things:** 1. **Is the LED connected?** - The WS2812 LED is built into your board - It's connected to GPIO 48 - No extra wiring needed! 2. **Is the code uploaded?** - Check Arduino IDE for "Upload complete" message - If upload failed, try again 3. **Is the brightness too low?** - Change `BRIGHTNESS` from 50 to 255 - Upload again 4. **Is the LED pin correct?** - Make sure `LED_PIN` is set to 48 - This is the correct pin for Lonely Binary ESP32-S3 ### If Upload Fails **Try these solutions:** 1. **Hold the BOOT button** while uploading 2. **Try slower upload speed** (115200) 3. **Check your USB cable** (use a data cable, not power-only) 4. **Try a different USB port** on your computer ## Step 5: Make It More Fun! ### Try Different Colors Change the colors in your code: ```cpp // Try these colors: leds[0] = CRGB::Red; // Red leds[0] = CRGB::Green; // Green leds[0] = CRGB::Blue; // Blue leds[0] = CRGB::Yellow; // Yellow leds[0] = CRGB::Purple; // Purple leds[0] = CRGB::Orange; // Orange leds[0] = CRGB::White; // White leds[0] = CRGB::Black; // Off ``` ### Try Custom Colors Create your own colors using RGB values: ```cpp // Custom colors (Red, Green, Blue values 0-255) leds[0] = CRGB(255, 0, 0); // Bright red leds[0] = CRGB(0, 255, 0); // Bright green leds[0] = CRGB(0, 0, 255); // Bright blue leds[0] = CRGB(255, 255, 0); // Yellow leds[0] = CRGB(255, 0, 255); // Magenta leds[0] = CRGB(0, 255, 255); // Cyan ``` ### Try Different Patterns **Fade Effect:** ```cpp // Fade from red to blue for(int i = 0; i < 255; i++) { leds[0] = CRGB(255-i, 0, i); FastLED.show(); delay(10); } ``` **Rainbow Effect:** ```cpp // Rainbow colors leds[0] = CRGB::Red; FastLED.show(); delay(500); leds[0] = CRGB::Orange; FastLED.show(); delay(500); leds[0] = CRGB::Yellow; FastLED.show(); delay(500); leds[0] = CRGB::Green; FastLED.show(); delay(500); leds[0] = CRGB::Blue; FastLED.show(); delay(500); leds[0] = CRGB::Purple; FastLED.show(); delay(500); ``` ## Step 6: Advanced Effects ### Breathing Effect ```cpp // Breathing effect (fade in and out) void breathing() { // Fade in for(int i = 0; i < 255; i++) { leds[0] = CRGB(i, 0, 0); // Red breathing FastLED.show(); delay(10); } // Fade out for(int i = 255; i >= 0; i--) { leds[0] = CRGB(i, 0, 0); FastLED.show(); delay(10); } } void loop() { breathing(); } ``` ### Color Cycle ```cpp // Cycle through all colors void colorCycle() { leds[0] = CRGB::Red; FastLED.show(); delay(1000); leds[0] = CRGB::Orange; FastLED.show(); delay(1000); leds[0] = CRGB::Yellow; FastLED.show(); delay(1000); leds[0] = CRGB::Green; FastLED.show(); delay(1000); leds[0] = CRGB::Blue; FastLED.show(); delay(1000); leds[0] = CRGB::Purple; FastLED.show(); delay(1000); } void loop() { colorCycle(); } ``` ## Understanding the Code ### Key Concepts **1. Library Inclusion:** ```cpp #include <FastLED.h> ``` - This tells Arduino to use the FastLED library **2. Configuration:** ```cpp #define LED_PIN 48 #define NUM_LEDS 1 #define BRIGHTNESS 50 ``` - Sets up the LED pin, number of LEDs, and brightness **3. LED Array:** ```cpp CRGB leds[NUM_LEDS]; ``` - Creates an array to control the LED **4. Setup Function:** ```cpp void setup() { // Runs once when ESP32-S3 starts } ``` **5. Loop Function:** ```cpp void loop() { // Runs forever, repeating your code } ``` **6. LED Control:** ```cpp leds[0] = CRGB::Red; // Set LED to red FastLED.show(); // Update the LED delay(1000); // Wait 1 second ``` ## Common Questions ### Q: Why do I need FastLED library? **A:** WS2812 LEDs are "smart" LEDs that need special commands. FastLED makes it easy to send these commands. ### Q: What does GPIO 48 mean? **A:** GPIO 48 is the pin number where the WS2812 LED is connected on your board. ### Q: Can I change the brightness? **A:** Yes! Change the `BRIGHTNESS` value (0-255). 0 = off, 255 = brightest. ### Q: Why doesn't my LED work? **A:** Check the pin number (should be 48), upload the code properly, and make sure the brightness isn't set to 0. ### Q: Can I add more LEDs? **A:** Yes! Change `NUM_LEDS` to the number of LEDs you have, and control them with `leds[1]`, `leds[2]`, etc. ## Next Steps Now that you can control your LED, try: 1. **Different colors** - Experiment with RGB values 2. **Different patterns** - Create your own blinking sequences 3. **Sensors** - Make the LED respond to buttons or sensors 4. **Music visualization** - Make the LED dance to music 5. **Weather display** - Show weather with different colors ## Summary ✅ **You've learned:** - How to install FastLED library - How to control WS2812 RGB LED - How to create blinking patterns - How to upload programs to ESP32-S3 - How to troubleshoot common issues ✅ **Your ESP32-S3 can now:** - Display 16 million different colors - Create beautiful lighting effects - Respond to your programming commands **Congratulations!** You've taken your first step into the amazing world of microcontroller programming! 🎉 --- ## Support If you encounter any issues, please go to [https://lonelybinary.com](https://lonelybinary.com) and click the Support button located in the bottom right corner. Our team is ready to assist you. [![Technical Support](https://cdn.shopify.com/s/files/1/0331/9994/7908/files/Pasted_image_20250527102623_4e41083f-a1d3-412d-a78f-9cb11ecf69e5.png?v=1754008493)](https://lonelybinary.com) or contact us at office@lonelybinary.com **Keep experimenting and have fun with your colorful LED!** 🌈✨