Lights
Motion ghost lamp
A little ghost that sits dark and quiet until someone walks past. Then it glows and lets out a wail.
- Medium
- about 45 min
- ESP32-C3
- 8 parts
- 8 wires
- 51 lines of code
ESP32 packs are built on real parts: the simulator runs Arduino Uno circuits only.
Idea
The motion sensor notices warmth that moves. It needs half a minute to settle after power-on, which is why it seems jumpy at first.
What you learn
Reading a motion sensor, keeping something on for a set time after a trigger, and a smooth pulse of light.
How it works
input
Motion sensor
decides
ESP32-C3
output
LED ring
Buzzer
Powered with 5 V through the board's USB-C port.
A reference design. The sketch is compiled with the real ESP32 toolchain, and its pins are checked against the wiring table automatically. It has not yet been built and tested on real parts by us, so wire it on the bench and run the checks before you build it into a case. Real modules vary: follow the labels printed on yours, not their position in the picture.
Parts
0 of 8 ready
No prices here: they change by shop and by week. Buy each part by its name and the note beside it.
You also need
A computer with Arduino IDE 2 installed (free)
3D wiring
One jumper per step, with the USB cable unplugged. Several wires may share 3V3, 5V or GND: the expansion board or breadboard gives each its own pin.
Step 0 of 8
Lay out the parts
Seat the board on its expansion board or breadboard and lay the modules out round it. Nothing is connected yet.
All connections
ghost-lamp.ino
// Motion ghost lamp
// Dark and quiet until someone walks past: then it glows and wails.
#include <Adafruit_NeoPixel.h>
const int PIN_PIR_OUT = 3;
const int PIN_RING_DIN = 4;
const int PIN_BUZZER_POS = 10;
const int LED_COUNT = 12;
const unsigned long GLOW_MS = 6000; // how long it stays awake after motion
Adafruit_NeoPixel ring(LED_COUNT, PIN_RING_DIN, NEO_GRB + NEO_KHZ800);
unsigned long awakeUntil = 0;
bool wasAwake = false;
void wail() {
for (int f = 400; f < 900; f += 10) {
tone(PIN_BUZZER_POS, f);
delay(6);
}
for (int f = 900; f > 300; f -= 8) {
tone(PIN_BUZZER_POS, f);
delay(6);
}
noTone(PIN_BUZZER_POS);
}
void setup() {
pinMode(PIN_PIR_OUT, INPUT);
pinMode(PIN_BUZZER_POS, OUTPUT);
ring.begin();
ring.show();
}
void loop() {
if (digitalRead(PIN_PIR_OUT) == HIGH) awakeUntil = millis() + GLOW_MS;
bool awake = millis() < awakeUntil;
if (awake && !wasAwake) wail();
if (awake) {
// a slow pulse of pale ghost light
float level = (sin(millis() / 300.0f) + 1.0f) / 2.0f;
uint8_t v = (uint8_t)(30 + level * 150);
ring.fill(ring.Color(v / 2, v, v));
} else {
ring.clear();
}
ring.show();
wasAwake = awake;
delay(30);
}
Libraries
- Adafruit NeoPixel: install it from Sketch > Include Library > Manage Libraries.
Uploading
First time with an ESP32: in Arduino IDE open Boards Manager, search “esp32” and install “esp32 by Espressif Systems”.
- Open the sketch in Arduino IDE.
- Plug the board in with the USB-C cable.
- Tools > Board > ESP32C3 Dev Module (USB CDC On Boot: Enabled), then Tools > Port and choose the board's port.
- Press Upload (the arrow). Wait for “Done uploading”.