NeoPixel Rainbow
A strip of addressable RGB LEDs flows through the colour wheel. Turn the knob to set the brightness.
- Intermediate
- LED & Lighting
- 3 parts
Opens in your browser with the parts, wiring and code already in place. Press Run to watch it work, then change anything you like.
How it works
A NeoPixel strip is a chain of RGB LEDs, twelve here, each with its own tiny controller chip. The whole strip needs just one signal wire (DIN, on pin 6) plus power and ground: the Arduino sends one long stream of colour data, and each pixel keeps its own piece and passes the rest on. A potentiometer on A0 sets the brightness.
Each pixel’s colour comes from a colour wheel of 65,536 hues. Pixel i gets firstHue plus its share of the wheel, i × 65536 ÷ 12, so the colours are spread evenly along the strip. ColorHSV() turns a hue into a colour, and gamma32() corrects it so the brightness looks natural to the eye. strip.show() sends the result to the strip. After every frame firstHue grows by 512, so the whole pattern turns a little and the rainbow appears to flow. The knob is read with analogRead() and mapped to a brightness between 5 and 255 for setBrightness().
Parts
- Arduino Uno
- NeoPixel LED Strip
- Potentiometer
Wiring
- Arduino Uno 5V → NeoPixel LED Strip VCC
- Arduino Uno GND → NeoPixel LED Strip GND
- Arduino Uno D6 → NeoPixel LED Strip DIN
- Arduino Uno 5V → Potentiometer VCC
- Potentiometer Wiper → Arduino Uno A0
- Arduino Uno GND → Potentiometer GND
Arduino code
// NeoPixel Rainbow
// Each pixel gets its own colour from a wheel of 65536 hues, spread evenly
// along the strip; every frame the whole wheel turns a little, so the
// rainbow flows. The knob sets the brightness.
#include <Adafruit_NeoPixel.h>
#define LED_PIN 6
#define LED_COUNT 12
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
int firstHue = 0;
void setup() {
strip.begin();
strip.show();
}
void loop() {
int knob = analogRead(A0);
strip.setBrightness(map(knob, 0, 1023, 5, 255));
for (int i = 0; i < strip.numPixels(); i++) {
int hue = firstHue + i * 65536 / strip.numPixels();
strip.setPixelColor(i, strip.gamma32(strip.ColorHSV(hue)));
}
strip.show();
firstHue = firstHue + 512;
delay(20);
}
Try changing it
- Make the rainbow flow faster by changing 512 to 2048 in firstHue = firstHue + 512.
- Make it flow the other way by changing the + to a − in that same line.