Addressable LED Strip Hookup Guide

Pages
Contributors: Nick Poole
Favorited Favorite 11

Example Code

Once your LED strip is wired up, you can load some Arduino code onto the RedStick to animate the lights! For our example code, we'll be making use of Adafruit's fantastic NeoPixel library.

Click here to download a copy of both the example code, as well as the NeoPixel library. The library is located in the "Adafruit_NeoPixel" folder, and the example code is found in the "WS2812_Breakout_Example" folder.

You'll need to install the library. For help there, check out our installing Arduino libraries tutorial.

We've broken down the example code into a few separate sketches that each have a fun animation. We've taken the liberty of cooking up a few special addition animations as well:

Rainbow Cycle

This one is my favorite of the NeoPixel example animations. It scrolls through the entire rainbow of colors while evenly distributing the color spectrum across the LED strip.

language:c
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
  #include <avr/power.h>
#endif

#define PIN 2

Adafruit_NeoPixel strip = Adafruit_NeoPixel(60, PIN, NEO_GRB + NEO_KHZ800);

void setup() {
  strip.begin();
  strip.setBrightness(64);
  strip.show(); // Initialize all pixels to 'off'
}

void loop() {
rainbowCycle(20);
}

void rainbowCycle(uint8_t wait) {
  uint16_t i, j;

  for(j=0; j<256*5; j++) { // 5 cycles of all colors on wheel
    for(i=0; i< strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
    }
    strip.show();
    delay(wait);
  }
}

// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
  WheelPos = 255 - WheelPos;
  if(WheelPos < 85) {
    return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  }
  if(WheelPos < 170) {
    WheelPos -= 85;
    return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
  WheelPos -= 170;
  return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}

Candy Chase

This is a little animation that I worked up based on the "Theater Chase" animation included in the NeoPixel example code. It creates a crawling lights effect in red on a white background, like an animated candy cane!

language:c
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
  #include <avr/power.h>
#endif

#define PIN 2

Adafruit_NeoPixel strip = Adafruit_NeoPixel(60, PIN, NEO_GRB + NEO_KHZ800);

void setup() {
  strip.begin();
  strip.setBrightness(64);
  strip.show(); // Initialize all pixels to 'off'
}

void loop() {
candyChase(100);
}

void candyChase(uint8_t wait) {
  for (int j=0; j<10; j++) {  //do 10 cycles of chasing
    for (int q=0; q < 3; q++) {
      for (uint16_t i=0; i < strip.numPixels(); i++) {
        strip.setPixelColor(i+q, 255,255,255);    //turn every pixel white
      }
      for (uint16_t i=0; i < strip.numPixels(); i=i+3) {
        strip.setPixelColor(i+q, 255,0,0);    //turn every third pixel red
      }
      strip.show();

      delay(wait);

      for (uint16_t i=0; i < strip.numPixels(); i=i+3) {
        strip.setPixelColor(i+q, 0);        //turn every third pixel off
      }
    }
  }
}

Snowflakes

This animation creates an array of randomly twinkling white pixels reminiscent of snowfall.

language:c
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
  #include <avr/power.h>
#endif

#define PIN 2

Adafruit_NeoPixel strip = Adafruit_NeoPixel(60, PIN, NEO_GRB + NEO_KHZ800);

void setup() {
  strip.begin();
  strip.setBrightness(64);
  strip.show(); // Initialize all pixels to 'off'
}

void loop() {
    snowflakes(100);
}

void snowflakes(uint8_t wait) {

// Setup the pixel array
int pixel[60];
for(int p=0; p<60; p++){
  pixel[p] = random(0,255); 
}

// Run some snowflake cycles
for (int j=0; j<200; j++) {

// Every five cycles, light a new pixel
if((j%5)==0){
  strip.setPixelColor(random(0,60), 255,255,255);
}

// Dim all pixels by 10
for(int p=0; p<60; p++){
  strip.setPixelColor(p, pixel[p],pixel[p],pixel[p] );
  pixel[p] =  pixel[p] - 10;
}
   strip.show();
   delay(wait);
}

}

Iceflakes

Not a real thing, I know... It's "Snowflakes" but blue instead of white.

language:c
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
  #include <avr/power.h>
#endif

#define PIN 2

Adafruit_NeoPixel strip = Adafruit_NeoPixel(60, PIN, NEO_GRB + NEO_KHZ800);

void setup() {
  strip.begin();
  strip.setBrightness(64);
  strip.show(); // Initialize all pixels to 'off'
}

void loop() {
    iceflakes(100);
}

void iceflakes(uint8_t wait) {

// Setup the pixel array
int pixel[60];
for(int p=0; p<60; p++){
  pixel[p] = random(0,255); 
}

// Run some snowflake cycles
for (int j=0; j<200; j++) {

// Every five cycles, light a new pixel
if((j%5)==0){
  strip.setPixelColor(random(0,60), 0,0,255);
}

// Dim all pixels by 10
for(int p=0; p<60; p++){
  strip.setPixelColor(p, 0,0,pixel[p] );
  pixel[p] =  pixel[p] - 10;
}
   strip.show();
   delay(wait);
}

}