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:
- How to connect the WS2812B LED strip to Arduino Nano
- How to program Arduino Nano to control the color of each individual LED on the WS2812B LED strip.
- How to program Arduino Nano to control the brightness of each individual LED on the WS2812B LED strip.
All the LEDs on the strip can be controlled with only one digital pin of the Arduino Nano.
Or you can buy the following sensor kit:
|DIYables Sensor Kit 30 types, 69 units
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.
※ NOTE THAT:
The order of pins may differ between manufacturers. It is imperative to always use the labels printed on the 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.
- Initializes WS2812B.
- Specify the color of each individual LED (known as a pixel).
- Adjust the brightness of all the strip.
※ 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
- 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.
- 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