Built-in project · Arduino Uno R3

LED sparkler on an Arduino Uno

Press the button to light a stick of 16 WS2812B LEDs: random gold and white sparks fly from the burning tip as it creeps down to the handle and goes out, never getting warm. 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 Runs in your browser. Free, and no account needed.

LED sparkler · Arduino Uno R3live0.000 s 0.00x
Press the button to light the sparkler
The circuit itself, running here on the simulator. Press what can be pressed; click anything else to open it in the editor.

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · Arduino Uno R3
// Sparkler: a stick of LEDs that you light with a button. It throws gold and
// white sparks from the burning tip, and burns down toward the handle like a
// real one, then goes out.
//
//   stick     16 WS2812B LEDs, DIN <- 330 ohms <- pin 6 (LED 0 at the handle)
//   button    pin 2 -> button -> GND        (INPUT_PULLUP): light it
//
// The strip goes inside a clear or frosted tube with LED 0 at the handle end.
// The burning point starts at the far end and creeps down over BURN_MS. Above
// it the stick is spent and dark; at it, the LEDs flash at random in gold and
// white; just below it, a few glow orange where the stick is about to catch.
//
// The randomness is the whole effect. Each frame, every LED near the burning
// point gets a fresh random chance to flash, so the sparkle never repeats.
//
// A real sparkler burns at over 1000 degrees Celsius and is a common cause of
// burns on the Fourth of July. This one never gets warm, so the smallest
// people at the party can hold it.
//
// Things to try. Press the button. Make it burn for a minute with BURN_MS.
// Try a red, white and blue sparkle.

#include <Adafruit_NeoPixel.h>

const int LEDS = 16;
const int BUTTON_PIN = 2;
const unsigned long BURN_MS = 20000UL;

Adafruit_NeoPixel stick(LEDS, 6, NEO_GRB + NEO_KHZ800);

bool burning = false;
unsigned long litAt = 0;
int sparklers = 0;

void setup() {
  Serial.begin(9600);
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  stick.begin();
  stick.setBrightness(200);
  stick.show();
  Serial.println("Sparkler ready: press the button to light it");
}

void loop() {
  unsigned long now = millis();
  if (!burning && digitalRead(BUTTON_PIN) == LOW) {
    burning = true;
    litAt = now;
    sparklers++;
    Serial.print("Sparkler ");
    Serial.print(sparklers);
    Serial.println(" lit");
  }
  if (!burning) return;

  unsigned long t = now - litAt;
  if (t >= BURN_MS) {
    burning = false;
    stick.clear();
    stick.show();
    Serial.println("Burned out");
    return;
  }
  // The burning point, from the tip (LEDS - 1) down to the handle.
  int tip = (LEDS - 1) - (int)((long)(LEDS - 1) * t / BURN_MS);
  stick.clear();
  for (int i = 0; i < LEDS; i++) {
    int d = tip - i;   // how far below the burning point
    if (d == 0 || (d == -1 && random(0, 2))) {
      // At the tip: flashes of white and gold.
      if (random(0, 3) == 0) stick.setPixelColor(i, 255, 255, 255);
      else stick.setPixelColor(i, 255, random(120, 200), 20);
    } else if (d > 0 && d <= 2) {
      // Just below: an orange glow, about to catch.
      int v = random(40, 120) / d;
      stick.setPixelColor(i, v, v / 3, 0);
    } else if (d < -1 && d > -5 && random(0, 6) == 0) {
      // Sparks flying off past the tip.
      stick.setPixelColor(i, 255, 220, 120);
    }
  }
  stick.show();
  delay(30);
}

Parts list

5 parts, plus the jumper wires. Every one is in the editor's parts bin.

How it is wired

5 connections, pin by pin, read from the circuit itself. Each line is one set of pins joined together, by a jumper wire or a breadboard strip.

  • Arduino Uno R3 pin 6; Resistor, 330 Ω pin 1
  • Arduino Uno R3 pin 2; Pushbutton pin 1a
  • Arduino Uno R3 pin 5V; Addressable RGB LEDs pin VDD
  • Ground: Arduino Uno R3 pin GND; Pushbutton pin 2a; Addressable RGB LEDs pin VSS
  • Resistor, 330 Ω pin 2; Addressable RGB LEDs pin DIN

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.