LED fireworks on an Arduino Uno
Rockets climb a 30-LED WS2812B strip and burst into fading sparks while a piezo whistles and pops, with a button for the grand finale. Safe for any age and any noise rule. The whole build, an Arduino Uno and 4 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.
Intermediate About 20 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $34. On Mokxi they're free.
What you'll learn
- Driving addressable LEDs with the Adafruit_NeoPixel library
- Animating with a frame loop instead of long delays
- Using whole numbers in 1/16ths of an LED for slow motion
- Fading a color by scaling its red, green and blue
- Making sound effects with tone()
- Difficulty: Intermediate
- Time: About 20 minutes
- Board: Arduino Uno R3
- Parts: 6
How it works
A rocket is a bright dot that climbs a strip of 30 WS2812B LEDs, slowing as it goes, while the piezo on pin 8 whistles with a rising pitch. At a random height it bursts into 8 sparks of one color that fly up and down, slow down and fade, with white glitters at the end.
A new rocket launches every 1.5 to 3.5 seconds. Press the button on pin 2 for a finale of 6 rockets in a row. Everything moves in small frames of 25 ms, so the button is never missed.
Parts list
6 parts, plus the jumper wires. They retail for about $34; 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.
- 1 × Piezo speakerIn Starter Kit, Claw Machine Kit, Vending Machine 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 |
|---|---|
| Piezo speaker pin 1 | Arduino Uno R3 pin 8 |
| 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 |
| Piezo speaker pin 2 | 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.
// LED fireworks: rockets climb a strip of LEDs, burst into sparks that fly
// both ways and fade, and a piezo whistles on the way up and pops at the top.
// Press the button for the grand finale.
//
// strip 30 WS2812B LEDs, DIN <- 330 ohms <- pin 6 (stand it up straight)
// piezo pin 8 -> piezo -> GND
// button pin 2 -> button -> GND (INPUT_PULLUP): the finale
//
// Hang the strip up a window or along a fence post, LED 0 at the bottom. A
// rocket is one bright dot with a short tail that slows as it climbs, the way
// a real one does, and bursts at a random height. The burst throws sparks of
// one color up and down from that point, each slowing and fading, with a few
// white glitters at the end.
//
// Everything moves on a frame clock: each frame moves the rocket, ages the
// sparks and draws them all, then returns, so the button is read between
// frames and is never missed.
//
// Real fireworks are for the professionals. These ones are safe for any age,
// any weather and any noise rule.
//
// Things to try. Press the button for the finale. Add colors to COLORS. Make
// the bursts bigger with SPARKS.
#include <Adafruit_NeoPixel.h>
const int LEDS = 30;
const int PIEZO_PIN = 8;
const int BUTTON_PIN = 2;
const int SPARKS = 8;
Adafruit_NeoPixel strip(LEDS, 6, NEO_GRB + NEO_KHZ800);
const uint32_t COLORS[] = {0xFF2020, 0xFFFFFF, 0x2050FF, 0xFFC020, 0x30FF60, 0xFF40FF};
const int SHADES = sizeof(COLORS) / sizeof(COLORS[0]);
// Positions and speeds in 1/16ths of an LED, so things can move slowly.
int rocket = -1, rocketSpeed = 0, burstAt = 0;
int sparkPos[SPARKS], sparkSpeed[SPARKS];
uint8_t sparkLife = 0;
uint32_t sparkColor = 0;
unsigned long nextLaunch = 1000;
int launches = 0;
int finale = 0; // rockets left in the finale
void launch() {
rocket = 0;
rocketSpeed = random(10, 14);
burstAt = random(LEDS / 2, LEDS - 3) * 16;
launches++;
Serial.print("Launch ");
Serial.println(launches);
}
void burst() {
sparkColor = COLORS[random(0, SHADES)];
for (int i = 0; i < SPARKS; i++) {
sparkPos[i] = rocket;
sparkSpeed[i] = (i % 2 ? 1 : -1) * random(3, 12);
}
sparkLife = 255;
rocket = -1;
tone(PIEZO_PIN, 140, 60); // pop
}
void setup() {
Serial.begin(9600);
pinMode(BUTTON_PIN, INPUT_PULLUP);
strip.begin();
strip.setBrightness(200);
strip.show();
randomSeed(analogRead(A0));
Serial.println("Fireworks ready: press the button for the finale");
}
void loop() {
unsigned long now = millis();
if (digitalRead(BUTTON_PIN) == LOW && finale == 0) {
finale = 6;
Serial.println("Grand finale!");
}
if (rocket < 0 && sparkLife < 120 && (now >= nextLaunch || finale > 0)) {
launch();
if (finale > 0) finale--;
nextLaunch = now + random(1500, 3500);
}
strip.clear();
if (rocket >= 0) {
rocket += rocketSpeed;
if (rocketSpeed > 4 && random(0, 3) == 0) rocketSpeed--; // slowing
tone(PIEZO_PIN, 1500 + rocket * 2, 20); // whistle
int at = rocket / 16;
strip.setPixelColor(at, 255, 200, 120);
if (at > 0) strip.setPixelColor(at - 1, 60, 30, 0);
if (rocket >= burstAt) burst();
}
if (sparkLife > 0) {
uint8_t r = (uint8_t)(sparkColor >> 16) * sparkLife / 255;
uint8_t g = (uint8_t)(sparkColor >> 8) * sparkLife / 255;
uint8_t b = (uint8_t)sparkColor * sparkLife / 255;
for (int i = 0; i < SPARKS; i++) {
sparkPos[i] += sparkSpeed[i];
if (sparkSpeed[i] > 1) sparkSpeed[i]--;
if (sparkSpeed[i] < -1) sparkSpeed[i]++;
int at = sparkPos[i] / 16;
if (at >= 0 && at < LEDS) {
bool glitter = sparkLife < 60 && random(0, 4) == 0;
strip.setPixelColor(at, glitter ? strip.Color(255, 255, 255) : strip.Color(r, g, b));
}
}
sparkLife = sparkLife > 8 ? sparkLife - 8 : 0;
}
strip.show();
delay(25);
}
Code walkthrough
The key functions of the sketch, and what each one does.
launch sketch.ino
Starts a rocket at the bottom with a random speed and a random burst height, somewhere between the middle of the strip and three LEDs from the top. It also counts the launch and prints it.
void launch() {
rocket = 0;
rocketSpeed = random(10, 14);
burstAt = random(LEDS / 2, LEDS - 3) * 16;
launches++;
Serial.print("Launch ");
Serial.println(launches);
}burst sketch.ino
Picks a random color from COLORS and gives each of the SPARKS a starting spot at the rocket and a speed, alternating up and down. It sets the sparks' life to full, ends the rocket and plays a short low pop.
void burst() {
sparkColor = COLORS[random(0, SHADES)];
for (int i = 0; i < SPARKS; i++) {
sparkPos[i] = rocket;
sparkSpeed[i] = (i % 2 ? 1 : -1) * random(3, 12);
}
sparkLife = 255;
rocket = -1;
tone(PIEZO_PIN, 140, 60); // pop
}loop sketch.ino
Each frame it checks the button, launches when due, then clears the strip. It moves and draws the rocket and its whistle, then ages and draws the sparks, dimming them as life falls. Last it shows the strip and waits 25 ms.
void loop() {
unsigned long now = millis();
if (digitalRead(BUTTON_PIN) == LOW && finale == 0) {
finale = 6;
Serial.println("Grand finale!");
}
if (rocket < 0 && sparkLife < 120 && (now >= nextLaunch || finale > 0)) {
launch();
if (finale > 0) finale--;
nextLaunch = now + random(1500, 3500);
}
strip.clear();
if (rocket >= 0) {
rocket += rocketSpeed;
if (rocketSpeed > 4 && random(0, 3) == 0) rocketSpeed--; // slowing
tone(PIEZO_PIN, 1500 + rocket * 2, 20); // whistle
int at = rocket / 16;
strip.setPixelColor(at, 255, 200, 120);
if (at > 0) strip.setPixelColor(at - 1, 60, 30, 0);
if (rocket >= burstAt) burst();
}
if (sparkLife > 0) {
uint8_t r = (uint8_t)(sparkColor >> 16) * sparkLife / 255;
uint8_t g = (uint8_t)(sparkColor >> 8) * sparkLife / 255;
uint8_t b = (uint8_t)sparkColor * sparkLife / 255;
for (int i = 0; i < SPARKS; i++) {
sparkPos[i] += sparkSpeed[i];
// ... 13 more lines in the editorTroubleshooting
The usual mistakes with this circuit, and the fix for each.
- The strip stays dark or shows random colors This project
Data goes from pin 6 through the 330 ohm resistor to DIN. Check VDD on 5 V and that the grounds are joined. - The strip has a different number of LEDs This project
ChangeLEDSto match your strip. - 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. - The strip works at low brightness but dims, turns yellow, or resets when many LEDs go white. Addressable RGB LEDs
Each LED can draw about 60 mA at full white. Use a 5 V supply sized for the count, feed power in at several points, and lower the brightness in code.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.