WS2812B: the magic RGB LED strip

WS2812B RGB LED strip

You probably need to add some color to your DIY projects. For this purpose, many makers use the famous WS2812B RGB LED strips, with which you can achieve varied color control and quite attractive lighting effects for your projects. Of course they are fully compatible with Arduino boards, so you will have no problem when trying to integrate them.

You can find them in lengths of 1 meter, for example, although they can vary depending on the type of density of LEDs per meter they have. For example, there are from 30 LEDs to 144 LEDs. However, if you need longer lengths to obtain a larger surface, you also have other options on the market such as RGB panels of LEDs or you can always use several strips .

These strips are nothing more than a series of RGB LEDs assembled and mounted on a common support to group them into strips. But their operation is identical to the separate RGB LEDs.

  • More information about RGB LEDs

What is WS2812B?

lighted RGB LED strip WS2812B

Actually the WS2812B is not the strip itself, but each of the cells or small RGB LED plates it includes. They can be grouped in strip or panel form, so you can make various settings as to their number and shape. The strips that make up these are flexible strips, but you can also find WS2812B on PCB panels that are not.

If you want, you can find the WS2812B devices separately to create the shapes you need yourself. For example, about 100 units of them usually cost a little more than 10

cutting led strips rgb

You should also know that you can cut the strips with a scissor wherever you need to, but that doesn’t mean that they stop working. So you can have only the RGB LEDs you need. In fact, it has some marks (three copper pads) that you can cut through. If you cut through these pads you will have three tracks left on one side and on the other side of the strip in case you want to reuse the other pieces, you can solder the pins to them for easy connection.

Pinout and datasheet

WS2812B in a single cell
It is an independent WS2812B cell with its inputs and outputs

For more information about your WS2812B>Strong> RGB LED strip you can read the datasheet offered by each manufacturer, there you can consult all the details of dimensions and technical characteristics to know how to use them properly, as well as knowing all the operating ranges and limits.

As for the pinout, it’s not a big problem either, these strips have a simple connection that you can master from the beginning without too much knowledge. There are only three available, although each WS2812B cell actually has more connections…

You simply have to connect on each strip the Vcc pin that feeds the strip to the 5V of Arduino or from a different power source, the GND to ground of course, and finally the DI which is the other one that will go to any output of the microcontroller to activate the RGB LEDs of the strip.

If you look at a WS2812B cell you will see that it has the Data In or DI inputs, and the Vcc and GND inputs for power. Then it will have another three outputs, those will be connected to the next cell in the strip, and the next cell will have its outputs connected to the input of the next one and so on until the whole strip is completed…

It is precisely that DI or data input that is interesting to configure the RGB LEDs, and that same one will be connected to Data Out or DO that will take that same information to the next link of the strip. And so it is propagated throughout the strip.

Buy WS2812B RGB LED strip
Roll of RGB LED strip

You can buy them at a low price in several specialized shops. You can also find them in different formats on Amazon. Some examples are:

Tests with Arduino and WS2812B

Arduino UNO with WS2812B

As you can imagine, with only three pins it’s very easy to connect to Arduino as you can see in the diagram above. You only have to connect 5v and GND to the WS2812B strip, and the DI to an output you want in Arduino. Remember that if you change pins you must also modify the source code for the program to work properly.

[highlighted]The FAST-LED Master library has been used to make things easier and to obtain simple functions to handle RGB LEDs. To download it and integrate it properly into the Arduino IDE, just download the library from that link, then unzip it and rename the unzipped directory or folder as FastLED, and then move that folder to where the Arduino IDE libraries are installed in your installation. Then reopen Arduino IDE and you are ready… [/highlighted]

As for the sketch code, it can be quite simple as the following code. If you don’t want to copy and paste, you can find it among the examples that come already. So, go to File > Examples > FastLED > ColorPalette.

Remember to change pin 14 in the example to pin 5 which is the one I used in the schematic. Or you can connect to pin 14 and avoid modifying the code. As you prefer. [/highlighted]


#FastLED.h

#define LED_PIN 5
#define NUM_LEDS 14
#define BRIGHTNESS 64
#define LED_TYPE WS2811
#define COLOR_ORDER GRB
CRGB leds [NUM_LEDS];

#define UPDATES_PER_SECOND 100

// This example shows several ways to set up and use 'palettes' of colors
// with FastLED.
//
// These compact palettes provide an easy way to re-color your
// animation on the fly, quickly, easily, and with low overhead.
//
// USING palettes is MUCH simpler in practice than in theory, so first just
// run this sketch, and watch the pretty lights as you then read through
// the code.  Although this sketch has eight (or more) different color schemes,
// the entire sketch compiles down to about 6.5K on AVR.
//
// FastLED provides a few pre-configured color palettes, and makes it
// extremely easy to make up your own color schemes with palettes.
//
// Some notes on the more abstract 'theory and practice' of
// FastLED compact palettes are at the bottom of this file.



CRGBPalette16 currentPalette;
TBlendType currentBlending;

external CRGBPalette16 myRedWhiteBluePalette;
external const TProgmemPalette16 myRedWhiteBluePalette_p PROGMEM;


