This guide shows how to control WS2812B addressable RGB LEDs (also known as Neopixels) with the ESP32 using Arduino IDE. You’ll learn how to wire the WS2812B LEDs to the ESP32 and how to control them using the FastLED library.

In this tutorial, we’ll control a WS2812B addressable RGB LED (neopixels) strip, but it works with any strip or PCB board with WS2812B LEDs:

Read our MicroPython Guide: WS2812B Addressable RGB LEDs with ESP32 and ESP8266
Table of Contents
In this guide, we’ll cover the following subjects:
- Introducing WS2812B RGB LEDs (Neopixels)
- Wiring the WS2812B (Neopixels) to the ESP32
- Example #1 – ESP32 Control WS2812B RGB LEDs Individually
- Example #2 – ESP32 Control All WS2812B RGB LEDs
- Example #3 – ESP32 Chase/Cycle Effect WS2812B RGB LEDs
- Example #4 – ESP32 Rainbow Effect WS2812B RGB LEDs
Prerequisites
Before proceeding, make sure you have the Arduino IDE installed and the ESP32 boards. Follow the next tutorial if you haven’t already:
Introducing WS2812B RGB LEDs (Neopixels)
The WS2812B LEDs are addressable RGB LEDs that can be connected in series and individually using just one digital pin of a microcontroller. These LEDs have an IC built right into the LED that makes all of this possible. This means you can control many LEDs using just one digital pin of your ESP32.
In the following figure, you can see the chip inside the LED. The LED is an RGB LED and works like so.

Powering the WS2812B LED Strip
The LED strip should be powered using a 5V power source. At 5V, each LED draws about 50mA when set to its full brightness. This means that for every 30 LEDs, the strip may draw as much as 1.5 A. Make sure you select a power source that matches the strip’s needs. An AC to DC power adapter that provides 5V and 2A should do the job:
If you use an external power source, don’t forget to connect the power source ground to the ESP32 GND pin.
In this tutorial, we’ll control a line of 8 LEDs, so we don’t need an external power source. We’ll use the ESP32 VIN pin.
Parts Required
For this project, you need the following parts:
- ESP32 board – any model of your choice
- WS2812B Addressable RGB LED Strips / Rings
- Jumper Wires
You can use the preceding links or go directly to MakerAdvisor.com/tools to find all the parts for your projects at the best price!
Wiring the WS2812B (Neopixels) to the ESP32
Wiring the WS2812B to the ESP32 is quite simple, as it only requires one digital pin (DIN).
| WS2812B | ESP32 |
| GND | GND |
| DIN | Any digital pin (for example: GPIO 2)* |
| 4-7 VDC | VIN 5V |
* you can use any other suitable GPIOs. Check the ESP32 Pinout Guide:
- ESP32 Pinout Reference: Which GPIO pins should you use?
- ESP32-S3 DevKitC Pinout Reference Guide: GPIOs Explained
Wiring the RGB LED strip to the ESP32 is very simple. You need to apply 5V to the VDC pin, GND to GND, and connect a GPIO to the DIN (data) pin. We’ll connect the data pin to GPIO 2.

You can solder several LED rings, sticks, or strips, and they will behave as one piece. Each PCB has DIN and DOUT pins that make wiring very simple:

In our example, we’ll use a stick with 8 LEDs as shown in the picture below.

Installing the FastLED Library
There are several libraries to interface the WS2812B LEDs with the ESP32. We’ll use the FastLED library. You can install it quickly by following these steps:
- Open the Arduino IDE Library Manager
- Search for FastLED
- Install the FastLED library by Daniel Garcia

