Arduino - WS2812B LED Strip

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

Hardware Required

1×Arduino UNO or Genuino UNO
1×USB 2.0 cable type A/B
1×WS2812B RGB LED Strip
1×1000uF Capacitor
1×470Ω resistor
1×5V Power Adapter
1×DC Power Jack
1×Jumper Wires
1×(Optional) 9V Power Adapter for Arduino
1×(Recommended) Screw Terminal Block Shield for Arduino Uno
1×(Optional) Transparent Acrylic Enclosure For Arduino Uno

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Please note: These are Amazon affiliate links. If you buy the components through these links, We will get a commission at no extra cost to you. We appreciate it.

About 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 Arduino pin.
WS2812B Pinout

※ NOTE THAT:

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

Wiring Diagram

Arduino WS2812B RGB LED strip Wiring Diagram

This image is created using Fritzing. Click to enlarge image

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 // Arduino 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.

Arduino 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
/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-ws2812b-led-strip */ #include <Adafruit_NeoPixel.h> #ifdef __AVR__ #include <avr/power.h> // Required for 16 MHz Adafruit Trinket #endif #define PIN_WS2812B 4 // Arduino pin that connects to WS2812B #define NUM_PIXELS 30 // The number of LEDs (pixels) on WS2812B #define DELAY_INTERVAL 250 // 250ms pause between each pixel 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 }

Quick Steps

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

※ NOTE THAT:

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

Video Tutorial

We are considering to make the video tutorials. If you think the video tutorials are essential, please subscribe to our YouTube channel to give us motivation for making the videos.

The Best Arduino Starter Kit

※ OUR MESSAGES