Setup
There are a few lines of code required to set up your sketch to use the library. First, call the constructor near the
top of your code (before
#define PIN 4
#define LED_COUNT 8
// Create an instance of the Adafruit_NeoPixel class called "leds".
// That'll be what we refer to from here on...
Adafruit_NeoPixel leds = Adafruit_NeoPixel(LED_COUNT, PIN, NEO_GRB + NEO_KHZ800);
The
and
PIN
LED_COUNT
connected to the first breakout's "DIN" pin and the total number of breakout boards you have linked up.
The rest of the setup is calling the
Setting An LED
Setting an LED with the Adafruit NeoPixel library is a two step process. First, you have to set a pixel color using
the
leds.setPixelColor(position, color)
RGB color-code, or three separate bytes for each color. For example:
leds.setPixelColor(3, 0xFF00FF);
leds.setPixelColor(3, 0xFF, 0x00, 0xFF)
Setting a pixel's color does not equate to that pixel actually turning on though. For that, you need to call
. Note that when this function is called any LED that was previously set will turn on.
leds.show()
The Rest of the Code
After you've gotten the hang of using the NeoPixel library, the remaining parts of the sketch are finding ways to
maniuplate the pixel colors. There are some fun functions in this example code, which create the classic "cylon"
larson scanner, or draw a rainbow. We hope you can adapt them to make even more creative animations.
The included "WS2812_Definitions.h" file defines a huge list of standard colors. In there you'll find anything from
navy to saddle brown to ghost white.
Have fun!
FastLED Alternative Library
Looking for an alternative library? Try checking out the FastLED library for more advanced features. The library
also supports other LED chipsets.
Resources and Going Further
For more information about the WS2812 and WS2812B, check out the resources below:
Breakout Board Schematic (PDF)
Breakout Board Eagle Files (ZIP)
Breakout Board GitHub Repo
Datasheet
, you'll probably want it to be a global variable).
setup()
parameters in that function call should be set, respectively, to the Arduino pin you've
leds.begin()
command. The color parameter in that function can either be a 24-bit
// Set fourth LED to full red, no green, full blue
FASTLED GITHUB REPO
function somewhere near the beginning of the
// Also set fourth LED to full red, no green, full blue
function.
setup()
Need help?
Do you have a question about the WS2812 and is the answer not in the manual?