Standard LEDs are great, and RGB LEDs are fun, but if you want to create stunning animations, ambient room lighting, or scrolling marquees, wiring up dozens of standard LEDs is a nightmare.
Enter the WS2812B Addressable LED. Commonly known by Adafruit’s brand name, “NeoPixels,” these LEDs are a game changer. They contain a tiny microchip inside every single LED. This means you can string hundreds of them together and control the color and brightness of each individual LED using just one single digital pin on your Arduino.
Addressable LEDs allow for incredibly complex and colorful lighting animations.
Before we write code, we need to talk about power. Addressable LEDs are incredibly bright and draw a lot of current. A single WS2812B LED at full white brightness can draw up to 60 milliamps (mA).
If you have a strip of 60 LEDs, that’s 60 * 60mA = 3600mA (or 3.6 Amps!).
Your Arduino’s 5V pin can only safely provide around 400-500mA. If you try to power a long strip of WS2812B LEDs directly from your Arduino, you will burn out your board’s voltage regulator.
For strips longer than 8-10 LEDs, you must use an external 5V power supply.
Need the parts? Grab the essentials on Amazon:
Let’s assume we are safely powering a strip of 30 LEDs using an external power supply.
There are two main libraries for controlling these LEDs: Adafruit NeoPixel and FastLED. In this lesson, we will use FastLED, as it is highly optimized and fantastic for complex animations.
Sketch > Include Library > Manage Libraries...Let’s create a classic animation: a single red dot bouncing back and forth along the strip.
#include <FastLED.h>
// How many LEDs are in your strip?
#define NUM_LEDS 30
// Which pin is connected to the data line?
#define DATA_PIN 6
// Define the array of LEDs
CRGB leds[NUM_LEDS];
void setup() {
// Tell FastLED about our setup
FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);
// Set global brightness (0-255).
// Keep it low while testing to save power!
FastLED.setBrightness(50);
}
void loop() {
// Move the dot forward
for(int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB::Red; // Set the current LED to red
FastLED.show(); // Update the strip
delay(30); // Wait a moment
leds[i] = CRGB::Black; // Turn it off before moving to the next
}
// Move the dot backward
for(int i = NUM_LEDS - 1; i >= 0; i--) {
leds[i] = CRGB::Red;
FastLED.show();
delay(30);
leds[i] = CRGB::Black;
}
}
CRGB leds[NUM_LEDS];): We create an array that acts like a map of our strip. leds[0] is the first LED, leds[1] is the second, and so on.CRGB::Red or CRGB::Blue, or we can set exact RGB values like CRGB(255, 0, 0).FastLED.show(): Similar to the OLED display we learned about in Lesson 139, updating the leds array doesn’t actually change the physical lights. The lights only update when you call FastLED.show(). This allows you to set up complex patterns across the entire strip and push them all out instantly.FastLED.setBrightness() is incredibly useful. It acts as a master dimmer, allowing you to test code without blinding yourself or drawing too much power from your supply.Addressable LEDs open up a massive world of creative possibilities. From here, you can explore rainbow gradients, reactive music visualizers, or custom holiday lighting!