Example #1 – ESP32 Control WS2812B RGB LEDs Individually
The following code sets a different color for each WS2812B RGB LED. Upload the following code to your board, and it will work straight away
/*
Rui Santos & Sara Santos - Random Nerd Tutorials
Complete project details at https://RandomNerdTutorials.com/esp32-ws2812b-neopixels-arduino/
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files.
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*/
#include <FastLED.h>
#define NUM_LEDS 8
#define DIN_PIN 2
#define BRIGHTNESS 80 // Brightness (0-255)
#define DELAY_MS 500
CRGB leds[NUM_LEDS];
void setup() {
FastLED.addLeds<WS2812B, DIN_PIN, GRB>(leds, NUM_LEDS);
FastLED.setBrightness(BRIGHTNESS);
FastLED.clear();
}
void loop() {
leds[0] = CRGB(255, 0, 0); // Red » (R=255, G=0, B=0)
FastLED.show();
delay(DELAY_MS);
leds[1] = CRGB(0, 255, 0); // Green » (R=0, G=255, B=0)
FastLED.show();
delay(DELAY_MS);
leds[2] = CRGB(0, 0, 255); // Blue » (R=0, G=0, B=255)
FastLED.show();
delay(DELAY_MS);
leds[3] = CRGB(255, 165, 0); // Orange » (R=255, G=165, B=0)
FastLED.show();
delay(DELAY_MS);
leds[4] = CRGB(128, 0, 128); // Purple » (R=128, G=0, B=128)
FastLED.show();
delay(DELAY_MS);
leds[5] = CRGB(255, 255, 0); // Yellow » (R=255, G=255, B=0)
FastLED.show();
delay(DELAY_MS);
leds[6] = CRGB(255, 255, 255); // White » (R=255, G=255, B=255)
FastLED.show();
delay(DELAY_MS);
leds[7] = CRGB(0, 0, 0); // Off/Black » (R=0, G=0, B=0)
FastLED.show();
delay(DELAY_MS);
FastLED.clear();
delay(DELAY_MS);
}
How Does the Code Work?
Let’s take a quick look at the code to see how this first example works.
Including Library
First, you need to include the FastLED.h library, which is required to control the WS2812B LEDs.
#include <FastLED.h>
Global Variables
Defines the total number of LEDs in your strip or ring.
#define NUM_LEDS 8
The LED strip’s data in pin (DIN_PIN) is connected to the ESP32 GPIO 2.
#define DIN_PIN 2
Set the brightness of all LEDs (range is from 0 to 255).
#define BRIGHTNESS 80
Delay time for the animation in milliseconds (500ms = 0.5s).
#define DELAY_MS 500
Create an array called leds that holds all colors for the 8 LEDs. CRGB is FastLED’s color type definition (Red, Green, Blue).
CRGB leds[NUM_LEDS];
setup()
In the setup(), start by initializing the LED strip or ring using the FastLED library. You need to pass the arguments:
- LED Type = WS2812B
- Data Pin = DIN_PIN
- Color Order = GRB (Most WS2812B LEDs use GRB order, not RGB)
FastLED.addLeds<WS2812B, DIN_PIN, GRB>(leds, NUM_LEDS);
Set the global brightness level and turn off all LEDs at the start.
FastLED.setBrightness(BRIGHTNESS);
FastLED.clear();
loop()
The loop() runs continously. It first sets the first LED (with index 0) to the color Red (R=255, G=0, B=0).
leds[0] = CRGB(255, 0, 0);
The previous line only sets the color. To actually change the color from the physical LEDs, you must call the show() method.
FastLED.show();
Add a delay of 500 milliseconds:
delay(DELAY_MS);
Then we control the other LEDs, one by one, in different colors.
leds[1] = CRGB(0, 255, 0); // Green » (R=0, G=255, B=0)
FastLED.show();
delay(DELAY_MS);
leds[2] = CRGB(0, 0, 255); // Blue » (R=0, G=0, B=255)
FastLED.show();
delay(DELAY_MS);
leds[3] = CRGB(255, 165, 0); // Orange » (R=255, G=165, B=0)
FastLED.show();
delay(DELAY_MS);
leds[4] = CRGB(128, 0, 128); // Purple » (R=128, G=0, B=128)
FastLED.show();
delay(DELAY_MS);
leds[5] = CRGB(255, 255, 0); // Yellow » (R=255, G=255, B=0)
FastLED.show();
delay(DELAY_MS);
leds[6] = CRGB(255, 255, 255); // White » (R=255, G=255, B=255)
FastLED.show();
delay(DELAY_MS);
leds[7] = CRGB(0, 0, 0); // Off/Black » (R=0, G=0, B=0)
FastLED.show();
delay(DELAY_MS);
Finally, we turn off all the LEDs by calling the clear() method.
FastLED.clear();
delay(DELAY_MS);
Demonstration
Here’s an illustration of the example running:


