Tree topper star on an Arduino Uno
A WS2812B ring behind a star cutout tops the tree: warm twinkle, a gold comet, a slow rainbow and a red and green chase, one press of a button each. The whole build, an Arduino Uno and 3 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.
Teaching? Assign it to your class in one click.
Beginner About 20 minutes to build and run. Runs in your browser. Free, and no account needed.
Build it as the Christmas Lights Kit: the parts in numbered bags and a booklet that checks every step. Retails for about $60, free on Mokxi.
What you'll learn
- Addressable LEDs in a ring
- Writing animations one frame at a time
- Edge detection and debouncing a button
- Timing with
millisinstead of waiting - Choosing a show with
switch
- Difficulty: Beginner
- Time: About 20 minutes
- Board: Arduino Uno R3
- Parts: 5
How it works
A ring of 16 WS2812B LEDs sits behind a star cutout. Each press of the button on pin 2 moves to the next of four light shows: a warm white twinkle, a gold comet, a slow rainbow and a red and green chase.
Every show is a function that draws one frame and returns. The loop calls the current one every 40 ms and checks the button in between, so no press is missed however slow the show. The button ignores bounces for 200 ms.
Parts list
5 parts, plus the jumper wires. They retail for about $33; on Mokxi they're free.
- 1 × Arduino Uno R3In Starter Kit, Claw Machine Kit, Vending Machine Kit.
- 1 × Half breadboard
- 1 × PushbuttonIn Starter Kit, Christmas Lights Kit, Logic Lab Kit.
- 1 × Resistor, 330 ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
- 1 × Addressable RGB LEDsIn Claw Machine Kit, Halloween Prop Kit, Christmas Lights Kit.
Wiring table
Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.
| Part and pin | Connects to |
|---|---|
| Resistor, 330 Ω pin 1 | Arduino Uno R3 pin 6 |
| Pushbutton pin 1a | Arduino Uno R3 pin 2 |
| Addressable RGB LEDs pin VDD | Arduino Uno R3 pin 5V |
| Pushbutton pin 2a | Arduino Uno R3 pin GND |
| Addressable RGB LEDs pin VSS | Arduino Uno R3 pin GND |
| Addressable RGB LEDs pin DIN | Resistor, 330 Ω pin 2 |
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// Tree topper star: a ring of 16 WS2812B LEDs behind a star cutout on top of
// the tree, with four light shows. A press of the button moves to the next.
//
// LED ring 16 LEDs, DIN <- 330 ohms <- pin 6, VDD -> 5 V, VSS -> GND
// button pin 2 -> button -> GND (INPUT_PULLUP, so pressed reads LOW)
//
// show 0 warm twinkle: soft white, with LEDs brightening and fading at random
// show 1 gold comet: a bright head with a fading tail running around the ring
// show 2 rainbow: the whole color wheel turning slowly
// show 3 red and green: alternating pairs that swap places
//
// Every show is a function that draws one frame and returns. loop() calls the
// current one every FRAME_MS and checks the button in between, so a press is
// never missed, however slow the show.
//
// On the tree: a ring this size is happy on the Uno's 5 V for testing, but
// give it its own 5 V supply on the tree, with the grounds joined. Cut a star
// from card or thin plywood, line it with foil or white paper, and set the
// ring behind it so the light spreads into the points.
//
// Things to try. Press the button. Change GOLD to your own color. Add a fifth
// show by writing one more function and adding it to the switch.
#include <Adafruit_NeoPixel.h>
const int RING_PIN = 6;
const int BUTTON_PIN = 2;
const int LEDS = 16;
const int SHOWS = 4;
const unsigned long FRAME_MS = 40;
Adafruit_NeoPixel ring(LEDS, RING_PIN, NEO_GRB + NEO_KHZ800);
const uint32_t GOLD = Adafruit_NeoPixel::Color(255, 150, 20);
int show = 0;
unsigned long frame = 0;
unsigned long nextFrame = 0;
bool wasPressed = false;
unsigned long lastPress = 0;
uint8_t sparkle[LEDS];
void twinkle() {
for (int i = 0; i < LEDS; i++) {
if (sparkle[i] > 40) sparkle[i] -= 6;
if (random(0, 30) == 0) sparkle[i] = 255;
int v = sparkle[i] < 40 ? 40 : sparkle[i];
ring.setPixelColor(i, v, v * 4 / 5, v / 2); // warm white
}
}
void comet() {
int head = frame % LEDS;
ring.clear();
for (int t = 0; t < 6; t++) {
int i = (head - t + LEDS) % LEDS;
// GOLD at full for the head, and each step of the tail at half the last.
uint8_t r = (uint8_t)(GOLD >> 16) >> t;
uint8_t g = (uint8_t)(GOLD >> 8) >> t;
uint8_t b = (uint8_t)GOLD >> t;
ring.setPixelColor(i, r, g, b);
}
}
void rainbow() {
ring.rainbow((uint16_t)(frame * 256));
}
void redGreen() {
bool swap = (frame / 12) % 2;
for (int i = 0; i < LEDS; i++) {
bool red = ((i / 2) % 2 == 0) != swap;
ring.setPixelColor(i, red ? 200 : 0, red ? 0 : 160, 0);
}
}
void setup() {
Serial.begin(9600);
pinMode(BUTTON_PIN, INPUT_PULLUP);
ring.begin();
ring.setBrightness(160);
for (int i = 0; i < LEDS; i++) sparkle[i] = 40;
Serial.println("Star lit: show 0, warm twinkle");
}
void loop() {
unsigned long now = millis();
// One step per press: act on the moment the button goes down, and ignore
// the contacts bouncing for a moment after.
bool pressed = digitalRead(BUTTON_PIN) == LOW;
if (pressed && !wasPressed && now - lastPress > 200) {
lastPress = now;
show = (show + 1) % SHOWS;
Serial.print("Show ");
Serial.println(show);
}
wasPressed = pressed;
if (now < nextFrame) return;
nextFrame = now + FRAME_MS;
frame++;
switch (show) {
case 0: twinkle(); break;
case 1: comet(); break;
case 2: rainbow(); break;
case 3: redGreen(); break;
}
ring.show();
}
Code walkthrough
The key functions of the sketch, and what each one does.
twinkle sketch.ino
Fades each LED down a little every frame, and at random sets one to full brightness. A warm white color is made from that one value, and none goes below 40.
void twinkle() {
for (int i = 0; i < LEDS; i++) {
if (sparkle[i] > 40) sparkle[i] -= 6;
if (random(0, 30) == 0) sparkle[i] = 255;
int v = sparkle[i] < 40 ? 40 : sparkle[i];
ring.setPixelColor(i, v, v * 4 / 5, v / 2); // warm white
}
}comet sketch.ino
Draws a head that moves one LED per frame with a tail of five more, each half as bright. It uses GOLD for the head and shifts the bits to dim the tail.
void comet() {
int head = frame % LEDS;
ring.clear();
for (int t = 0; t < 6; t++) {
int i = (head - t + LEDS) % LEDS;
// GOLD at full for the head, and each step of the tail at half the last.
uint8_t r = (uint8_t)(GOLD >> 16) >> t;
uint8_t g = (uint8_t)(GOLD >> 8) >> t;
uint8_t b = (uint8_t)GOLD >> t;
ring.setPixelColor(i, r, g, b);
}
}loop sketch.ino
Checks for a new press and moves to the next show, wrapping after the last. Then it waits until the next frame is due, calls the current show's function and sends the colors to the ring.
void loop() {
unsigned long now = millis();
// One step per press: act on the moment the button goes down, and ignore
// the contacts bouncing for a moment after.
bool pressed = digitalRead(BUTTON_PIN) == LOW;
if (pressed && !wasPressed && now - lastPress > 200) {
lastPress = now;
show = (show + 1) % SHOWS;
Serial.print("Show ");
Serial.println(show);
}
wasPressed = pressed;
if (now < nextFrame) return;
nextFrame = now + FRAME_MS;
frame++;
switch (show) {
case 0: twinkle(); break;
case 1: comet(); break;
case 2: rainbow(); break;
case 3: redGreen(); break;
}
ring.show();
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- The button skips shows This project
It ignores presses within 200 ms of each other. If it still skips, check the button is wired between pin 2 and ground. - The ring stays dark This project
Check DIN goes through the 330 ohm resistor from pin 6, and VDD is on 5V with VSS on ground. - The colors look wrong This project
Check thatNEO_GRBmatches your LED type. - The input reads random HIGH and LOW when the button is not pressed. Pushbutton
A floating pin picks up noise. Add a 10 k pull-down or pull-up resistor, or use INPUT_PULLUP and wire the button between the pin and GND. Then pressed reads LOW. - The button seems always pressed, or never changes. Pushbutton
A 4-leg tactile switch has legs joined in pairs. Place it across the breadboard center gap so the pairs sit on different rows. Wired along one side, the legs are already connected. - The LED is dim, burns out, or the pin reads wrong because the resistor value is off. Resistor
Read the color bands again. A 220 ohm to 330 ohm resistor suits a typical LED on 5 V. 10k is a common pull-up or pull-down value. Check with a multimeter if unsure. - A pull-up or pull-down resistor is missing, so the input floats and reads random values. Resistor
Tie the input to 5 V or ground through about 10k. Or use INPUT_PULLUP and wire the switch to ground. - The first LED flickers, shows wrong colors, or the whole strip glitches. Addressable RGB LEDs
Join the grounds of the strip and the controller. Add a 300 to 500 ohm resistor in series with the data line. Put a 1000 uF capacitor across the strip's 5 V and GND.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.