Arduino Uno: NeoPixel ring animation
Color wipe and rainbow on 12 WS2812 LEDs.
- Arduino Uno
- WS2812
Open in the simulatorNo account needed: press Simulate and it runs.
How it works
A ring of 12 WS2812B LEDs (NeoPixel) on D6 runs color wipes and a rainbow with the Adafruit NeoPixel library. The ATmega328P has no special hardware for this: the library sends the 800 kHz signal by toggling the pin with exact instruction timing, and the LEDs decode it from the voltage in the simulation. Potentiometer RV1 on A0 sets the speed. Put the logic analyzer on DIN to see the bits.
Wiring
Each line is one connection (net) and the pins it joins.
| Net | Pins |
|---|---|
| GND | A1 GNDLED1 GNDRV1 2 |
| 5V | A1 5VLED1 5VRV1 1 |
| DIN | A1 ~D6LED1 DIN |
| POT | A1 A0RV1 W |
Parts
Firmware
sketch.ino · 28 lines
// 12-LED NeoPixel ring (WS2812B) on D6, speed from the potentiometer on A0.
// Adafruit_NeoPixel bit-bangs the 800 kHz signal with exact cycle timing (interrupts off per frame).
#include <Adafruit_NeoPixel.h>
const int PIN = 6, COUNT = 12;
Adafruit_NeoPixel ring(COUNT, PIN, NEO_GRB + NEO_KHZ800);
int pause() { return map(analogRead(A0), 0, 1023, 5, 120); } // ms per step
void wipe(uint32_t color) {
for (int i = 0; i < COUNT; i++) { ring.setPixelColor(i, color); ring.show(); delay(pause()); }
}
void setup() {
Serial.begin(115200);
ring.begin();
ring.setBrightness(80);
Serial.println(F("NeoPixel ring ready"));
}
void loop() {
wipe(ring.Color(255, 0, 0)); wipe(ring.Color(0, 255, 0)); wipe(ring.Color(0, 0, 255));
Serial.println(F("rainbow"));
for (long hue = 0; hue < 65536; hue += 2048) {
for (int i = 0; i < COUNT; i++) ring.setPixelColor(i, ring.gamma32(ring.ColorHSV(hue + i * 65536L / COUNT)));
ring.show();
delay(pause());
}
}Open in the simulator