Example Code for Arduino - Light Up WS2812 Light Strip

Last revision 2026/02/05

WS2812 is a type of single-wire controlled light strip. Each LED chip is integrated with a control chip, allowing independent control of each LED's on/off state and color. This enables the creation of cool lighting effects. The DMX512 controller has reserved two IO ports, IO4 and IO5, which can be used to drive WS2812 light strips.

Note: WS2812 light strips have a relatively high power consumption and require external 5V power supply. If only the controller is powered, it may cause insufficient power supply and even damage the controller's power chip.

Hardware Connection

Hardware Connection

Example 1: WS2812 Rainbow Light

WS2812 Light Strip Library Download Link: https://github.com/adafruit/Adafruit_NeoPixel

#include <Adafruit_NeoPixel.h>

#define LED_PIN     4 //Set the pin number for connection.

#define LED_COUNT  137 //Set the total number of LEDs.

#define BRIGHTNESS 255 //Set the brightness, maximum 255.

Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRBW + NEO_KHZ800);

void setup() {
    strip.begin();           
    strip.show();            
    strip.setBrightness(BRIGHTNESS);
    for(int i=0; i<strip.numPixels(); i++) { 
        uint32_t pixelHue = 40 + (i * 65536L / strip.numPixels());
        strip.setPixelColor(i, strip.gamma32(strip.ColorHSV(pixelHue, 255,255 )));         
    }
    strip.show(); 
}

void loop() {
    delay(1);

}

Result: The light strip displays in rainbow colors.

Example 2: WS2812 Flowing Light

#include <Adafruit_NeoPixel.h>

#define LED_PIN     4

#define LED_COUNT  137 

#define BRIGHTNESS 255 

Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRBW + NEO_KHZ800);
void setup() {
    strip.begin();           
    strip.show();            
    strip.setBrightness(BRIGHTNESS);
}

void loop() {
    for(uint32_t firstPixelHue = 0; firstPixelHue < 3*65536;
        firstPixelHue += 256) {

            for(int i=0; i<strip.numPixels(); i++) { 
                uint32_t pixelHue = firstPixelHue + (i * 65536L / strip.numPixels());
                strip.setPixelColor(i, strip.gamma32(strip.ColorHSV(pixelHue, 255,
                    255)));
            }

            strip.show();
            delay(3);
        }

}

Result: The light strip flows dynamically like water.

Example 3: WS2812 Breathing Light

#include <Adafruit_NeoPixel.h>

#define LED_PIN     4

#define LED_COUNT  137

#define BRIGHTNESS 255

Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRBW + NEO_KHZ800);
void setup() {
    strip.begin();           
    strip.show();          
    strip.setBrightness(BRIGHTNESS);
}

void loop() {
    for(int j=0; j<256; j++) { 
        strip.fill(strip.Color(0, 0, 0, strip.gamma8(j)));
        strip.show();
        delay(5);
    }

    for(int j=255; j>=0; j--) { 
        strip.fill(strip.Color(0, 0, 0, strip.gamma8(j)));
        strip.show();
        delay(5);
    }

}

Result: The light strip fades in and out like breathing.

Example 4: WS2812 Running Light

#include <Adafruit_NeoPixel.h>

#define LED_PIN     4

#define LED_COUNT  137

#define BRIGHTNESS 255

Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRBW + NEO_KHZ800);

int      head          = 10 - 1;
int      tail          = 0;
uint32_t lastTime      = millis();
uint32_t firstPixelHue = 0;
int whiteSpeed = 20;

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

void loop() {
    for(int i=0; i<strip.numPixels(); i++) {  
        if(((i >= tail) && (i <= head)) ||      
            ((tail > head) && ((i >= tail) || (i <= head)))) {
            strip.setPixelColor(i, strip.Color(0, 0, 0, 255)); 
        } else {                                             
            int pixelHue = firstPixelHue + (i * 65536L / strip.numPixels());
            strip.setPixelColor(i, strip.gamma32(strip.ColorHSV(pixelHue)));
        }
    }

    strip.show(); 
    firstPixelHue += 40; 

    if((millis() - lastTime) > whiteSpeed) { 
        if(++head >= strip.numPixels()) {      
            head = 0;
        }
        if(++tail >= strip.numPixels()) {      
            tail = 0;
        }
        lastTime = millis();                   
    }

    if(firstPixelHue > 3 * 65536)
        firstPixelHue = 0;

}

Result: The light strip cycles dynamically like a racetrack.

Was this article helpful?

ON THIS PAGE

TOP