Haunted window on an Arduino Uno
A WS2812B strip across a window throws lightning in bursts of flashes while four LEDs on the sill flicker like candles, and a knob sets how wild the storm is. The whole build, an Arduino Uno and 5 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 25 minutes to build and run. Runs in your browser. Free, and no account needed.
Build it as the Halloween Prop Kit: the parts in numbered bags and a booklet that checks every step. Retails for about $66, free on Mokxi.
What you'll learn
- Driving addressable LED strips with NeoPixel
- Timing with millis instead of delay
- Running two animations at once
- Mapping a potentiometer to a time range
- Smoothing random changes to look natural
- Difficulty: Intermediate
- Time: About 25 minutes
- Board: Arduino Uno R3
- Parts: 7
How it works
Two addressable LED strips do the work. The sky strip on pin 6 has 12 pixels along the top of the window. The sill strip on pin 5 has 4 pixels that act as candles. Each data line goes through a 330 ohm resistor.
Lightning happens in bursts of one to three flashes. Each flash lights a random stretch of the sky strip in white-blue, and a dim blue storm glow fills the gaps. A potentiometer on A0 sets how long the quiet times between strikes are.
The candles each drift toward a new random brightness every few frames, in a warm orange. The sketch never uses delay, so the candles keep moving while the sky waits.
Parts list
7 parts, plus the jumper wires. They retail for about $37; on Mokxi they're free.
- 1 × Arduino Uno R3In Starter Kit, Claw Machine Kit, Vending Machine Kit.
- 1 × Half breadboard
- 1 × PotentiometerIn Halloween Prop Kit, Starter Projects Kit, MicroPython Pico Kit.
- 2 × Resistor, 330 ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
- 2 × 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 Ω (1) pin 1 | Arduino Uno R3 pin 6 |
| Resistor, 330 Ω (2) pin 1 | Arduino Uno R3 pin 5 |
| Potentiometer pin 1 | Arduino Uno R3 pin 5V |
| Addressable RGB LEDs (1) pin VDD | Arduino Uno R3 pin 5V |
| Addressable RGB LEDs (2) pin VDD | Arduino Uno R3 pin 5V |
| Potentiometer pin 3 | Arduino Uno R3 pin GND |
| Addressable RGB LEDs (1) pin VSS | Arduino Uno R3 pin GND |
| Addressable RGB LEDs (2) pin VSS | Arduino Uno R3 pin GND |
| Potentiometer pin 2 | Arduino Uno R3 pin A0 |
| Addressable RGB LEDs (1) pin DIN | Resistor, 330 Ω (1) pin 2 |
| Addressable RGB LEDs (2) pin DIN | Resistor, 330 Ω (2) pin 2 |
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// Haunted window: a storm outside a haunted house. A WS2812B strip along the
// top of the window frame throws lightning, four more on the sill flicker
// like candles, and a knob sets how often the lightning strikes.
//
// sky strip 12 LEDs, DIN <- 330 ohms <- pin 6, VDD -> 5 V, VSS -> GND
// sill strip 4 LEDs, DIN <- 330 ohms <- pin 5, VDD -> 5 V, VSS -> GND
// knob 10 k potentiometer: ends to 5 V and GND, wiper to A0
//
// Real lightning is rarely a single flash. A stroke often comes in a quick
// burst of return strokes down the same channel, so here each strike is one
// to three flashes, each a few tens of milliseconds long, along one stretch
// of the strip, with a dim blue storm glow in between.
//
// The candles are the opposite: slow, warm and never quite still. Each one
// drifts toward a new random brightness every few frames, which reads as a
// flame far better than random blinking does.
//
// Nothing here uses delay(), so the candles keep flickering while the sky
// waits for its next strike.
//
// On the bench: give the strips their own 5 V supply sized for the LEDs you
// run, join its ground to the Uno's, and put a large capacitor (1000 uF is
// the usual advice) across the strip's 5 V and GND where the power comes in.
// Keep the 330 ohm resistor close to the first LED.
//
// Things to try. Turn the knob. Make the storm purple by changing STORM. Add
// a DFPlayer with a thunder track and play it a second or two after each
// strike, the way thunder trails a distant flash.
#include <Adafruit_NeoPixel.h>
const int SKY_PIN = 6;
const int SILL_PIN = 5;
const int KNOB_PIN = A0;
const int SKY_LEDS = 12;
const int SILL_LEDS = 4;
Adafruit_NeoPixel sky(SKY_LEDS, SKY_PIN, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel sill(SILL_LEDS, SILL_PIN, NEO_GRB + NEO_KHZ800);
const uint32_t STORM = Adafruit_NeoPixel::Color(0, 0, 10);
// The lightning.
unsigned long nextChange = 0;
int flashesLeft = 0;
bool flashOn = false;
int boltStart = 0;
int boltLength = 0;
// The candles: where each one is, and where it is drifting to.
int flame[SILL_LEDS];
int target[SILL_LEDS];
unsigned long nextFrame = 0;
void storm() {
sky.fill(STORM);
sky.show();
}
void flash() {
sky.fill(STORM);
int level = random(160, 256);
for (int i = boltStart; i < boltStart + boltLength && i < SKY_LEDS; i++) {
sky.setPixelColor(i, level, level, 255);
}
sky.show();
}
// How long until the next strike: a wilder storm (knob up) strikes sooner.
unsigned long quietTime() {
int knob = analogRead(KNOB_PIN);
unsigned long longest = map(knob, 0, 1023, 9000, 1200);
return random(longest / 3, longest);
}
void lightning(unsigned long now) {
if (now < nextChange) return;
if (flashOn) {
// End of a flash: back to the storm, then either the next flash of this
// strike or a long quiet.
flashOn = false;
storm();
nextChange = now + (flashesLeft > 0 ? random(50, 160) : quietTime());
return;
}
if (flashesLeft == 0) {
// A new strike.
flashesLeft = random(1, 4);
boltLength = random(4, SKY_LEDS + 1);
boltStart = random(0, SKY_LEDS - boltLength + 1);
Serial.print("Strike, ");
Serial.print(flashesLeft);
Serial.println(flashesLeft == 1 ? " flash" : " flashes");
}
flashesLeft--;
flashOn = true;
flash();
nextChange = now + random(30, 90);
}
void candles(unsigned long now) {
if (now < nextFrame) return;
nextFrame = now + 40;
for (int i = 0; i < SILL_LEDS; i++) {
if (random(0, 6) == 0) target[i] = random(90, 256);
flame[i] += (target[i] - flame[i]) / 3;
// A warm orange: lots of red, some green, almost no blue.
sill.setPixelColor(i, flame[i], flame[i] * 2 / 5, flame[i] / 40);
}
sill.show();
}
void setup() {
Serial.begin(9600);
randomSeed(analogRead(A1));
sky.begin();
sill.begin();
for (int i = 0; i < SILL_LEDS; i++) {
flame[i] = 180;
target[i] = 180;
}
storm();
nextChange = millis() + 600;
Serial.println("Storm rolling in");
}
void loop() {
unsigned long now = millis();
lightning(now);
candles(now);
}
Code walkthrough
The key functions of the sketch, and what each one does.
lightning sketch.ino
A small state machine. When the timer is up, it either ends a flash and goes back to the storm glow, or starts a new strike with one to three flashes. It picks a random bolt position and length, and sets nextChange for when to act again.
void lightning(unsigned long now) {
if (now < nextChange) return;
if (flashOn) {
// End of a flash: back to the storm, then either the next flash of this
// strike or a long quiet.
flashOn = false;
storm();
nextChange = now + (flashesLeft > 0 ? random(50, 160) : quietTime());
return;
}
if (flashesLeft == 0) {
// A new strike.
flashesLeft = random(1, 4);
boltLength = random(4, SKY_LEDS + 1);
boltStart = random(0, SKY_LEDS - boltLength + 1);
Serial.print("Strike, ");
Serial.print(flashesLeft);
Serial.println(flashesLeft == 1 ? " flash" : " flashes");
}
flashesLeft--;
flashOn = true;
flash();
nextChange = now + random(30, 90);
}quietTime sketch.ino
Reads the knob and maps it to a longest wait, from 9000 ms with the knob down to 1200 ms with it up. The actual wait is random between a third of that and the full amount, so turning the knob up makes the storm wilder.
// How long until the next strike: a wilder storm (knob up) strikes sooner.
unsigned long quietTime() {
int knob = analogRead(KNOB_PIN);
unsigned long longest = map(knob, 0, 1023, 9000, 1200);
return random(longest / 3, longest);
}candles sketch.ino
Every 40 ms, each candle may pick a new target brightness. Then its current brightness moves a third of the way there. Green and blue are scaled from the same value, giving a warm orange flame.
void candles(unsigned long now) {
if (now < nextFrame) return;
nextFrame = now + 40;
for (int i = 0; i < SILL_LEDS; i++) {
if (random(0, 6) == 0) target[i] = random(90, 256);
flame[i] += (target[i] - flame[i]) / 3;
// A warm orange: lots of red, some green, almost no blue.
sill.setPixelColor(i, flame[i], flame[i] * 2 / 5, flame[i] / 40);
}
sill.show();
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- Nothing lights up This project
Check that each strip has 5 V and ground, and that its data input, DIN, is wired through the resistor to the right pin: 6 for the sky and 5 for the sill. - The candles work but the sky does not, or the reverse This project
The two strips are on different pins. CompareSKY_PINandSILL_PINwith your wires. - The knob does nothing This project
Its middle pin must go to A0, with the outer pins on 5 V and ground. Strikes are random, so give it a few seconds. - The reading jumps around or only covers part of the range. Potentiometer
Wire the two outer legs to 5 V and ground and the middle leg (the wiper) to an analog pin. Using the wrong two legs gives a dead or partial range. - Turning the knob does nothing, or the value is stuck at 0 or 1023. Potentiometer
Check that the wiper goes to an analog input (A0 to A5 on an Uno), not a digital pin. Make sure both ends are connected, since a floating end gives noise. - 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.