Although Adafruit has an excellent guide on getting started, I thought I would include a short post on using their library for RGB or RGBW strips. This already assumes you’ve got your strip hooked up to an Arduino and have successfully run one of the library’s example sketches. If not, read up on Adafruit’s basic connections tutorial.

After starting a new sketch, the first step is to create an instance of the class by calling its constructor.  Here you declare what Arduino pin you want to output data from, how long your strip is (in pixels), and the type of addressable LEDs you’re using.  The RGBW strand I’m using for this project (see the above photo) was purchased from Adafruit, and uses SK6812RGBW 5050 pixels. If you’re using a different type of strip the last argument will be different. If your strip is flashing or you’re otherwise getting weird results, be sure to check that you have your strip settings right.

#define PIN 6
#define NUM_LEDS 60

Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, PIN, NEO_GRBW + NEO_KHZ800);

Next, we need to initialize the strip. Call strip.begin() to set the output pin and then call strip.show() to clear any colors that might be on the pixels. You can set the strip’s brightness if you’d like it to be dimmer, but it isn’t necessary to get the pixels to work.

#define BRIGHTNESS 50

void setup() {
  strip.setBrightness(BRIGHTNESS);
  strip.begin();
  strip.show();
}

Once that’s done the pixels are up and running! Now we need to send them some data.

To set a pixel’s color, you need to call setPixelColor(). The function can take three different types of color inputs: RGB values, RGBW values, or a hex value in either form. Assigning a white value if you’re using a set of RGB pixels won’t have any affect, so you can safely ignore it. You also need to specify which pixel you want to change. Remember that in C++ (the language Arduino uses) arrays start at an index of 0, so the first pixel is 0 and the last pixel is the number of pixels you have – 1 (e.g. for a strip of 60 pixels, you would address them 0-59).

/*Syntax:
RGB:      strip.setPixelColor(PIXEL, int r, int g, int b);
RGBW:     strip.setPixelColor(PIXEL, int r, int g, int b, int w);
Hex RGB:  strip.setPixelColor(PIXEL, 0xFFFFFF);
Hex WRGB: strip.setPixelColor(PIXEL, 0xFFFFFFFF); */

//These all set pixel #1 to red
strip.setPixelColor(0, 255, 0, 0);
strip.setPixelColor(0, 255, 0, 0, 0);
strip.setPixelColor(0, 0xFF0000);
strip.setPixelColor(0, 0x00FF0000);

//These all set pixel #2 to green
strip.setPixelColor(1, 0, 255, 0);
strip.setPixelColor(1, 0, 255, 0, 0);
strip.setPixelColor(1, 0x00FF00);
strip.setPixelColor(1, 0x0000FF00);

//These all set pixel #3 to blue
strip.setPixelColor(2, 0, 0, 255);
strip.setPixelColor(2, 0, 0, 255, 0);
strip.setPixelColor(2, 0x0000FF);
strip.setPixelColor(2, 0x000000FF);

//These all set pixel #4 to RGB white
strip.setPixelColor(3, 255, 255, 255);
strip.setPixelColor(3, 255, 255, 255, 0);
strip.setPixelColor(3, 0xFFFFFF);
strip.setPixelColor(3, 0x00FFFFFF);

//These both set pixel #5 to true white
strip.setPixelColor(4, 0, 0, 0, 255);
strip.setPixelColor(4, 0xFF000000);

If you want to fill the whole strip, a simple for() loop will suffice. You can call numPixels() to get the number of pixels in the strip. Here is a simple code that fills the strip with green.

for(int i = 0; i<strip.numPixels(); i++){
   strip.setPixelColor(i, 0, 255, 0);
}

The setPixelColor() function changes a pixel’s value in the stored color data, but it doesn’t actually send any information to the strip. The last thing to do is to call strip.show(), which sends all of the data to the NeoPixel strip at once!

If you make any changes to a pixel, you’ll need to call show() again.

And that’s it! A solid understanding of color representation and these few functions should be all you need to start making color patterns with NeoPixels!


Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *

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

Would you like to know more?