Getting started with NeoPixels
Getting started with NeoPixels
2. 8-LED stick
This LED Stick is a small chainable board with eight 5050 WS2812 RGB LEDs. They make great bargraph indicators!
A NeoPixel is just a name that Adafruit uses to refer to a bunch of different RGB (and RGBW) individually addressable LEDs. We’re using a stick that is using a WS2812 RGB individually addressable LED. The LED itself has three inputs and three outputs. Power, ground, and data are the three inputs, and they are passed through to the next LED in the strip, allowing you to set the Red, Green, and Blue intensities for each LED in the strip individually. The data signal to control the LEDs brightness is timing dependent, and a little complex to generate. Luckily for us, the folks over at Adafruit wrote a nice library that takes care of all that complicated logic and makes it super simple for us to create cool light patterns.
Define variables
Next, we need to
#define
a couple of constants; PIN is the pin we have the NeoPixel data line
connected to, and NUM_LEDS is the length of the NeoPixel strip. Once we
have that, we can create an instance of the Adafruit_NeoPixel called pixels, that uses the PIN and NUM_LEDS values defined earlier. The final
parameter is a value to indicate the type of NeoPixel that’s being used.
The WS2812 LEDs that we use are :NEO_GRB + NEO_KHZ800
// Which pin on the Arduino is connected to the NeoPixels? #define PIN 2 // How many NeoPixels are attached to the Arduino? #define NUMPIXELS 8 // When we setup the NeoPixel library, we tell it how many pixels, and which pin to use to send signals. // Note that for older NeoPixel strips you might need to change the third parameter--see the strandtest // example for more information on possible values. Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
Our setup() function
We then need to called the begin()
method on our NeoPixel strip to set it up. Any time we make changes to the strip, we need to send them to the strip using the show()
method. This allows us to queue up all of the pixel changes and then
push them out all at once. Since we haven’t set any strip pixels, our
call to show()
sets all of the LEDs to be off.
void setup() {
pixels.begin(); // This initializes the NeoPixel library.
pixels.show();}
The loop() function
void loop() { // For a set of NeoPixels the first NeoPixel is 0, second is 1, all the way up to the count of pixels minus one. for(int i=0;i<NUMPIXELS;i++){ // pixels.Color takes RGB values, from 0,0,0 up to 255,255,255 pixels.setPixelColor(i, pixels.Color(0,100,0)); // Moderately bright green color. pixels.show(); // This sends the updated pixel color to the hardware. delay(1000); // Delay for a period of time (in milliseconds). }.ino file
}