ACROBOTIC Industries ACROBOTIC Industries

ESP8266: Controlling WS2812B RGB LEDs (NeoPixels)

Overview

Time to complete: 10–15min; Level of difficulty: Beginner

In this activity we’ll use an ESP8266 ESP-12E Development Board to individually control the color and brightness of an array of WS2812B RGB LEDs.

List of Materials


Obtaining the Code

For this activity, we'll be using the wonderful WS2812/NeoPixels Arduino Library developed by Github user Makuna.  From the many alternatives, this is the one that works the best for our ESP8266 Development Board; the library's source code can be found at:

We'll use a simple process to install this library and make it available for use in our example programs below.


Installing the Library

There are a few different ways to install a library in the Arduino IDE.  For detailed, step-by-step instructions on all the different ways to do so please visit our tutorial.  The simplest method is to use the built-in library manager in the Arduino IDE. It can be accessed via the menu option Sketch → Include Library → Manage Libraries…

In the search field we can enter "Makuna NeoPixel" and install the resulting option:

Install NeoPixelBus WS2812 Library in Arduino IDE

Before diving into the code, let's review a bit the operation of WS2812 RGB LEDs, and how their brightness and color are controlled.


How WS8212 RGB LEDs Work

For a detailed explanation about how WS2812 RGB LEDs (aka NeoPixels) work, please visit our neat tutorial!


Wiring: NeoPixelTest

One of the most attractive features of the WS2812 is that all we need to control them is a very simple circuit, which provides Power, Ground, and 1 Control Input in order to create colorful lighting displays.  The Control Input is used to send the color data to each LED that needs to be controlled.  

As seen in the previous step, the data is transmitted by changing the HIGH and LOW state of the Control Input pin using very a precise (and fast!) timing schedule.  There are different methods for changing between these HIGH and LOW states, and the one that works best on the ESP8266 is by using its built-in (hardware) functionality I2S.  For more details you can read over the library's wiki: https://github.com/Makuna/NeoPixelBus/wiki/NeoPixelBus-object

In order to use I2S to send data to the LEDs, we have to use the RX pin on our ESP8266 development board.  If you have a different board or ESP8266 module, you might need to disconnect this data line in order to re-program the chip.  Luckily, this is not necessary using our board:

Wiring Diagram For WS2812 Strip And ESP8266 Development Board

A trick to easily wire these LED strips to the ESP8266 Development Board is to use Male/Male jumper wires with one end plugged into each of the pins of the female (Data In) JST connector of the strip, and the other end plugged into the solderless breadboard!

Once we've connected the strip as shown in the diagram, we're ready to upload code to the board that will allow us to control the color and brightness of each NeoPixel WS2812B RGB LED!


Code Walkthrough: NeoPixelTest

To ensure that everything's working correctly, we use the example code NeoPixelTest that comes with the NeoPixelBus library.  As usal, we can find this example via the menu option File → Examples → NeoPixelBus by Makuna → NeoPixelTest

NeoPixelBus Library Example

Once we've open the example code, we typically would change the of const uint16_t PixelCount = 4; to match the number of 'NeoPixels' (LEDs) that we have connected (60 in our case).  However, this test code only uses 4 LEDs so it's okay to use the default value.

 

#include 

const uint16_t PixelCount = 4; // this example assumes 4 pixels, making it smaller will cause a failure
const uint8_t PixelPin = 2;  // make sure to set this to the correct pin, ignored for Esp8266

#define colorSaturation 128

// three element pixels, in different order and speeds
NeoPixelBus<NeoGrbFeature, Neo800KbpsMethod> strip(PixelCount, PixelPin);

The first section of the code includes the NeoPixelBus library so that it's available in the code.  Also, it includes the definitions of different constant values that we'll be using later.  The colorSaturation is the desired brightness values for the LEDs, it can range from 0 (fully off) to 255 (fully on) and it's set by default to 50%.

Note that depending on your specific NeoPixel strip manufacturer, the order of the individual LEDs inside each 'pixel' might be different.  If the colors do not match you can simply change the last line above to:

NeoPixelBus<NeoRgbFeature, Neo400KbpsMethod> strip(PixelCount, PixelPin);

The next section of the code defines the different colors that will be used later in the code.  It uses a specific data structure as defined in the NeoPixelBus library:

RgbColor red(colorSaturation, 0, 0);
RgbColor green(0, colorSaturation, 0);
RgbColor blue(0, 0, colorSaturation);
RgbColor white(colorSaturation);
RgbColor black(0);

HslColor hslRed(red);
HslColor hslGreen(green);
HslColor hslBlue(blue);
HslColor hslWhite(white);
HslColor hslBlack(black);

