Arduino Nano - WS2812B LED Strip

The WS2812B RGB LED strip is a line of LEDs where the color and brightness of each one can be adjusted independently. This tutorial instructs you how to use an Arduino Nano to control the WS2812B RGB LED strip. In detail, we will learn:

All the LEDs on the strip can be controlled with only one digital pin of the Arduino Nano.

Hardware Preparation

1×Arduino Nano
1×USB A to Mini-B 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×(Recommended) Screw Terminal Expansion Board for Arduino Nano
1×(Recommended) Breakout Expansion Board for Arduino Nano

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:

  • GND pin: must be connected to GND (0V)
  • VCC pin: must be connected to 5V of an external power source
  • Din pin: is the pin that receives the control signal and should be connected to an Arduino Nano's digital pin.
WS2812B pinout

※ NOTE THAT:

The order of pins may differ between manufacturers. It is imperative to always use the labels printed on the LED Strip.

Wiring Diagram

The wiring diagram between Arduino Nano and WS2812B RGB LED strip

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.

  • Create a WS2812B object.
#define PIN_WS2812B 2 // Arduino Nano 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 WS2812B.
WS2812B.begin(); // INITIALIZE WS2812B strip object (REQUIRED)
  • Specify the color of each individual LED (known as a pixel).
WS2812B.setPixelColor(pixel, WS2812B.Color(255, 0, 0));
  • Adjust the brightness of all the strip.
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 called.

Arduino Nano 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 in red for two seconds
  • Repeats this process endlessly
/* * This Arduino Nano code was developed by newbiely.com * * This Arduino Nano code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano/arduino-nano-ws2812b-led-strip */ #include <Adafruit_NeoPixel.h> #define PIN_WS2812B 2 // The Arduino Nano 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

  • Click to the Libraries icon on the left bar of the Arduino IDE.
  • Search for “Adafruit NeoPixel” and locate the NeoPixel library by Adafruit.
  • Then, press the Install button to install the NeoPixel library.
Arduino Nano ws2812b library
  • Copy the code and open it in the Arduino IDE.
  • Click the Upload button in the Arduino IDE to send the code to the Arduino Nano.
  • 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!