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 About 20 minutes to build and run. Runs in your browser. Free, and no account needed.

The parts retail for about $33. On Mokxi they're free.

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.

What you'll learn

  • Addressable LEDs with the NeoPixel library
  • Randomness for natural sparkle
  • A burning point that moves with elapsed time
  • A button with INPUT_PULLUP
  • Why a data pin has a series resistor
  • Difficulty: Beginner
  • Time: About 20 minutes
  • Board: Arduino Uno R3
  • Parts: 5

How it works

A stick of 16 WS2812B LEDs, with LED 0 at the handle. Press the button on pin 2 (it uses the internal pull-up) and the sparkler lights. A burning point starts at the far end and creeps toward the handle over BURN_MS, which is 20 seconds.

At the burning point the LEDs flash at random: one time in three white, otherwise gold. Just behind it, a dim orange glow shows where it is about to catch. A few LEDs past the tip throw off sparks. Everything beyond that is dark.

When the time is up the stick goes out. The sketch counts how many sparklers you have lit and prints it.

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 pinConnects to
Resistor, 330 Ω pin 1Arduino Uno R3 pin 6
Pushbutton pin 1aArduino Uno R3 pin 2
Addressable RGB LEDs pin VDDArduino Uno R3 pin 5V
Pushbutton pin 2aArduino Uno R3 pin GND
Addressable RGB LEDs pin VSSArduino Uno R3 pin GND
Addressable RGB LEDs pin DINResistor, 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.

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);
}

Code walkthrough

The key functions of the sketch, and what each one does.

setup sketch.ino

setup opens serial, sets the button to INPUT_PULLUP, starts the LED stick at brightness 200, and shows it dark. It then prints that the sparkler is ready.

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");
}

loop sketch.ino

loop waits for a button press to start the burn. While burning, it works out the tip position from how much of BURN_MS has passed. Each LED is then colored by how far it is from the tip, with random white and gold flashes, orange glow and sparks. When the time is up it clears the stick.

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);
  // ... 13 more lines in the editor

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • Pressing the button does nothing. This project
    The button goes between pin 2 and GND. It is also ignored while a sparkler is already burning.
  • The LEDs show strange colors or none. This project
    DIN goes through the 330 ohm resistor to pin 6. VDD goes to 5V and VSS to GND.
  • It burns too fast or too slow. This project
    Change BURN_MS. The value is in milliseconds.
  • 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.