cookbook/getting-started/first-example.md
Write and run your first LED program with FastLED.
Difficulty Level: ⭐ Beginner Time to Complete: 30 minutes (including hardware setup) Prerequisites:
You'll Learn:
fill_solid() to set all LEDs to one colorFastLED.show() is required to update your LED stripThis program blinks all LEDs between red and off:
#include <FastLED.h>
// Configuration
#define LED_PIN 5
#define NUM_LEDS 60
#define LED_TYPE WS2812B
#define COLOR_ORDER GRB
// LED array
CRGB leds[NUM_LEDS];
void setup() {
// Initialize FastLED
FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS);
FastLED.setBrightness(50); // Start with low brightness
}
void loop() {
// Turn all LEDs red
fill_solid(leds, NUM_LEDS, CRGB::Red);
FastLED.show();
delay(1000);
// Turn all LEDs off
fill_solid(leds, NUM_LEDS, CRGB::Black);
FastLED.show();
delay(1000);
}
void setup() {
FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS);
FastLED.setBrightness(50);
}
FastLED.addLeds<>() - Tells FastLED about your hardwareFastLED.setBrightness(50) - Sets brightness to ~20% (safe for testing)void loop() {
fill_solid(leds, NUM_LEDS, CRGB::Red); // Set all LEDs to red
FastLED.show(); // Send to strip
delay(1000); // Wait 1 second
fill_solid(leds, NUM_LEDS, CRGB::Black); // Set all LEDs to off
FastLED.show(); // Send to strip
delay(1000); // Wait 1 second
}
#define LED_PIN 6 // Use pin 6 instead of 5
#define NUM_LEDS 30 // For a 30-LED strip
fill_solid(leds, NUM_LEDS, CRGB::Blue); // Blue instead of red
fill_solid(leds, NUM_LEDS, CRGB::Green); // Green
fill_solid(leds, NUM_LEDS, CRGB::Purple); // Purple
delay(500); // Faster (0.5 seconds)
delay(2000); // Slower (2 seconds)
LED_PIN matches your wiringFastLED.setBrightness(100)LED_TYPE matches your stripTry different COLOR_ORDER values:
#define COLOR_ORDER GRB // Try GRB first (most common)
// If that doesn't work, try:
#define COLOR_ORDER RGB
#define COLOR_ORDER BGR
// Named colors
CRGB::Red
CRGB::Green
CRGB::Blue
CRGB::White
CRGB::Purple
CRGB::Orange
CRGB::Yellow
// Custom RGB colors
CRGB(255, 0, 0) // Red
CRGB(0, 255, 0) // Green
CRGB(128, 0, 128) // Purple
// HSV colors
CHSV(0, 255, 255) // Red
CHSV(96, 255, 255) // Green
CHSV(160, 255, 255) // Blue
void loop() {
// Turn on first 3 LEDs to different colors
leds[0] = CRGB::Red;
leds[1] = CRGB::Green;
leds[2] = CRGB::Blue;
// Rest of LEDs stay off
FastLED.show();
delay(1000);
}
// Fill LEDs 10-19 with green
fill_solid(&leds[10], 10, CRGB::Green);
Now that you have a working program: