Musical Relay Switch
This code allows you to control an LED, play a melody, and activate a relay using a pushbutton. When you press the button, the LED turns on, the relay briefly activates, and the buzzer plays a melody. When you release the button, the LED turns off, and the relay deactivates. This project is a good introduction to combining different components in an Arduino project and reacting to user input with actions. It also introduces the concept of debouncing to avoid accidental button presses. You can visit and simulate the circuit HERE.
This Arduino code is a project for beginners that combines different components and actions, including controlling an LED, playing a melody on a buzzer, and activating a relay when a button is pressed. Let me break it down step by step in a beginner-friendly way:
1. Definitions of Musical Notes:
- The code starts by defining musical notes and their corresponding frequencies. These definitions will be used later to create a melody.
2. Variable Declarations:
- Next, the code declares some variables that will be used in the program. Here's what each variable is for:
- `relayPin`: This is the pin number for controlling a relay. A relay is an electrical switch that can be turned on or off.
- `buttonPin`: This is the pin number for a pushbutton, which is like a simple switch.
- `ledPin`: This is the pin number for an LED, which is a small light.
- `buzzer`: This is the pin number for a buzzer, which can produce sound.
- Various variables like `buttonState`, `ledState`, `lastDebounceTime`, and `debounceDelay` are used for managing the button and LED.
3. Melody Array:
- The code defines a melody as an array called `melody`. Each element in the array represents a musical note and its duration. The melody is what the buzzer will play.
4. Setup Function (`setup()`):
- The setup function is executed only once when the Arduino is powered on.
- In this function:
- Serial communication is initialized for debugging (printing messages to the computer).
- Pins are configured as inputs or outputs, such as the LED pin and button pin.
- The relay pin is also configured as an output.
5. Main Loop Function (`loop()`):
- The loop function runs continuously, over and over.
- It does the following:
- Reads the state of the pushbutton (whether it's pressed or not).
- Checks for button press and filters out noise using a debounce mechanism to prevent false triggers.
- If the button is pressed and the LED is off:
- The LED is turned on.
- A relay is activated, and it's kept on for a short duration.
- A melody is played on the buzzer.
- If the button is released and the LED is on:
- The LED is turned off.
- The relay is deactivated.
- The melody is played using the `tone()` function, and there are delays between notes to control the rhythm of the melody.
6. Custom Function (`ActivateRelay()`):
- This is a separate function you've defined in your code.
- It turns on the relay for a specified duration and then turns it off.
In summary, this code allows you to control an LED, play a melody, and activate a relay using a pushbutton. When you press the button, the LED turns on, the relay briefly activates, and the buzzer plays a melody. When you release the button, the LED turns off, and the relay deactivates. This project is a good introduction to combining different components in an Arduino project and reacting to user input with actions. It also introduces the concept of debouncing to avoid accidental button presses.
// Int for Relayint relayPin =3;// constants won't change. They're used here to set pin numbers:const int buttonPin = 7; // the number of the pushbutton pinconst int ledPin = 12; // the number of the LED pin// change this to whichever pin you want to use for speaker int buzzer = 2;// variables will change:int buttonState = 0; // variable for reading the pushbutton status// change this to make the song slower or fasterint tempo = 225;
const int melody[] PROGMEM = {
NOTE_E2, 8, NOTE_E2, 8, NOTE_E3, 8, NOTE_E2, 8, NOTE_E2, 8, NOTE_D3, 8, NOTE_E2, 8, NOTE_E2, 8, //1 NOTE_C3, 8, NOTE_E2, 8, NOTE_E2, 8, NOTE_AS2, 8, NOTE_E2, 8, NOTE_E2, 8, NOTE_B2, 8, NOTE_C3, 8, NOTE_E2, 8, NOTE_E2, 8, NOTE_E3, 8, NOTE_E2, 8, NOTE_E2, 8, NOTE_D3, 8, NOTE_E2, 8, NOTE_E2, 8, NOTE_C3, 8, NOTE_E2, 8, NOTE_E2, 8, NOTE_AS2, -2,
NOTE_E2, 8, NOTE_E2, 8, NOTE_E3, 8, NOTE_E2, 8, NOTE_E2, 8, NOTE_D3, 8, NOTE_E2, 8, NOTE_E2, 8, //5 NOTE_C3, 8, NOTE_E2, 8, NOTE_E2, 8, NOTE_AS2, 8, NOTE_E2, 8, NOTE_E2, 8, NOTE_B2, 8, NOTE_C3, 8, NOTE_E2, 8, NOTE_E2, 8, NOTE_E3, 8, NOTE_E2, 8, NOTE_E2, 8, NOTE_D3, 8, NOTE_E2, 8, NOTE_E2, 8, NOTE_C3, 8, NOTE_E2, 8, NOTE_E2, 8, NOTE_AS2, -2,
NOTE_E2, 8, NOTE_E2, 8, NOTE_E3, 8, NOTE_E2, 8, NOTE_E2, 8, NOTE_D3, 8, NOTE_E2, 8, NOTE_E2, 8, //9 NOTE_C3, 8, NOTE_E2, 8, NOTE_E2, 8, NOTE_AS2, 8, NOTE_E2, 8, NOTE_E2, 8, NOTE_B2, 8, NOTE_C3, 8, NOTE_E2, 8, NOTE_E2, 8, NOTE_E3, 8, NOTE_E2, 8, NOTE_E2, 8, NOTE_D3, 8, NOTE_E2, 8, NOTE_E2, 8, NOTE_C3, 8, NOTE_E2, 8, NOTE_E2, 8, NOTE_AS2, -2,
NOTE_E2, 8, NOTE_E2, 8, NOTE_E3, 8, NOTE_E2, 8, NOTE_E2, 8, NOTE_D3, 8, NOTE_E2, 8, NOTE_E2, 8, //13 NOTE_C3, 8, NOTE_E2, 8, NOTE_E2, 8, NOTE_AS2, 8, NOTE_E2, 8, NOTE_E2, 8, NOTE_B2, 8, NOTE_C3, 8, NOTE_E2, 8, NOTE_E2, 8, NOTE_E3, 8, NOTE_E2, 8, NOTE_E2, 8, NOTE_D3, 8, NOTE_E2, 8, NOTE_E2, 8, NOTE_FS3, -16, NOTE_D3, -16, NOTE_B2, -16, NOTE_A3, -16, NOTE_FS3, -16, NOTE_B2, -16, NOTE_D3, -16, NOTE_FS3, -16, NOTE_A3, -16, NOTE_FS3, -16, NOTE_D3, -16, NOTE_B2, -16,};// sizeof gives the number of bytes, each int value is composed of two bytes (16 bits)// there are two values per note (pitch and duration), so for each note there are four bytesint notes = sizeof(melody) / sizeof(melody[0]) / 2;
// this calculates the duration of a whole note in msint wholenote = (60000 * 4) / tempo;
int divider = 0, noteDuration = 0;
//********************* BEGIN VOID SETUP **************************************************void setup() {
// initialize the LED pin as an output: pinMode(ledPin, OUTPUT); // initialize the pushbutton pin as an input: pinMode(buttonPin, INPUT);
//initialize Relay as OUTPUT pinMode(relayPin, OUTPUT);
// initialize the LED pin as an output: pinMode(ledPin, OUTPUT); // initialize the pushbutton pin as an input: pinMode(buttonPin, INPUT); } // END OF VOID SETUP()**********************************************************
//***************************** START VOID LOOP *****************************************************void loop() { // read the state of the pushbutton value: buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed. If it is, the buttonState is HIGH: if (buttonState == HIGH) { //Call Function to activate Relay on seperate timer ActivateRelay(500); // turn LED on: digitalWrite(ledPin, HIGH); // 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 = pgm_read_word_near(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); noteDuration *= 1.5; // increases the duration in half for dotted notes }
// we only play the note for 90% of the duration, leaving 10% as a pause tone(buzzer, pgm_read_word_near(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); } } if (buttonState == LOW) { // turn LED off: digitalWrite(ledPin, LOW); } } // *****************************************closing bracket for void loop()
//************************* OUTSIDE of Loop**************************************void ActivateRelay(int delayTime){ // swith relay on: digitalWrite(relayPin, HIGH); delay(delayTime); // swith relay off: digitalWrite(relayPin, LOW); delay(delayTime);}
"Musical Doorbell Relay: Combining Tech and Creativity"
Introduction:
In the world of DIY electronics and home automation, one of my recent projects was to bring a touch of music and technology to the classic doorbell. I've created a "Musical Doorbell Relay" that not only activates the standard doorbell upstairs but also plays the iconic Doom theme song in my lab/office when someone rings the doorbell. And it doesn't stop there! I've even 3D printed a custom doorbell design with my own push button to complete the setup.
Project Overview:
This project combines various elements, including a relay, an Arduino board, a buzzer, and a 3D-printed doorbell, to create a unique and entertaining doorbell system. Here's how it works:
1. The Musical Component:
- I've incorporated a buzzer into the system to play the Doom theme song when the doorbell rings. This adds an element of surprise and fun to the whole experience.
2. Activating the Standard Doorbell:
- The relay is used to trigger the standard doorbell upstairs, ensuring that the conventional functionality is retained.
3. Custom 3D-Printed Doorbell:
- To make this project truly unique, I designed and 3D printed a new doorbell. It not only looks great but also houses the custom-made push button that integrates seamlessly into the circuit.
How It Works:
When someone rings the doorbell, the relay is activated, triggering the standard doorbell sound upstairs. Simultaneously, the buzzer in my lab/office plays the Doom theme song, adding an element of personalization to the experience.
Conclusion:
This project has been a delightful combination of creativity, technology, and a touch of nostalgia. The ability to customize my doorbell's sound and design has not only added a unique twist to my home but also showcased the endless possibilities of DIY electronics.
Stay tuned for more DIY projects that bring technology and creativity together in exciting ways! If you have any questions or want to learn more about how you can create your own Musical Doorbell Relay, feel free to reach out. Happy tinkering!
Donate
If you've enjoyed exploring my Arduino projects and want to see more amazing creations, your support can make a big difference! By contributing, you're helping me continue to innovate and bring even more exciting projects to life. Together, we can explore the endless possibilities of DIY electronics! Don't forget to like, subscribe, and follow for updates on the latest developments. Thank you for being a part of this journey!
Click here to make a difference with your donation today!