void setup() {
    delay( 3000 ); // power-up safety delay
    FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER ;(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
    FastLED.setBrightness( BRIGHTNESS );
    
    currentPalette = RainbowColors_p;
    currentBlending = LINEARBLEND;
}


void loop()
{
    ChangePalettePeriodically();
    
    static uint8_t startIndex = 0;
    startIndex = startIndex + 1; /* motion speed */
    
    FillLEDsFromPaletteColors( startIndex);
    
    FastLED.show();
    FastLED.delay(1000 / UPDATES_PER_SECOND);
}

void FillLEDsFromPaletteColors( uint8_t colorIndex)
{
    uint8_t brightness = 255;
    
    for( int i = 0; i < NUM_LEDS; i++) {
        leds[i] = ColorFromPalette( currentPalette, colorIndex, brightness, currentBlending);
        colorIndex += 3;
    }
}


// There are several different palettes of colors demonstrated here.
//
// FastLED provides several 'preset' palettes: RainbowColors_p, RainbowStripeColors_p,
// OceanColors_p, CloudColors_p, LavaColors_p, ForestColors_p, and PartyColors_p.
//
// Additionally, you can manually define your own color palettes, or you can write
// code that creates color palettes on the fly.  All are shown here.

void ChangePalettePeriodically()
{
    uint8_t secondHand = (millis() / 1000) % 60;
    static uint8_t lastSecond = 99;
    
    if( lastSecond != secondHand) {
        lastSecond = secondHand;
        if( secondHand == 0) { currentPalette = RainbowColors_p; currentBlending = LINEARBLEND; }
        if( secondHand == 10) { currentPalette = RainbowStripeColors_p; currentBlending = NOBLEND; }
        if( secondHand == 15) { currentPalette = RainbowStripeColors_p; currentBlending = LINEARBLEND; }
        if( secondHand == 20) { SetupPurpleAndGreenPalette(); currentBlending = LINEARBLEND; }
        if( secondHand == 25) { SetupTotallyRandomPalette(); currentBlending = LINEARBLEND; }
        if( secondHand == 30) { SetupBlackAndWhiteStripedPalette(); currentBlending = NOBLEND; }
        if( secondHand == 35) { SetupBlackAndWhiteStripedPalette(); currentBlending = LINEARBLEND; }
        if( secondHand == 40) { currentPalette = CloudColors_p; currentBlending = LINEARBLEND; }
        if( secondHand == 45) { currentPalette = PartyColors_p; currentBlending = LINEARBLEND; }
        if( secondHand == 50) { currentPalette = myRedWhiteBluePalette_p; currentBlending = NOBLEND; }
        if( secondHand == 55) { currentPalette = myRedWhiteBluePalette_p; currentBlending = LINEARBLEND; }
    }
}

// This function fills the palette with totally random colors.
void SetupTotallyRandomPalette()
{
    for( int i = 0; i < 16; i++) {
        currentPalette[i] = CHSV( random8(), 255, random8());
    }
}

// This function sets up a palette of black and white stripes,
// using code.  Since the palette is effectively an array of
// sixteen CRGB colors, the various fill_* functions can be used
// to set them up.
void SetupBlackAndWhiteStripedPalette()
{
    // 'black out' all 16 palette entries...
    fill_solid( currentPalette, 16, CRGB::Black);
    // and set every fourth one to white.
    currentPalette[0] = CRGB::White;
    currentPalette[4] = CRGB::White;
    currentPalette[8] = CRGB::White;
    currentPalette[12] = CRGB::White;
    
}

// This function sets up a palette of purple and green stripes.
void SetupPurpleAndGreenPalette()
{
    CRGB purple = CHSV( HUE_PURPLE, 255, 255);
    CRGB green = CHSV( HUE_GREEN, 255, 255);
    CRGB black = CRGB::Black;
    
    currentPalette = CRGBPalette16(
                                   green, green, black, black,
                                   purple, purple, black, black,
                                   green, green, black, black,
                                   purple, purple, black, black );
}


// This example shows how to set up a static color palette
// which is stored in PROGMEM (flash), which is almost always more
// plentiful than RAM.  A static PROGMEM palette like this
// takes up 64 bytes of flash.
const TProgmemPalette16 myRedWhiteBluePalette_p PROGMEM =
{
    CRGB::Network,
    CRGB::Gray, // 'white' is too bright compared to red and blue
    CRGB::Blue,
    CRGB::Black,
    
    CRGB::Network,
    CRGB::Gray,
    CRGB::Blue,
    CRGB::Black,
    
    CRGB::Network,
    CRGB::Network,
    CRGB::Gray,
    CRGB::Gray,
    CRGB::Blue,
    CRGB::Blue,
    CRGB::Black,
    CRGB::Black
};



// Additionl notes on FastLED compact palettes:
//
// Normally, in computer graphics, the palette (or "color lookup table")
// has 256 entries, each containing a specific 24-bit RGB color.  You can then
// index into the color palette using a simple 8-bit (one byte) value.
// A 256-entry color palette takes up 768 bytes of RAM, which on Arduino
// is quite possibly "too many" bytes.
//
// FastLED does offer traditional 256-element palettes, for setups that
// can afford the 768-byte cost in RAM.
//
// However, FastLED also offers a compact alternative.  FastLED offers
// palettes that store 16 distinct entries, but can be accessed AS IF
// they actually have 256 entries; this is accomplished by interpolating
// between the 16 explicit entries to create fifteen intermediate palette
// entries between each pair.
//
// So for example, if you set the first two explicit entries of a compact
// palette to Green (0,255,0) and Blue (0,0,255), and then retrieved
// the first sixteen entries from the virtual palette (of 256), you'd get
// Green, followed by a smooth gradient from green-to-blue, and then Blue.


Leave a Comment

*

*