After all the constants definitions, we find the setup function where a few debug messages are printed (using the Serial object as it's typical), and the communication to the NeoPixels is started.  The Begin() method initializes the communication as well as sets in memory the brightness values to 0 for all the NeoPixels (as many as we have defined in PixelCount).  The Show() method then sends the data from memory to the physical LEDs.

    // this resets all the neopixels to an off state
    strip.Begin();
    strip.Show();

Finally, the loop() function sends the color data to the first four LEDs.  Note that this process consists of first setting the different colors in memory using the SetPixelColor() method, and then it sends it to the physical NeoPixels over the data line using the aforementioned Show() method.

    strip.SetPixelColor(0, red);
    strip.SetPixelColor(1, green);
    strip.SetPixelColor(2, blue);
    strip.SetPixelColor(3, white);
    strip.Show();

The code makes the NeoPixels hold their color for 5 seconds, then turns them off by setting the color to black, and then turns them on to the colors defined using the HslColor definitions!  As a result we see the first four NeoPixels blink on and off to their respective colors (red, green, blue, and white).


Making Custom Animations

As we can see, it's straightforward to set the colors of all the NeoPixels connected to our microcontroller/SoC, and display them using the SetPixelColor() and Show() functions of the NeoPixelBus library.  We now can use our imagination to come up with ideas for creating custom animations we'd like to see display.

ESP8266 Development Board with WS2812B Strip

The rest of the examples in the NeoPixelBus library are a good start to see different methods to create animations (e.g., NeoPixelCylon), but we also wanted to show you how to create a "rainbow" pattern where each LED has a different color covering the entire color space.  We've posted this code example in our Github repository:

You can copy and paste the code from the .ino file located inside the Rainbow folder.


Code Walkthrough: Rainbow

The initial constants and setup() function are similar to the NeoPixelTest example.  The code that generates the animation is split between the loop() function and a custom (user-defined) function called Wheel(). The Wheel() function takes in a value from 0 to 255 and returns a color comprised by red, gren, and blue values.  The way the color is chosen is by splitting the color space in 3 segments:

  • decreasing red (255 to 0), no green (0), increasing blue (0 to 255)
  • no red (0), increasing green (0 to 255), decreasing blue (255 to 0)
  • increasing red (0 to 255), decreasing green (255 to 0), no blue (0)

Notice that in each of the three segments, one color is set to off, one color goes from full brightness to zero, whereas the third goes from zero to full brightness.  The two colors that are on are mixed as their brightness increase/decrease.

// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
RgbColor Wheel(uint8_t WheelPos) 
{
  WheelPos = 255 - WheelPos;
  if(WheelPos < 85) 
  {
    return RgbColor(255 - WheelPos * 3, 0, WheelPos * 3);
  } else if(WheelPos < 170) 
  {
    WheelPos -= 85;
    return RgbColor(0, WheelPos * 3, 255 - WheelPos * 3);
  } else 
  {
    WheelPos -= 170;
    return RgbColor(WheelPos * 3, 255 - WheelPos * 3, 0);
  }
}

If we follow each segment in the order listed above waiting for the values to transition fully, we see the following behavior: in the first segment we go from full red to full blue (across the red-blue colorspace), then in the second we go from full blue to full green (across the blue-green colorspace), and in the last we go from full green back to full red (across the green-red colorspace).  This provides a convenient way to navigate the entire colorspace.

So the only problem is, how do we assign each NeoPixel a different value?

Because of the way the Wheel() function works, we have to use the numbers between 0 and 255 to choose all the different colors.  The simplest case would be having 256 NeoPixels, in which case we'd assign the color value corresponding to 0 to the 1st NeoPixel (red as this is the color returned by Wheel(0)), and the color value corresponding to 255 to the last NeoPixel (also red as this is the color returned by Wheel(255)).

Once we'd assign all the different color values and displayed them using the Show() function, we could add 1 to all of them so we'd see the colors "traveling" along the length of the strip!  Of course, we'd need to ensure that the last NeoPixel whose value would be 256 (255+1) rolls back to 0 so that the Wheel() function is able to return the correct value.

What we've explained above can be implemented using two for loops, an outer loop for "shifting" the values down the length of the strip, and the inner loop for assigning the different colors to each NeoPixel on the strip.  With a little arithmetic trickery, we can also account for cases where the number of NeoPixels are not 256.

  for(uint16_t j=0; j<256*5; j++) // complete 5 cycles around the color wheel
  { 
    for(uint16_t i=0; i<PixelCount; i++)
    {
      // generate a value between 0~255 according to the position of the pixel
      // along the strip
      pos = ((i*256/PixelCount)+j) & 0xFF;
      // calculate the color for the ith pixel
      color = Wheel( pos );
      // set the color of the ith pixel
      strip.SetPixelColor(i, color);
    }
    strip.Show();
    delay(50);
  } 

 


Comments, questions, or concerns? Drop us a line!.