Jump-scare box on an Arduino Uno
A radar inside a box sees someone walk up: a servo throws the lid open, a DFPlayer screams and two red eyes flicker, then it shuts and rearms. The whole build, an Arduino Uno and 9 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 30 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
- A state machine with ARMED, SCARING and RESTING
- Timing with
millis()instead ofdelay() - Reading a motion sensor on a digital pin
- Driving a servo, an MP3 module and LEDs together
- Random flicker with
random
- Difficulty: Intermediate
- Time: About 30 minutes
- Board: Arduino Uno R3
- Parts: 11
How it works
A microwave motion sensor sits inside the box and reads HIGH on pin 2 when it sees movement. In the armed state, two red LEDs on pins 5 and 6 glow faintly. When someone walks up, the servo on pin 9 throws the lid open, the DFPlayer plays track 1, and the eyes start to flicker.
After 3 seconds the lid shuts and the box rests for 8 seconds. It arms again only once that rest is over and the sensor no longer sees anyone, so one visitor gets one scare.
The sketch never uses a long delay. It keeps a state and a timestamp, so it can flicker the eyes and watch the sensor at the same time.
Parts list
11 parts, plus the jumper wires. They retail for about $39; on Mokxi they're free.
- 1 × Arduino Uno R3In Starter Kit, Claw Machine Kit, Vending Machine Kit.
- 1 × Full-size breadboard
- 1 × DFPlayer Mini MP3 moduleIn Halloween Prop Kit, Christmas Lights Kit, Laser Sword Kit.
- 1 × Resistor, 1k ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
- 2 × Resistor, 220 ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
- 2 × LED, redIn Starter Kit, Halloween Prop Kit, Logic Lab Kit.
- 1 × Microwave motion sensorIn Halloween Prop Kit.
- 1 × Hobby servoIn Halloween Prop Kit, Christmas Lights Kit, Starter Projects Kit.
- 1 × Speaker, 8 ohmIn Halloween Prop Kit, Christmas Lights Kit, Laser Sword 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, 1k Ω pin 1 | Arduino Uno R3 pin 11 |
| DFPlayer Mini MP3 module pin TX | Arduino Uno R3 pin 10 |
| Hobby servo pin PWM | Arduino Uno R3 pin 9 |
| Resistor, 220 Ω (2) pin 1 | Arduino Uno R3 pin 6 |
| Resistor, 220 Ω (1) pin 1 | Arduino Uno R3 pin 5 |
| Microwave motion sensor pin OUT | Arduino Uno R3 pin 2 |
| DFPlayer Mini MP3 module pin VCC | Arduino Uno R3 pin 5V |
| Microwave motion sensor pin VIN | Arduino Uno R3 pin 5V |
| Hobby servo pin VCC | Arduino Uno R3 pin 5V |
| DFPlayer Mini MP3 module pin GND | Arduino Uno R3 pin GND |
| LED, red (1) pin C | Arduino Uno R3 pin GND |
| LED, red (2) pin C | Arduino Uno R3 pin GND |
| Microwave motion sensor pin GND | Arduino Uno R3 pin GND |
| Hobby servo pin GND | Arduino Uno R3 pin GND |
| Resistor, 1k Ω pin 2 | DFPlayer Mini MP3 module pin RX |
| Speaker, 8 ohm pin 1 | DFPlayer Mini MP3 module pin SPK_1 |
| Speaker, 8 ohm pin 2 | DFPlayer Mini MP3 module pin SPK_2 |
| LED, red (1) pin A | Resistor, 220 Ω (1) pin 2 |
| LED, red (2) pin A | Resistor, 220 Ω (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.
// Jump-scare box: someone walks up to the box on the porch, the lid bursts
// open with a scream and two red eyes flicker inside. Then it shuts, rests,
// and waits for the porch to clear before it arms again.
//
// RCWL-0516 VIN -> 5 V GND -> GND OUT -> pin 2 (high while it sees movement)
// servo PWM -> pin 9, VCC -> 5 V, GND -> GND (the arm that lifts the lid)
// DFPlayer VCC -> 5 V GND -> GND TX -> pin 10 RX <- 1 k <- pin 11
// a small speaker on SPK_1 and SPK_2; track 001.mp3 is the scream
// eyes two red LEDs on pins 5 and 6, each through 220 ohms to GND
//
// Why a radar: the RCWL-0516 sees movement through a thin plastic or cardboard
// wall, so it can hide inside the box. It also sees through doors and thin
// walls, so aim it away from the house. A PIR sensor works too, on the same
// pin, but it needs a hole to look out of.
//
// The sketch never waits in delay(). It keeps one state, ARMED, SCARING or
// RESTING, and the time it entered it, so the eyes can flicker while the lid
// is open and the sensor is read the whole time.
//
// On the bench: give the servo its own 5 V supply (a USB power bank works)
// with its ground joined to the Uno's. A servo that jumps hard can pull the
// Uno's 5 V down far enough to reset it, right at the scary moment.
//
// Things to try. Click the radar to walk up. Change LID_OPEN for a bigger
// throw, or SCARE_MS for a longer scream. Put a second track on the card and
// pick one with random(1, 3) so repeat visitors get a surprise.
#include <SoftwareSerial.h>
#include <DFRobotDFPlayerMini.h>
#include <Servo.h>
const int SENSOR_PIN = 2;
const int LID_PIN = 9;
const int EYE_LEFT = 5;
const int EYE_RIGHT = 6;
const int LID_SHUT = 10; // degrees, lid resting on the box
const int LID_OPEN = 110; // degrees, lid thrown open
const int EYE_IDLE = 12; // a faint glow while armed
const unsigned long SCARE_MS = 3000; // how long the lid stays open
const unsigned long REST_MS = 8000; // quiet time before it can fire again
SoftwareSerial link(10, 11); // RX, TX
DFRobotDFPlayerMini player;
Servo lid;
bool haveSound = false;
enum State { ARMED, SCARING, RESTING };
State state = ARMED;
unsigned long since = 0;
unsigned long nextFlicker = 0;
unsigned int scares = 0;
void eyes(int level) {
analogWrite(EYE_LEFT, level);
analogWrite(EYE_RIGHT, level);
}
void setup() {
Serial.begin(9600);
link.begin(9600);
pinMode(SENSOR_PIN, INPUT);
lid.attach(LID_PIN);
lid.write(LID_SHUT);
haveSound = player.begin(link);
if (haveSound) {
player.volume(28);
} else {
Serial.println("No DFPlayer answered: the box will scare in silence");
}
eyes(EYE_IDLE);
Serial.println("Armed");
}
void loop() {
unsigned long now = millis();
bool someone = digitalRead(SENSOR_PIN) == HIGH;
switch (state) {
case ARMED:
if (someone) {
state = SCARING;
since = now;
scares++;
lid.write(LID_OPEN);
if (haveSound) player.play(1);
Serial.print("BOO! Scare ");
Serial.println(scares);
}
break;
case SCARING:
if (now - since >= SCARE_MS) {
lid.write(LID_SHUT);
eyes(0);
state = RESTING;
since = now;
Serial.println("Lid shut, resting");
} else if (now >= nextFlicker) {
// A candle-like flicker: a random level, held for a random moment.
eyes(random(40, 256));
nextFlicker = now + random(30, 90);
}
break;
case RESTING:
// Rearm only once the rest is over and nobody is standing there, so
// one visitor gets one scare.
if (now - since >= REST_MS && !someone) {
state = ARMED;
eyes(EYE_IDLE);
Serial.println("Armed");
}
break;
}
}
Code walkthrough
The key functions of the sketch, and what each one does.
eyes sketch.ino
Writes the same brightness to both eye LEDs with analogWrite. The sketch uses it for the faint idle glow, the random flicker, and turning the eyes off.
void eyes(int level) {
analogWrite(EYE_LEFT, level);
analogWrite(EYE_RIGHT, level);
}setup sketch.ino
Sets up the serial link to the DFPlayer, the sensor pin and the servo, and closes the lid. If the player answers, it sets the volume to 28. If not, the box still works, just silently. Then the eyes go to their idle glow.
void setup() {
Serial.begin(9600);
link.begin(9600);
pinMode(SENSOR_PIN, INPUT);
lid.attach(LID_PIN);
lid.write(LID_SHUT);
haveSound = player.begin(link);
if (haveSound) {
player.volume(28);
} else {
Serial.println("No DFPlayer answered: the box will scare in silence");
}
eyes(EYE_IDLE);
Serial.println("Armed");
}loop sketch.ino
Reads the sensor and the clock, then handles the current state. ARMED waits for movement. SCARING flickers the eyes until SCARE_MS passes, then shuts the lid. RESTING waits for REST_MS and an empty porch before arming again.
void loop() {
unsigned long now = millis();
bool someone = digitalRead(SENSOR_PIN) == HIGH;
switch (state) {
case ARMED:
if (someone) {
state = SCARING;
since = now;
scares++;
lid.write(LID_OPEN);
if (haveSound) player.play(1);
Serial.print("BOO! Scare ");
Serial.println(scares);
}
break;
case SCARING:
if (now - since >= SCARE_MS) {
lid.write(LID_SHUT);
eyes(0);
state = RESTING;
since = now;
Serial.println("Lid shut, resting");
} else if (now >= nextFlicker) {
// A candle-like flicker: a random level, held for a random moment.
eyes(random(40, 256));
nextFlicker = now + random(30, 90);
// ... 14 more lines in the editorTroubleshooting
The usual mistakes with this circuit, and the fix for each.
- The Uno resets just as the lid opens This project
Give the servo its own 5 V supply and join its ground to the Uno's ground. - The lid opens but there is no scream This project
The sketch prints that no DFPlayer answered if it was not found. Check the TX and RX wiring and the 1k resistor. - The lid opens too little or too much This project
ChangeLID_OPENandLID_SHUT. - The module stays silent or does not respond to commands. DFPlayer Mini MP3 module
Format the microSD card as FAT32 and name files 0001.mp3, 0002.mp3 and so on. Copy them in the order you want. Check the card is seated in the slot. - Commands are garbled or ignored on the serial link. DFPlayer Mini MP3 module
The module uses 9600 baud and 3.3 V logic on RX. Put a 1 k resistor in series with the Arduino TX line to its RX. Cross the lines: Arduino TX to module RX. - 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 LED does not light, or it lights once and then dies. LED
LEDs have polarity. The longer leg (anode) goes toward the positive side. Always add a series resistor, about 220 ohms to 1 k ohm on a 5 V pin.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.