NeoPixel rainbow ring
Spin a rainbow around a 16-LED NeoPixel ring with the Adafruit NeoPixel library.
Displays · Beginner
In a real product: Smart speaker status ring (Consumer electronics): A ring of LEDs shows listening and volume.
NeoPixels (WS2812 LEDs) each have a tiny controller inside. They are chained: you send all the colours down one data wire, each pixel keeps the first 24 bits it receives and passes the rest on. Sixteen of them make this ring.
What you'll learn
- Addressable LEDs: one data pin for any number of pixels
- Colours as hue, saturation and value (HSV) with
ColorHSV() - Gamma correction, so colours look evenly bright
How it works
DIN goes to pin 6. Each pixel needs 24 bits (green, red, blue), sent as very precise pulses: about 0.4 µs high for a 0 and 0.8 µs for a 1. The library's show() sends them all with interrupts briefly off.
Each frame gives pixel i the hue hue + i × 65536 / 16, so the 16 pixels span the whole colour wheel, and hue moves on a little each time so the rainbow turns. gamma32() corrects for how our eyes see brightness.
Try this
- Make a single bright dot chase around the ring with a fading tail.
- Show a "loading" spinner, or a clock with hour and minute hands in different colours.
- Use a potentiometer to set the speed.
sketch.ino
// A 16-pixel NeoPixel ring on pin 6: a turning rainbow.
#include <Adafruit_NeoPixel.h>
const int PIN = 6;
const int COUNT = 16;
Adafruit_NeoPixel ring(COUNT, PIN, NEO_GRB + NEO_KHZ800);
uint16_t hue = 0;
void setup() {
ring.begin();
ring.setBrightness(80);
}
void loop() {
for (int i = 0; i < COUNT; i++) {
uint16_t pixelHue = hue + i * 65536L / COUNT;
ring.setPixelColor(i, ring.gamma32(ring.ColorHSV(pixelHue)));
}
ring.show();
hue += 512;
delay(20);
}