NeoPixel Rainbow (WS2812 + Arduino)
Animate a flowing rainbow across an addressable LED strip. Learn the Adafruit_NeoPixel library and HSV color.
Beginner20 min
What you need
- · 330Ω resistor
- · 1000µF capacitor (for long strips)
- · jumper wires
Wiring
Hover a pin or a wire to trace the connection.
Code
neopixel_rainbow.ino
// Flowing rainbow across a NeoPixel (WS2812) strip.
// Install "Adafruit NeoPixel".
#include <Adafruit_NeoPixel.h>
const int PIN = 6;
const int NUM = 8; // set to your strip's LED count
Adafruit_NeoPixel strip(NUM, PIN, NEO_GRB + NEO_KHZ800);
void setup() {
strip.begin();
strip.setBrightness(40); // keep current draw sane
strip.show();
}
void loop() {
for (long j = 0; j < 65536; j += 256) {
for (int i = 0; i < NUM; i++) {
long hue = (i * 65536L / NUM + j) & 0xFFFF;
strip.setPixelColor(i, strip.gamma32(strip.ColorHSV(hue)));
}
strip.show();
delay(20);
}
}
Steps
- Wire DIN to D6 through a ~330Ω resistor; 5V and GND to power (external 5V for long strips, shared ground).
- Set NUM to your strip's LED count and install "Adafruit NeoPixel".
- Upload to see a flowing rainbow. Lower setBrightness if the strip draws too much current.