ESP32 - WS2812B LED Strip

In this tutorial, we are going to learn how to use ESP32 to control WS2812B RGB LED strip. With a single ESP32 pin:

Hardware Used In This Tutorial

1×ESP-WROOM-32 Dev Module
1×USB Cable Type-C
1×WS2812B RGB LED Strip
1×1000uF Capacitor
1×470Ω resistor
1×5V Power Adapter
1×(Optional) DC Power Jack
1×Jumper Wires
1×Breadboard
1×(Recommended) Screw Terminal Expansion Board for ESP32
1×(Recommended) Power Splitter For ESP32

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links in this section are Amazon affiliate links, meaning we may earn a commission at no additional cost to you if you make a purchase through them. Additionally, some links direct you to products from our own brand, DIYables.

Introduction to WS2812B RGB LED Strip

Pinout

the WS2812B RGB LED Strip has three pins:

  • GND pin: needs to be connected to GND (0V)
  • VCC pin: needs to be connected to 5V of external power supply
  • Din pin: is pin that receives the control signal. It should be connected to a ESP32 pin.
WS2812B Pinout

※ NOTE THAT:

The order of pins can vary between manufacturers. ALWAYS use the labels printed on the LED Strip.

Wiring Diagram

ESP32 WS2812B RGB LED strip Wiring Diagram

This image is created using Fritzing. Click to enlarge image

If you're unfamiliar with how to supply power to the ESP32 and other components, you can find guidance in the following tutorial: How to Power ESP32.

How To Program For WS2812B RGB LED Strip

There are two libraries can be used to control WS2812B led strip:

  • Adafruit NeoPixel library.
  • FastLED library.

This tutorial will use the Adafruit NeoPixel library

  • Declare a WS2812B object
#define PIN_WS2812B 4 // ESP32 pin that connects to WS2812B #define NUM_PIXELS 30 // The number of LEDs (pixels) on WS2812B Adafruit_NeoPixel WS2812B(NUM_PIXELS, PIN_WS2812B, NEO_GRB + NEO_KHZ800);
  • Initializes the WS2812B
WS2812B.begin(); // INITIALIZE WS2812B strip object (REQUIRED)
  • Set color of each individual LED (called pixel).
WS2812B.setPixelColor(pixel, WS2812B.Color(255, 0, 0));
  • Set brightness of all strip.
WS2812B.setBrightness(100); // a value from 0 to 255

※ NOTE THAT:

  • WS2812B.setBrightness() is used for all pixel on LED strip. To set the brightness for each individual pixel, we can scale the color value.
  • The values set by WS2812B.setBrightness() and WS2812B.setPixelColor() only take effect when WS2812B.show() is called.

ESP32 Code

The below code does:

  • Turn pixels to green one by one with a delay between each pixel
  • Turn off all pixels for two seconds
  • Turn on all pixels to red at the same time for two seconds
  • Repeat the above process infinitely
/* * This ESP32 code is created by esp32io.com * * This ESP32 code is released in the public domain * * For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-ws2812b-led-strip */ #include <Adafruit_NeoPixel.h> #define PIN_WS2812B 16 // The ESP32 pin GPIO16 connected to WS2812B #define NUM_PIXELS 30 // The number of LEDs (pixels) on WS2812B LED strip Adafruit_NeoPixel ws2812b(NUM_PIXELS, PIN_WS2812B, NEO_GRB + NEO_KHZ800); void setup() { ws2812b.begin(); // initialize WS2812B strip object (REQUIRED) } void loop() { ws2812b.clear(); // set all pixel colors to 'off'. It only takes effect if pixels.show() is called // turn pixels to green one-by-one with delay between each pixel for (int pixel = 0; pixel < NUM_PIXELS; pixel++) { // for each pixel ws2812b.setPixelColor(pixel, ws2812b.Color(0, 255, 0)); // it only takes effect if pixels.show() is called ws2812b.show(); // update to the WS2812B Led Strip delay(500); // 500ms pause between each pixel } // turn off all pixels for two seconds ws2812b.clear(); ws2812b.show(); // update to the WS2812B Led Strip delay(2000); // 2 seconds off time // turn on all pixels to red at the same time for two seconds for (int pixel = 0; pixel < NUM_PIXELS; pixel++) { // for each pixel ws2812b.setPixelColor(pixel, ws2812b.Color(255, 0, 0)); // it only takes effect if pixels.show() is called } ws2812b.show(); // update to the WS2812B Led Strip delay(1000); // 1 second on time // turn off all pixels for one seconds ws2812b.clear(); ws2812b.show(); // update to the WS2812B Led Strip delay(1000); // 1 second off time }

Quick Instructions

  • Click to the Libraries icon on the left bar of the Arduino IDE.
  • Search “Adafruit NeoPixel”, then find the WS2812B library by Adafruit
  • Click Install button to install NeoPixel library.
ESP32 neopixel library
  • Copy the above code and open with Arduini IDE
  • Click Upload button on Arduini IDE to upload code to ESP32
  • See the LED effect

※ NOTE THAT:

For any complicated LED effect, we offer the paid programming service

Video Tutorial

Making video is a time-consuming work. If the video tutorial is necessary for your learning, please let us know by subscribing to our YouTube channel , If the demand for video is high, we will make the video tutorial.

※ OUR MESSAGES