Example #2 – ESP32 Control All WS2812B RGB LEDs
The following code applies the same color to all WS2812B RGB LEDs with a single command. You can upload the following code to your ESP32 board.
/*
Rui Santos & Sara Santos - Random Nerd Tutorials
Complete project details at https://RandomNerdTutorials.com/esp32-ws2812b-neopixels-arduino/
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files.
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*/
#include <FastLED.h>
#define NUM_LEDS 8
#define DIN_PIN 2
#define BRIGHTNESS 80 // Brightness (0-255)
#define DELAY_MS 500
CRGB leds[NUM_LEDS];
void setup() {
FastLED.addLeds<WS2812B, DIN_PIN, GRB>(leds, NUM_LEDS);
FastLED.setBrightness(BRIGHTNESS);
FastLED.clear();
}
void loop() {
fill_solid(leds, NUM_LEDS, CRGB(255, 0, 0)); // Color » Red
FastLED.show();
delay(DELAY_MS);
fill_solid(leds, NUM_LEDS, CRGB(0, 255, 0)); // Color » Green
FastLED.show();
delay(DELAY_MS);
fill_solid(leds, NUM_LEDS, CRGB(0, 0, 255)); // Color » Blue
FastLED.show();
delay(DELAY_MS);
fill_solid(leds, NUM_LEDS, CRGB(255, 255, 255)); // Color » White
FastLED.show();
delay(DELAY_MS);
}
Code Overview
Now let’s take a quick look at the code in this section. The code initialization is very similar to Example #1, but instead of controlling each LED individually, we set all the LEDs to the same color using a single command.
To do that, use the fill_solid() function that accepts as arguments the array that stores the color data for all LEDs, the number of LEDs in your strip, and the color you want to apply:
fill_solid(leds, NUM_LEDS, CRGB(255, 0, 0));
FastLED.show();
delay(DELAY_MS);
We repeat the same process to apply other colors:
fill_solid(leds, NUM_LEDS, CRGB(0, 255, 0)); // Color » Green
FastLED.show();
delay(DELAY_MS);
fill_solid(leds, NUM_LEDS, CRGB(0, 0, 255)); // Color » Blue
FastLED.show();
delay(DELAY_MS);
fill_solid(leds, NUM_LEDS, CRGB(255, 255, 255)); // Color » White
FastLED.show();
delay(DELAY_MS);
Demonstration
Here’s an illustration of the example #2 running:


