ESP8266 - WS2812B LED Strip

The WS2812B RGB LED strip is composed of LEDs whose color and brightness can be adjusted individually. This tutorial instructs you how to use ESP8266 to control the WS2812B RGB LED strip. In detail, we will learn:

We only need to use one ESP8266's digital pin to control all the LEDs on the strip.

Hardware Preparation

1×ESP8266 NodeMCU
1×Micro USB Cable
1×WS2812B RGB LED Strip
1×1000uF Capacitor
1×470Ω resistor
1×5V Power Adapter
1×DC Power Jack
1×Jumper Wires
1×Breadboard
1×(Optional) 5V Power Adapter for ESP8266
1×(Optional) Screw Terminal Expansion Board for ESP8266

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 provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables.

Overview of WS2812B RGB LED Strip

The WS2812B LED Strip Pinout

The WS2812B RGB LED Strip has three pins that require specific connections:

  • GND pin must be linked to GND (0V)
  • VCC pin should be connected to the 5V of an external power source
  • Din pin is the pin that receives the control signal and should be linked to an ESP8266 pin.
WS2812B pinout

※ NOTE THAT:

The sequence of pins may differ depending on the manufacturer. It is IMPERATIVE to use the markings printed on the LED Strip.

Wiring Diagram

The wiring diagram between ESP8266 NodeMCU and WS2812B RGB LED strip

This image is created using Fritzing. Click to enlarge image

See more in ESP8266's pinout and how to supply power to the ESP8266 and other components.

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.

  • Create a WS2812B object
#define PIN_WS2812B D1 // The ESP8266 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);
  • Strip with 30 LEDs
  • Initiates the WS2812B strip which contains 30 lights.
WS2812B.begin(); // INITIALIZE WS2812B strip object (REQUIRED)
  • Specify the colour of each LED (known as a pixel).
WS2812B.setPixelColor(pixel, WS2812B.Color(255, 0, 0));
  • Adjust the brightness of all the strips.
WS2812B.setBrightness(100); // a value from 0 to 255

※ NOTE THAT:

  • WS2812B.setBrightness() is used to adjust the brightness of all pixels on the LED strip. To set the brightness for each individual pixel, we can scale the color values (R,G, B) with the same ratio.
  • The values set by WS2812B.setBrightness() and WS2812B.setPixelColor() will only be applied when WS2812B.show() is executed.

ESP8266 Code

The code below does the following sequences:

  • Turn pixels to green one-by-one with a delay between each pixel
  • Turns off all pixels for two seconds
  • Lights up all pixels red simultaneously for two seconds
  • Repeats this process endlessly
/* * This ESP8266 NodeMCU code was developed by newbiely.com * * This ESP8266 NodeMCU code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp8266/esp8266-ws2812b-led-strip */ #include <Adafruit_NeoPixel.h> #define PIN_WS2812B D1 // The ESP8266 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); 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(); // send the updated pixel colors to the WS2812B hardware. delay(DELAY_INTERVAL); // pause between each pixel } // turn off all pixels for two seconds WS2812B.clear(); WS2812B.show(); // send the updated pixel colors to the WS2812B hardware. delay(2000); // 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(); // send the updated pixel colors to the WS2812B hardware. delay(2000); // on time // turn off all pixels for one seconds WS2812B.clear(); WS2812B.show(); // send the updated pixel colors to the WS2812B hardware. delay(2000); // off time }

Detailed Instructions

To get started with ESP8266 on Arduino IDE, follow these steps:

  • Check out the how to setup environment for ESP8266 on Arduino IDE tutorial if this is your first time using ESP8266.
  • Wire the components as shown in the diagram.
  • Connect the ESP8266 board to your computer using a USB cable.
  • Open Arduino IDE on your computer.
  • Choose the correct ESP8266 board, such as (e.g. NodeMCU 1.0 (ESP-12E Module)), and its respective COM port.
  • Click to the Libraries icon on the left bar of the Arduino IDE.
  • Search for “Adafruit NeoPixel” and locate the NeoPixel library from Adafruit.
  • Then, press the Install button to install the NeoPixel library.
ESP8266 NodeMCU NeoPixel library
  • Copy the code and open it in the Arduino IDE.
  • Click the Upload button to send the code to the ESP8266.
  • Check out the LED effect.

※ NOTE THAT:

For any intricate LED effect, we provide the paid programming service

Video Tutorial

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!