Project - Musical Doorbell

# Musical Doorbell Let’s create a simple music doorbell using a button and a passive buzzer! When a guest presses the doorbell button, the music will play. **Wiring Instructions:** 1. **Buzzer**: - Connect the buzzer’s **negative (-)** terminal to **GND**. - Connect the buzzer’s **positive (+)** terminal to **Arduino GPIO3**. 1. **Button**: - Connect one terminal of the button to **Arduino GPIO4**. - Connect the other terminal of the button to **3.3V**. - Since pressing the button sets GPIO4 to HIGH, use a **10K pull-down resistor** to ensure the default state is LOW when the button is not pressed. With this setup, the buzzer will play music whenever the button is pressed. You can write your own music or using any classic music from arduino-songs collection in github. https://github.com/robsoncouto/arduino-songs ![Pasted image 20250126174009.png](https://cdn.shopify.com/s/files/1/0331/9994/7908/files/Pasted_image_20250126174009.png?v=1741564007) Choose your favourite song from arduino-songs library. Let’s update the code to implement the functionality of the doorbell. ``` #define BTN_PIN 4 ``` At the top of the code, add this line to define the button pin using Arduino GPIO4. ```cpp // change this to Buzzer pin GPIO 3 int buzzer = 3; ``` Find this line, and change the buzzer uses Arduino GPIO3. ```cpp void playMusic() { // iterate over the notes of the melody. // Remember, the array is twice the number of notes (notes + durations) for (int thisNote = 0; thisNote < notes * 2; thisNote = thisNote + 2) { // calculates the duration of each note divider = melody[thisNote + 1]; if (divider > 0) { // regular note, just proceed noteDuration = (wholenote) / divider; } else if (divider < 0) { // dotted notes are represented with negative durations!! noteDuration = (wholenote) / abs(divider); // increases the duration in half for dotted notes noteDuration *= 1.5; } // we only play the note for 90% of the duration, leaving 10% as a pause tone(buzzer, melody[thisNote], noteDuration * 0.9); // Wait for the specief duration before playing the next note. delay(noteDuration); // stop the waveform generation before the next note. noTone(buzzer); } } ``` Relocate all the code from the setup() function into a new playMusic() function, then add this function above the existing setup() function. ```cpp void setup() { pinMode(BTN_PIN,INPUT); } void loop() { if (digitalRead(BTN_PIN) == HIGH) { delay(300); // button debounce playMusic(); } } ``` In the setup function, configure the button pin as an input. In the loop function, check if the button is pressed, and if it is, call playMusic() to play the song. At this point, our music doorbell is fully implemented. Now, you might wonder: what if we could press the button again while the music is playing to stop it? Next, let’s try to implement this feature. In the playMusic(), there is a for loop which loop the melody and play each note. All we have to do is to add the following code just before the for loop ending. If the buttons is press, break the for and quite. ```cpp void playMusic() { for (int thisNote = 0; thisNote < notes * 2; thisNote = thisNote + 2) { // ....... if (digitalRead(BTN_PIN) == HIGH) { delay(300); break; } } } ``` # The Complete Code ```cpp #define BTN_PIN 2 #define NOTE_B0 31 // ... ... #define REST 0 int tempo = 200; int buzzer = 3; int melody[] = { NOTE_E5,8, NOTE_E5,8, REST,8, NOTE_E5,8, REST,8, NOTE_C5,8, NOTE_E5,8, //1 // ... ... NOTE_G4,8, NOTE_D4,8, NOTE_E4,-2, }; int notes = sizeof(melody) / sizeof(melody[0]) / 2; int wholenote = (60000 * 4) / tempo; int divider = 0, noteDuration = 0; void playMusic() {) for (int thisNote = 0; thisNote < notes * 2; thisNote = thisNote + 2) { divider = melody[thisNote + 1]; if (divider > 0) { noteDuration = (wholenote) / divider; } else if (divider < 0) { noteDuration = (wholenote) / abs(divider); noteDuration *= 1.5; } tone(buzzer, melody[thisNote], noteDuration * 0.9); delay(noteDuration); noTone(buzzer); if (digitalRead(BTN_PIN) == HIGH) { delay(300); break; } } } void setup() { pinMode(BTN_PIN,INPUT); } void loop() { if (digitalRead(BTN_PIN) == HIGH) { delay(300); playMusic(); } } ```

RELATED ARTICLES