Getting Started with the Adafruit NeoPixels library

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.

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.

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).

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.

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

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