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.
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
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.
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.
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.
Step 3 - Move Circuit to the BoxPut 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!
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.
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'.
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.
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.
Comments