LED & MUSIC W/ Volume Control
For this project, I have created a simple circuit making use of the free-to-use Arduino code for the tone function with slight modifications. The original code is from "Play a Melody using the tone() function" article by Arduino. This circuit by default plays the signature riff from "Bad to the Bone" from George Thorogood. An LED and two different types of buzzers. The second buzzer is an attempt to recreate the distortion effect of an electric guitar. The LED will light up every time a note is played, and the potentiometer can be used to control the incoming power, therefore changing the brightness of the LED and the volume of the speakers. I ended up making this project after messing around in my Intro to Mechatronics class. Feel free to make any modifications!
Supplies
You will need:
-5 Wires (Double-ended)
-Potentiometer
-LED (Any color)
-Speaker with the circle holes dotted in the middle (The one in the picture)
-Buzzer
-Arduino Uno R3
-USB cord that is applicable with the Arduino
-Motivation
Arduino Setup
Take one wire and plug it into the ground (GND) pin of the Arduino. Take another wire and plug it into the 8th pin of the Arduino on the other side. *Please make sure you have placed it on this pin specifically, otherwise the code I have provided will not work.
Starting the Breadboard
Take the wire connected to the GND and plug the other end into the 5th pin of the negative power rail. (The first row)
More Wiring
Next take a new wire and connect one end to the first pin of the negative power rail. Connect the other end to the first pin of the terminal strips.
Potentiometer
At this point, take your potentiometer and make sure the middle pin is facing the forward (pointing at the Arduino). Place the left leg of the potentiometer 3 pins below the wire you placed in the previous step (same column).
Speaker Setup
Place the negative end of big buzzer on 28 E of the breadboard. The positive end should go on 23 F of the breadboard. If done correctly the big buzzer should be diagonal.
Connection
Next connect a wire from the middle of the potentiometer to the slot one above the negative end of the speaker.
Second Speaker
The second buzzer should be placed right next to the speaker. Next connect a wire starting from one pin below the positive end of the buzzer leading into the positive leg of the buzzer. Make sure there is a one pin gap between the buzzer and the wire for the next part.
LED
Place an LED in the space that was left open. Make sure the legs of the buzzer and LED are lined up so that the LED is directly below it.
Wire Back to Arduino
Take the wire that is connected to pin 8 of the Arduino and finally attach it to the space below the left leg of the LED.
Finish! (Almost)
Next, plug the Arduino into a computer.
Tone() Code
Copy & Paste this code:
Explanation:
There are two arrays, one that contains the note durations and the notes that correspond to the pitches.h file that will be created in the next step. This code is mostly from the Arduino "Play a melody using the tone() function" however it does have some slight changes. The setup call is looped in order to make the notes play infinitely and another aspect I will go into soon. In this setup() call the function first loops through and plays each note, checking to see if the note is not zero ( if it is tone() will not be called). The built in Arduino function Tone() is called and the parameters frequency and pin number are passed in. The delay between notes is calculated counting by milliseconds. 1000 which is 1 beat in 60bpm is split based on the note duration. A quarter note would in this case be 250ms. A flaw in this current system would be the inability to change the bpm of the song, however this can be added by creating a new variable called bpm. Then the formula for getting the time between notes would be ( (1000 / bpm) * 60 ) / noteDurations(thisNote) ) . The delay() function is called to delay the notes. After the delay, the note is silenced using the noTone() function.
Pitches.h
Go into the Arduino IDE and press Control Shift N. This should open a prompt to rename something. Name this new file PItches.h. Then copy paste the following into the file.
Explanation:
This is a file that contains all the pitch values for the buzzer to play each note. Each of these pitch values is defined as a note name. The note goes by the format NOTE_[Letter][Octave]
Save and Upload!
Save and Upload your Arduino and enjoy! You can change the notes in the code by swapping out the notes in the notes[] array and changing the noteDurations[].