Pixels part 3: FastLED
Posted on Tue 30 January 2024 in Making
In the first post in this series, I introduced RGB pixels, discussed the various types, and compared analog and true RGB pixels. In the second part, I covered wiring and shared my tips for reliably connecting to your microcontroller. In this third part, I will discuss the FastLED library, one of the most popular libraries for programming pixels.
FastLED
FastLED is a hugely popular library for programming all sorts of RGB pixels. The intro on that linked page summarizes the library nicely: fast, easy to use, widely used, and under active development. See the GitHub page for code and an excellent wiki. Get help in the FastLED Reddit sub.
Let's start with the basics. For this, I think it will help to see code. The comments inline explain what's going on.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
|
Phew, that was a lot. I hope you read through the comments and they were helpful. I'll expand on a few points. Starting from the top, the LED_TYPE
is a constant representing the type of pixels you're using. FastLED supports many, though not all types. The supported pixels are listed here.
Next is the COLOR_ORDER
. This varies by pixel type. Some expect the data to have the red color bits first, followed by green, then blue. But others will be in BGR or GRB or whatever order. I'm sure there's a better way, but when I don't know, I'll set all the LEDs to be red. If they show up blue or green, I know to start with a different order. Repeat for green and blue till you get the order correct.
Next I'll jump down to the FastLED.addLeds
call. There are two primary versions of this function: one for 3-wire pixels and the other for 4-wire pixels. Shown above is the 3-wire variant, where there is no dedicated clock line. If you're using 4-wire pixels, the function you'll call is FastLED.addLeds<LED_TYPE, DATA_PIN, CLOCK_PIN, COLOR_ORDER>(leds, NUM_LEDS)
.
Colors and palettes
In the example above, I demonstrated using one of the pre-defined palettes of color. These define a set of colors that you can apply across the array of LEDs to get a pleasing mix of colors, such as a rainbow or set of ocean (blue-green) tones. That might be considered a bit more advanced than setting a specific color.
FastLED gives you many ways to specify a color for an LED. You can do so using one of the pre-defined named colors, by RGB value, HSV value, and more. For example, to set an LED to display 100% red, you could use any of the following:
leds[i] = CRGB::Red;
leds[i] = 0xFF0000;
leds[i] = CRGB(255, 0, 0);
leds[i].setRGB(255, 0, 0);
leds[i] = CHSV(HUE_RED, 255, 255); // HUE_RED happens to be 0 on the color wheel
leds[i].setHSV(HUE_RED, 255, 255);
// or
leds[i].red = 255;
leds[i].green = 0;
leds[i].blue = 0;
// ... and probably more!
There's one more trick up the sleeve ... let's say you wanted to set all of the LEDs to red. You could loop through, set the color using one of the above techniques, then call FastLED.show()
. Or, you could call FastLED.setColor(CRGB::Red)
(or specify a hex value, HSV value, etc.). With setColor()
you do not call .show()
!
Fading out
I'll end with two techniques for fading to black. In both cases, you would put the call to the function inside the loop()
function so that it's called with each iteration of the program loop.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
Or:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
There's a ton more to explore in the FastLED library. There's support for matrices, controlling multiple strips (even of different types), built-in fast math functions for varying pixels by sine waves and such, and so much more. I hope this gets you off to a good start. Happy exploring.