title: "Door Sensor Alert System with La Cucaracha Melody" description: "Create a small, humorous security innovation using Arduino and a Magnetic Reed Switch to detect door status."
In this project, we will bring a plain door to life by building a "Smart Door Sounder". We will program the system to monitor the door's status, and when the door "closes", our Arduino board will instruct a speaker to play the classic Mexican melody "La Cucaracha" using the powerful tone() function.
This learning experience is not just about creating music, but also about understanding the fundamentals of Digital Input, Logic States, and controlling audio frequencies in embedded systems.
Hardware Components
To complete this project, we will need the following electronic components:
- Arduino Board (Uno, Nano, or other models): Serves as the main processing unit.
- Magnetic Reed Switch: A switch that activates when a magnetic field approaches (used to detect door closure).
- Piezo Buzzer or small speaker: For converting electrical signals into sound waves.
- Resistor (220Ω - 1kΩ): To limit current for the Buzzer.
- Jumper wires and Breadboard: For circuit connections.
Engineering Principles
1. Detection with Magnetic Reed Switch
A magnetic sensor consists of two small metal plates enclosed in a vacuum glass tube. When a magnet (attached to the door frame) moves close, the magnetic force pulls the plates together, completing the circuit. In terms of software, we will read this value via digitalRead() to check if the state changes from HIGH to LOW (when using an Internal Pull-up).
2. Sound Generation with tone() Function
The core of sound generation in Arduino is the tone(pin, frequency, duration) function, which creates a Square Wave with a specific Frequency according to musical notes, such as:
- Note C4: 262 Hz
- Note F4: 349 Hz
- Note A4: 440 Hz
When these frequencies are arranged correctly with proper Timing, we will get the complete "La Cucaracha" melody.
Code Logic
For the system to function accurately, our code will divide the operation into three main parts:
- Input Sensing: Continuously check the state of the Digital pin connected to the Reed Switch within
loop(). - Edge Detection: The system will not play the song repeatedly, but will only play "when the door is closing" (Transition from Open to Closed).
- Melody Sequence: Once the conditions are met, the function will call the Array of La Cucaracha musical notes to play.
// Example code structure for La Cucaracha melody
#define BUZZER_PIN 8
#define REED_SWITCH_PIN 2
void setup() {
pinMode(REED_SWITCH_PIN, INPUT_PULLUP); // Using internal Pull-up for convenience
pinMode(BUZZER_PIN, OUTPUT);
}
void playLaCucaracha() {
// La Cucaracha melody notes (C4, C4, C4, F4, A4...)
int melody[] = {262, 262, 262, 349, 440};
int durations[] = {200, 200, 200, 400, 400};
for (int i = 0; i < 5; i++) {
tone(BUZZER_PIN, melody[i], durations[i]);
delay(durations[i] * 1.3); // Pause between notes
}
}
void loop() {
int doorState = digitalRead(REED_SWITCH_PIN);
// If sensor detects magnet (door closed), value will be LOW
if (doorState == LOW) {
playLaCucaracha();
while(digitalRead(REED_SWITCH_PIN) == LOW); // Wait until door opens again to prevent repeat playback
}
}
Project Overview (Media)
(Retain original image and video positions)
So, in this proyect, we will make a sensor for your door. We will programe it so that when the door closes, It plays La Cucaracha. We will use a function called tone.
Conclusion of Experiment
From this experiment, we found that using tone() in conjunction with Timing management (delay()) is a simple and effective method for notifying various Hardware statuses. This project can be applied in various ways, such as a safe alert system, an anti-theft system, or even a welcome sound system in shops, by changing the melody according to your needs.