Using Brickstuff BrickPixel Controllers with the Adafruit NeoPixel Library in Arduino

(NOTE: this post is intended for experienced Arduino programmers.  We cannot provide specific programming advice or any sample code beyond what it provided here.  If you would like to learn more about how NeoPixels work, Adafruit has published an excellent, in-depth guide here.)

Since the release of our lighting kit for the LEGO Ferris Wheel several years ago, we have used a lighting control technology based on the WS281x protocol commonly found in individually-addressable RGB LEDs known generally as NeoPixels.  Using the WS281x LED control technology, it is possible to control tens or even hundreds of LEDs with a single controller.

We have created our own LED controller boards that use the WS2811 control chip.  These boards have a 3-wire input connector and individual Pico LED connectors for each output.  Each output can control a single LED.  Many of the adapter boards have a 3-wire output connector as well, which allows boards and LEDs to be daisy-chained together in a logical string, while still being controlled by a single "master" controller.

Because our controller boards use the same technology as the NeoPixel LEDs, you can use the Adafruit NeoPixel library to control Brickstuff Pico LEDs as well.  You can control individual brightness of each LED, and you can also write your own lighting effects inside the Arduino IDE.

Unlike traditional NeoPixels, however, our controllers manage individual LEDs.  This means you can't use the Adafruit library directly, since that library was written to manage RGB LEDs (which are actually three individual LEDs-- red, green, and blue-- inside a single LED case).  Don't worry, though!  We've created a small little bit of code you can seamlessly drop into any Arduino sketch to give you full control of any BrickPixel LEDs in your chain.  Read on to learn how.

 

Installing the Adafruit Library

First, make sure you have the Arduino IDE installed and that you have also installed the Adafruit NeoPixel library (see their excellent Überguide for instructions).

Next, create a new Arduino sketch with the core elements of the NeoPixel library included:

#include <Adafruit_NeoPixel.h>
// Which pin on the Arduino is connected to the NeoPixels?
#define LED_PIN    6

// How many NeoPixels are attached to the Arduino?
#define LED_COUNT 10

// Declare our NeoPixel strip object:
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
// Argument 1 = Number of pixels in NeoPixel strip
// Argument 2 = Arduino pin number (most are valid)
// Argument 3 = Pixel type flags, add together as needed:
//   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
//   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
//   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
//   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
//   NEO_RGBW    Pixels are wired for RGBW bitstream (NeoPixel RGBW products)

(the code above was copied from the Adafruit Überguide)

 

Additional BrickPixel Code

In addition to the base NeoPixel code above, you will need to add a few more variable declarations before the setup() block in your code:

int pixelMax = 30;  // Maximum number of LEDs (not pixel boards) in a string
int numPixels = 10; // Total number of pixel boards (not individual LEDs) in a string (is calculated by dividing pixelMax by 3 and adding one if there's a remainder)

int pixelBright[xx];    // Change "xx" to be the same as your numPixels setting

Looking at the code sample above, pixelMax should be set to the number of total LEDs in your chain.  The example above has pixelMax set to 30, which means there are 30 total individual LEDs in your string.

Next is the numPixels setting.  This should refer to the total number of NeoPixel control chips in your setup.  One control chip has three individual LED outputs, so to get the total for numPixels, just divide pixelMax by three and add one if there is a remainder.  numPixels should also be the same as the LED_COUNT setting in your NeoPixel setup code (remember, the LED_COUNT number refers to the number of RGB LEDs, and just like with our control chips, each RGB LED represents three physical LEDs).

Once you have configured numPixels and pixelMax, you can define the variable pixelBright as an array with the same number of members as your numPixels setting.

pixelBright is where all the magic happens.  Each element in the array refers to a single LED in your chain.  Array element 0 controls LED #1 and so on.

In addition to the code elements above, which should all be placed in your Arduino sketch above the setup() block, you need to add one subroutine after the end of your loop() block.  This is the code to add:

void uStrip()
{
  // Refreshes the entire strip
  int subPixel;

  for(int i=0; i<numPixels; i++)
  {
    strip.setPixelColor(i,strip.Color(pixelBright[subPixel], pixelBright[(subPixel+1)], pixelBright[(subPixel+2)]));
    subPixel++;
    subPixel++;
    subPixel++;
  }
    strip.show();
    subPixel = 0;
}

 This subroutine handles the translation of single-color LEDs into RGB-like code that the NeoPixel library is designed to understand.

 

Controlling LEDs

To set the brightness of any LED in your chain, set the element of pixelBright corresponding to that LED to any value between 0 (off) and 255 (full brightness).

For example, to set the brightness of the 10th LED in your chain to 50%, you would type:

pixelBright[9] = 128;

If you wanted to set the brightness of a range of LEDs in your string, you could use a for/next loop.  For example, to set the brightness of LEDs 10-20 to 100% brightness, you would type:

for(int i=9; i<20; i++)

{

    pixelBright[i] = 255;

}

Once you have set the brightness for each LED you want to change or set using the appropriate elements of the pixelBright array, you should call the uStrip() subroutine:

uStrip();

This will update all variables in the NeoPixel object and refresh the strip.  When using uStrip(), you do not need to call strip.show() as outlined in the Adafruit guide.

That's pretty much all there is to it.  Using the variety of Brickstuff BrickPixel adapters, controller boards, and cables, you can add lights to your model and control them all from your Arduino board.

Reply Oldest first
  • Oldest first
  • Newest first
  • Active threads
  • Popular
Like1 Follow
  • 1 Likes
  • 4 mths agoLast active
  • 29Views
  • 1 Following