Example #3 – ESP32 Chase/Cycle Effect WS2812B RGB LEDs
The following code creates a chase/cycle effect. It lights up one LED at a time that moves across the entire strip, while the other LEDs remain off. Upload the following code to your ESP32 board.
/*
Rui Santos & Sara Santos - Random Nerd Tutorials
Complete project details at https://RandomNerdTutorials.com/esp32-ws2812b-neopixels-arduino/
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files.
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*/
#include <FastLED.h>
#define NUM_LEDS 8
#define DIN_PIN 2
#define BRIGHTNESS 80 // Brightness (0-255)
#define DELAY_MS 50 // Chase effect speed
CRGB leds[NUM_LEDS];
void setup() {
FastLED.addLeds<WS2812B, DIN_PIN, GRB>(leds, NUM_LEDS);
FastLED.setBrightness(BRIGHTNESS);
FastLED.clear();
}
void loop() {
// Chase effect from left to right
for (int i = 0; i < NUM_LEDS; i++) {
FastLED.clear(); // Clear LEDs
leds[i] = CRGB(0, 0, 255); // Light current position
FastLED.show();
delay(DELAY_MS); // Chase effect speed
}
// Reverse the chase effect direction
for (int i = NUM_LEDS - 1; i >= 0; i--) {
FastLED.clear();
leds[i] = CRGB(0, 0, 255);
FastLED.show();
delay(DELAY_MS);
}
}
Code Overview
The code in this example is similar to the previous ones. We’ll skip the similar parts and only explain the chase effect.
This loop creates a simple chase effect by lighting up one blue LED at a time, moving it across the strip from the first to the last LED, with a short delay between each step.
for (int i = 0; i < NUM_LEDS; i++) {
FastLED.clear();
leds[i] = CRGB(0, 0, 255);
FastLED.show();
delay(DELAY_MS);
}
Then, we repeat the function but move the LEDs from right to left:
for (int i = NUM_LEDS - 1; i >= 0; i--) {
FastLED.clear();
leds[i] = CRGB(0, 0, 255);
FastLED.show();
delay(DELAY_MS);
}
Demonstration
Below is an illustration of Example #3 in action:


Example #4 – ESP32 Rainbow Effect WS2812B RGB LEDs
The next code creates a rainbow effect with colors moving smoothly across all LEDs. Upload the code below to your ESP32 board.
/*
Rui Santos & Sara Santos - Random Nerd Tutorials
Complete project details at https://RandomNerdTutorials.com/esp32-ws2812b-neopixels-arduino/
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files.
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*/
#include <FastLED.h>
#define NUM_LEDS 8
#define DIN_PIN 2
#define SATURATION 255 // Saturarion (0-255)
#define BRIGHTNESS 255 // Brightness (0-255)
#define DELAY_MS 20
CRGB leds[NUM_LEDS];
void setup() {
FastLED.addLeds<WS2812B, DIN_PIN, GRB>(leds, NUM_LEDS);
FastLED.setBrightness(BRIGHTNESS);
FastLED.clear();
}
void loop() {
for (int i = 0; i < NUM_LEDS; i++) {
// Set LED i to a moving rainbow color effect: i*10 creates color spread across LEDs
// millis()/20 makes the rainbow slowly animate over time with full saturation and full brightness
leds[i] = CHSV(i * 10 + millis() / 20, SATURATION, BRIGHTNESS);
}
FastLED.show();
delay(DELAY_MS);
}
Code Overview
This example is also very similar to the previous ones, so we’ll focus only on the new code in the loop() function. This next code snippet assigns a shifting rainbow color to the LED in position i, producing a dynamic animated rainbow effect.
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CHSV(i * 10 + millis() / 20, SATURATION, BRIGHTNESS);
}
FastLED.show();
delay(DELAY_MS);
- CHSV() – Creates a color using the HSV color model (Hue, Saturation, Value/Brightness)
- i * 10 – Changes the hue for each LED, creating a spread of colors across the strip
- millis() / 20 – Adds a time based offset, making the rainbow colors slowly move along the strip
- SATURATION (255) – Sets full saturation
- BRIGHTNESS (255) – Sets full brightness
Demonstration
Here’s what Example #4 looks like when it’s running:


Wrapping Up
In this tutorial, you’ve learned how to control WS2812B addressable RGB LEDs (rings, strips, or sticks). Controlling these LEDs with Arduino coding is simple thanks to the FastLED library. Now, you can apply the concepts learned in this tutorial in your own projects. For example, you can build a web server with different buttons that control the LEDs remotely (we’ll create something similar in a future tutorial).
We have more tutorials about RGB LEDs that you may like:
- How do RGB LEDs work?
- Node-RED with WS2812B Addressable RGB LED Strip
- MicroPython: WS2812B Addressable RGB LEDs with ESP32 and ESP8266
Learn more about the ESP32 with our resources:
We hope you enjoyed this project and learned something new.
Thanks for reading.




