Double-clap light switch
Clap twice and the lights come on. Clap twice again to switch them off.
Shop by category
Shop all componentsA magnet in the lid holds a reed switch closed; lifting the lid opens it and the Nano plays Happy Birthday on the buzzer. Closing the lid stops the tune. Change the notes and lengths in the two lists to play any tune you like.
Tick anything you already have and the total updates.
The brain of the project
Works with the same pins and code as the original Arduino Nano.
Glass switch whose contacts close when a magnet is near.
Follow the colours: red is power, dark grey is ground, blue is a signal.
musical_gift_box.inoVerify & upload in Electrolark// Musical gift box that plays when opened
// Geekobot project library · board: Arduino Nano
// Wiring diagram, parts list and build steps: Geekobot project page "musical-gift-box"
// ---- pins ----
const uint8_t REED_PIN = 2;
const uint8_t PIEZO_PIN = 3;
// ---- latest readings ----
bool doorOpen = false;
// Happy Birthday: note frequencies (Hz) and lengths (ms); 0 is a rest
const int MELODY[] = {262,262,294,262,349,330,0, 262,262,294,262,392,349,0, 262,262,523,440,349,330,294,0, 466,466,440,349,392,349};
const int DUR[] = {300,150,450,450,450,900,200, 300,150,450,450,450,900,200, 300,150,450,450,450,450,900,200, 300,150,450,450,450,900};
const int N = sizeof(MELODY) / sizeof(MELODY[0]);
bool played = false;
void setup() {
Serial.begin(115200);
pinMode(REED_PIN, INPUT_PULLUP);
}
void loop() {
doorOpen = digitalRead(REED_PIN) == HIGH; // lid lifted: magnet moves away from the reed switch
if (!doorOpen) { played = false; delay(50); return; }
if (played) { delay(50); return; }
played = true;
Serial.println(F("Lid open: playing"));
for (int i = 0; i < N; i++) {
if (digitalRead(REED_PIN) == LOW) break; // stop if the lid closes
if (MELODY[i]) tone(PIEZO_PIN, MELODY[i], DUR[i] * 9 / 10);
delay(DUR[i]);
}
noTone(PIEZO_PIN);
}
The sketch above runs here on a simulated Arduino Nano. Move the sliders for the sensors and buttons it reads, and watch the Serial monitor, display and outputs change.
Bring the magnet within 1 cm and align it parallel to the glass body.
Normal for a bare piezo; mount it on a small box to resonate.
Open the Serial Monitor at 115200 baud and check for messages. Make sure you chose the right board and port in the Arduino IDE.
Close other programs using the serial port, try another USB cable (some are charge-only) and press reset just before uploading.
Yes. Most of the code works unchanged on an ESP32 with the ESP32 board package. Watch for 5 V modules: ESP32 pins are 3.3 V, so 5 V output signals need a resistor divider.
Use a 5 V USB phone charger or a power bank into the board's USB port.
6 parts: Nano CH340 Development Board (Soldered), Y213 Reed Switch Normally Open (glass), Piezo Buzzer (passive), Battery Holder (AA), Solderless Breadboard (830 points) and Dupont Jumper Wires (40 pcs). Tools: Digital Multimeter and Wire Stripper and Cutter. On this page you can tick the parts you already have and add only the rest to your cart.
It is a beginner project and takes about 1 hour. The build on this page has 8 steps.
No. This page has the complete, tested code (musical_gift_box.ino).
Yes. The Run it panel under the code runs this exact sketch on a simulated board. Move the sliders or press the buttons for the sensors it reads, and the Serial monitor, display and outputs react as they will on the real build. It is a simulation, so real readings depend on your parts and wiring.
About ₹322 for the parts at the prices on this page, plus tools if you do not have them. Quantity price breaks lower the cost when you build several.
Generating musical notes with tone(); Pull-up resistors and reed switches.
Ideas that reuse this build: Keypad mini piano and Talking doorbell. They are listed under Take it further on this page.
Clap twice and the lights come on. Clap twice again to switch them off.
Plays a friendly ding-dong whenever someone walks into the shop or room.
Beeps and flashes any message in Morse, or key your own by hand.
Turn a membrane keypad into a two-octave piano.
Learning project. Check every circuit and the datasheet of each part before you power it up.
Lid open: playing
A simulation of the sketch above, so you know what to expect. Real readings vary with your parts and wiring.