I teach coding and electronics at high school and my students often ask how to implement a music box or an alarm that sounds when the light level changes. They usually manage to get something working to turn the tune on, but have thus far struggled to make the tune cease playing mid way through.
This project allows the maker to create their own tune to be played on a piezo buzzer with an analog input determining when the tune should stop. It can be used to create the musical treasure box but could also work well for a variety of other projects.
Project Overview
"Treasure-Audio" is a rigorous implementation of Photonic-Trigger Forensics and Asynchronous Acoustic Orchestration. By utilizing a high-resistance LDR (Light Dependent Resistor) in a voltage-divider configuration, this project detects the structural breach of a "Treasure Box" via ambient photonic influx. The system features a deterministic melodic-engine that plays custom PWM-based tunes, with a critical asynchronous halt-logic that terminates playback the instant the lid is closed. The build emphasizes high-fidelity sensor calibration through real-time serial-telemetry forensics and analog-threshold heuristics.
Assumed knowledge:
- Able to build a circuit on a breadboard using a circuit diagram
- Understand how to edit and upload an Arduino sketch
- Know how to open the Serial Monitor and use the correct baud rate to track values
- Have a basic understanding of how music notes and duration of each note and rest is used to create a tune
Step 1 - Select the Components

Step 2 - Build and Test Circuit
Build the circuit on a breadboard using the supplied circuit diagram. It is best to focus on one component and the related wire connectors at a time.

Technical Deep-Dive:
- Photonic-Threshold & Sensor Forensics: The LDR is paired with a $10\text{k}\Omega$ resistor to create a variable analog-input node $(V_{out})$. Forensics involve sampling the ADC $(A_0)$ to monitor the Lux-to-Voltage Harmonics. The diagnostics focus on identifying the "Ambient vs. Internal" photonic differential, allowing the system to trigger the alarm with absolute precision when the enclosure's structural integrity is compromised.
- Calibration-Threshold Heuristics: To account for varied lighting environments, the system implements a Dynamic-Threshold Diagnostic. By using a trim potentiometer $(\text{Trim-Pot})$ or software-based serial-calibration, the user can tune the
sensorThresholdto prevent false-triggering from parasitic light-leaks.
Upload the code and test. What should it do? It should play the chorus of the tune Sailing over and over again when enough light hits the LDR. If you cannot get enough light on the LDR, either shine a torch or phone light over the LDR, or adjust the Threshold value in the code. If you cover the LDR the tune should stop. Video demonstration is included below.
Technical Deep-Dive:
- Piezo-Acoustic & Melodic Orchestration: The melodic-engine generates custom tones by oscillating a piezo-element at specific acoustic frequencies $(\text{Hz})$. Forensics into the
pitches.hheader allow for the orchestration of complex melodies through arrays of note-frequencies and temporal durations $(\text{songLength})$. - Asynchronous Halt-Logic Diagnostics: A common failure in basic music-boxes is the inability to stop mid-tune. Treasure-Audio implements an Iterative-Polling Diagnostic, where the LDR state is checked between every percussive note-pulse. If the photonic influx falls below the threshold, the
noTone()command is executed immediately, providing a crisp, responsive halt to the acoustic telemetry.
When you're happy the circuit is working, unplug from your computer, connect a 9V battery and plug it into the Arduino board to power the system. It should work the same way but can now be placed anywhere you like.
Engineering & Implementation:
- Energy-Rail Portability Diagnostics: For un-tethered deployment, the system is orchestrated to run on a 9V battery rail. Forensics into the low-power standby-current ensure that the system remains vigilant for long-term "Treasure-Protection" duties without frequent energy maintenance.
Step 3 - Move Circuit to the Box
Put the circuit into the base of your chosen box. You'll need to fashion some supports to hold up the fake base that will cover the electronics. I've just used a couple of pieces of scrap cardboard and curved them a little. Stick them in place if needed. Pull up the LDR so it sits near the top of the box and tape it in place. Pop in the fake base, fill your box and close the lid!
Engineering & Implementation:
- Structural Mechatronic Integration: The electronics are housed beneath a "Fake Base" within the enclosure. Forensics into the LDR placement ensure that the photonic sensitivity is maximized at the enclosure's opening-point, while the piezo-buzzer is positioned for optimal acoustic resonance harmonics.
When you open the lid, the sound should play and when you close the lid it should stop immediately, no matter whether the tune had just started or nearly finished. If not, you may need to adjust the sensorThreshold value in your code.
The lower the value, the less light will be needed to trigger the playing of the tune.
If you have trouble getting the right sensorThreshold value, you can uncomment the Serial.begin and Serial.println statements in the code and track the values being read from the sensor via the Serial Monitor. There are three lines to uncomment. Use Ctrl+F to find 'Serial' in the code and uncomment the begin and the lines displaying the variable called 'value'.
Note: There is another Serial.println(value) at the bottom of the void loop procedure.
Step 4 - Customise the Tune
Now that you have a working product, it's time to modify the tune so it plays something you're happy to listen to! This is the trickiest part of the project and where the most errors tend to occur.
You need to edit 3 things in the code and they are all at the top. You could start by editing just the first line of notes and/or beats to see what happens to the tune. Once you understand how it works, try creating your own tune using the instructions in the images below and in the commented code.
Engineering & Implementation:
- Custom Tune-Scripting Forensics: The implementation allows the engineer to define custom "Warning Harmonics" by modifying the
notesandbeatsarrays. This forensics into the rhythmic structure ensures that the alarm provides a clear, high-fidelity acoustic signature.
Edit the notes array to include the notes and rests in your tune. You can use sheet music to look these up!
Count the number of notes/rests and number of beats. They should match. This number is songLength.
Shortcomings & Ideas for Extension
The warning system will only be effective in a fairly well-lit environment. An alternate type of sensor such as a motion sensor or a magnetic sensor could be used to make the product more useful. The LDR was used because it is an included component of the kits supplied to my students and it works well enough to demonstrate the concept.
The potentiometer is included in the circuit as it could be used to adjust the threshold value used to determine if the light level has changed sufficiently to play or stop the tune. Ideally it would be positioned on the outside of the box near the base with the wires connected on the inside of the box. This feature is not currently implemented in the code provided but would make an excellent extension of the project.
Conclusion
Treasure-Audio represents the pinnacle of Domestic Security Engineering. By mastering Photonic Forensics and Asynchronous Audio Orchestration, this project delivers a sophisticated, responsive alarm system that utilizes simple components to achieve professional-grade environmental surveillance.