Individually Addressable WS2812B Full Color LED strip Code Examples and Review

Today I review an individually addressable WS2812B Full Color LED strip and provide some code examples.

Overview of the WS2812B Full Color LED strip

The WS2812B Full colour LED strip I purchased is 5 meters long and has 150 LED’s on it. Each LED has its LED driver which allows each one to be individually addressable.

Between each LED is a cutting point to allow the strip to be cut to the required length easily. The connections on the LED strip are GND, Digital in/out, and 5V. the simple connections allow you to easily cut up the LED strip as required.

Another advantage of these LED strips is that there is excellent Arduino support from the FastLED library. This allows you to easily control each LED or all of them at once.

 

Wiring it up to an Arduino

Here is it expected that we have cut a specific length (or are using the entire length) of the LED strip and each Digital In is connected to each Digital Out.

The 5V and ground are connected to the Arduino’s 5V and GND ports respectively. To reduce the noise and current on the digital line it is recommended to put a 330Ω resistor between the Digital in and the Arduino digital pin you are using. Here I have wired the circuit to Arduino pin 3.

Programming the Arduino with the WS2812B Full Color LED strip

To program the WS2812B full colour LED strip I am going to use the FastLED library. This handles some of the complexities of talking to the LED strip and accessing each LED. This can be obtained by adding the “FastLED” library in the “manage library” section of the Arduino IDE.

The first step to program the LED strip is to define some constants and set up the LED’s.

#include <FastLED.h>
#define LED_PIN 3
#define NUM_LEDS 100

CRGB leds[NUM_LEDS];

Above I include the FastLED library and define that the LED data pin will be pin 2. I also set up the number of LED’s I am going to be accessing (set to 100). Then an array of CRGB objects is created, each array element provides control over a single LED. In creating this object I use the NUM_LEDS define so that the number is constant throughout the program.

void setup() {
    FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
}

In the setup function I add the LED’s defined above into the FastLED library. This configures them so that they can be controlled later in the program.

void loop() { 
  for(int value = 0; value < 255; value += 1) {
    for(int i = 0; i < NUM_LEDS; i++) {
      leds[i] = CHSV(value, 128, 255);
    }
    FastLED.show();
    delay(10);
  }
}

In the final piece of code I have two loops, the outer loop loops over the hue of the LED’s and the inner loops over every LED.

The Inner loop is used to loop over every LED and set them to the same value. Once they are all set the FastLED library is called with the function show. This sends out a message to update the LED’s with the changes made.

Once this is done I wait 10 milliseconds and increase the hue in the first loop. Each time the hue loop is run the hue value is increased by one.

By increasing the hue slowly the LED’s loop over a wide range of colours producing an interesting effect (shown below).

Final Review

The fact that this LED strip has individually addressable RGB LED’s with a well supported Arduino Library makes it very useful.

Since the library is relatively expressive it allows simple to very complex patterns to be programmed in. In the above example, I have shown a simple colour changing program. In the future, I will code some more interesting patterns to share them.

I would definitely, and am looking to, buy more of this in the future due to the wide range of uses.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.