LED & MUSIC W/ Volume Control

by JayoMayo in Circuits > Arduino

61 Views, 0 Favorites, 0 Comments

LED & MUSIC W/ Volume Control

megamain.jpg

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

20240830_105626.jpg

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

20240830_105016.jpg

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

20240830_105121.jpg

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

20240830_105147.jpg

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

20240830_105320.jpg

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

20240830_105628.jpg
20240830_105735.jpg

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

20240830_105948.jpg

Next connect a wire from the middle of the potentiometer to the slot one above the negative end of the speaker.

Second Speaker

20240830_110234.jpg

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

20240830_110252.jpg

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

20240830_104712.jpg
20240830_105033.jpg

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)

20240830_110330.jpg

Next, plug the Arduino into a computer.

Tone() Code

Copy & Paste this code:

#include "pitches.h"
int melody[] = {
NOTE_D4, NOTE_D4, NOTE_G4, NOTE_D4, NOTE_F4, NOTE_D4,0,0
};
// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
8, 8, 8, 8, 8, 8, 4,1
};
void setup() {
int numberOfNotes = sizeof(melody) / sizeof(melody[0]);
// Iterate over the notes of the melody:
for (int thisNote = 0; thisNote < numberOfNotes; thisNote++) {
int noteDuration = 1000 / noteDurations[thisNote];
if (melody[thisNote] != 0) {
tone(8, melody[thisNote], noteDuration);
}
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
noTone(8);
}
}
void loop() {
setup();
}

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.

/*************************************************

 * Public Constants

 *************************************************/
#define NOTE_B0 31
#define NOTE_C1 33
#define NOTE_CS1 35
#define NOTE_D1 37
#define NOTE_DS1 39
#define NOTE_E1 41
#define NOTE_F1 44
#define NOTE_FS1 46
#define NOTE_G1 49
#define NOTE_GS1 52
#define NOTE_A1 55
#define NOTE_AS1 58
#define NOTE_B1 62
#define NOTE_C2 65
#define NOTE_CS2 69
#define NOTE_D2 73
#define NOTE_DS2 78
#define NOTE_E2 82
#define NOTE_F2 87
#define NOTE_FS2 93
#define NOTE_G2 98
#define NOTE_GS2 104
#define NOTE_A2 110
#define NOTE_AS2 117
#define NOTE_B2 123
#define NOTE_C3 131
#define NOTE_CS3 139
#define NOTE_D3 147
#define NOTE_DS3 156
#define NOTE_E3 165
#define NOTE_F3 175
#define NOTE_FS3 185
#define NOTE_G3 196
#define NOTE_GS3 208
#define NOTE_A3 220
#define NOTE_AS3 233
#define NOTE_B3 247
#define NOTE_C4 262
#define NOTE_CS4 277
#define NOTE_D4 294
#define NOTE_DS4 311
#define NOTE_E4 330
#define NOTE_F4 349
#define NOTE_FS4 370
#define NOTE_G4 392
#define NOTE_GS4 415
#define NOTE_A4 440
#define NOTE_AS4 466
#define NOTE_B4 494
#define NOTE_C5 523
#define NOTE_CS5 554
#define NOTE_D5 587
#define NOTE_DS5 622
#define NOTE_E5 659
#define NOTE_F5 698
#define NOTE_FS5 740
#define NOTE_G5 784
#define NOTE_GS5 831
#define NOTE_A5 880
#define NOTE_AS5 932
#define NOTE_B5 988
#define NOTE_C6 1047
#define NOTE_CS6 1109
#define NOTE_D6 1175
#define NOTE_DS6 1245
#define NOTE_E6 1319
#define NOTE_F6 1397
#define NOTE_FS6 1480
#define NOTE_G6 1568
#define NOTE_GS6 1661
#define NOTE_A6 1760
#define NOTE_AS6 1865
#define NOTE_B6 1976
#define NOTE_C7 2093
#define NOTE_CS7 2217
#define NOTE_D7 2349
#define NOTE_DS7 2489
#define NOTE_E7 2637
#define NOTE_F7 2794
#define NOTE_FS7 2960
#define NOTE_G7 3136
#define NOTE_GS7 3322
#define NOTE_A7 3520
#define NOTE_AS7 3729
#define NOTE_B7 3951
#define NOTE_C8 4186
#define NOTE_CS8 4435
#define NOTE_D8 4699
#define NOTE_DS8 4978

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[].