Unleash your inner musician with this DIY project
Here are the Components needed:
To build a music player using Arduino and a buzzer, you will need the following components:
- An Arduino
- A 5V battery
- A passive buzzer
- A speaker
This project can be simulated using Proteus 8. You don’t need to download a passive buzzer module, as the buzzer in Proteus works just fine.
Circuit diagram for the mp3 player with Arduino and buzzer only .
Explanation of the code:
The variables:
int dt = 100;
This is the delay variable. The delay is used to pause the code for a couple of seconds. In Arduino, this function is called 'delay'. Specifically, the value for delay in this code is 100 microseconds. To make the code more efficient, instead of writing '100 microseconds' every time we want to call the function, we store it in a container (variable) labeled 'dt'."
void setup() {
pinMode(4, OUTPUT); // set a pin for buzzer output
}
This function tells the Arduino to send voltage to the buzzer. ‘void setup’ is a function in Arduino used to setup built-in commands that will be used in the code. In this project, we set up the pinMode for pin 4 to output. In the circuit diagram, you will notice that we connected the buzzer to pin 4. pinMode is a built-in function in Arduino that tells it what to do with a pin - either input or output. This time, we set it to output because it is going to send voltage to the buzzer. The buzzer can only be turned on with voltage.”
void buzz(int targetPin, long frequency, long length) {
long delayValue = 250000/frequency/2; // calculate the delay value between transitions
//// 0.25 second's worth of microseconds, divided by the frequency, then split in
half since
//// there are two phases to each cycle
long numCycles = frequency * length/ 250; // calculate the number of cycles for proper
timing
//// multiply frequency, which is really cycles per second, by the number of seconds to
//// get the total number of cycles to produce
for (long i=0; i < numCycles; i++){ // for the calculated length of time...
digitalWrite(targetPin,HIGH); // write the buzzer pin high to push out the sound
delayMicroseconds(delayValue); // wait for the calculated delay value
digitalWrite(targetPin,LOW); // write the buzzer pin low to pull back the sound
delayMicroseconds(delayValue); // wait again or the calculated delay value
}
void buzz is a user defined function (i created it),i told the function to accept buzzpin ,frequency and length. buzzpin- this is where the buzzer is connected to . frequency- this is the frequency of each note in a music scale (or piano). length - its how long each note should play during the song.
long delayValue = 250000/frequency/2; // calculate the delay value between transitions
//// 0.25 second's worth of microseconds, divided by the frequency, then split in half since
//// there are two phases to each cycle
The delayValue variable tells the Arduino how long to leave the buzzer on. The difference between this and length is that length is for each note while delayValue affects the buzzer. The delay value is affected by the length. The 250000 in the code is used to convert the frequency to milliseconds. This is because frequency is in Hertz and Hertz is calculated in seconds, not milliseconds. The comparison we used is 1000 microseconds = 1 second and 1000 microseconds = 1,000,000 milliseconds. Hence, 250 microseconds = 250,000 milliseconds or 0.25 seconds. It is divided by two because we want it to affect either the positive phase or the negative phase. You will see the effect of that in the for loop area.
long numCycles = frequency * length/ 250; // calculate the number of cycles for proper timing
//// multiply frequency, which is really cycles per second, by the number of seconds to
//// get the total number of cycles to produce
This line of code calculates the number of cycles needed to produce the desired note. The numCycles variable is calculated by multiplying the frequency (cycles per second) by the length (in seconds) and then dividing by 250. This gives the total number of cycles needed to produce the note for the desired length of time. It is divided by 250 because of the specified length, which is 250. Without this division, it would not give the total number of cycles. for (long i=0; i < numCycles; i++){ // for the calculated length of time...
digitalWrite(targetPin,HIGH); // write the buzzer pin high to push out the diaphram
delayMicroseconds(delayValue); // wait for the calculated delay value
digitalWrite(targetPin,LOW); // write the buzzer pin low to pull back the diaphram
delayMicroseconds(delayValue); // wait againf or the calculated delay value
This for loop runs for the number of cycles calculated by the numCycles variable. Each cycle consists of two phases: a positive phase and a negative phase. During the positive phase, the digitalWrite function sets the targetPin to HIGH, which applies a positive voltage to the buzzer and produces a sound. The program then waits for the amount of time specified by the delayValue variable before moving on to the negative phase. During the negative phase, the digitalWrite function sets the targetPin to LOW, which applies a negative voltage to the buzzer and stops producing sound. The program then waits again for the amount of time specified by the delayValue variable before starting the next cycle. This process repeats for the total number of cycles specified by the numCycles variable, producing a sound with the desired frequency and length.
void loop() {.
buzz(4, 440, 250); //A
buzz(4, 440, 250); //A
buzz(4, 392, 250); //G
buzz(4, 440, 250); //A
buzz(4, 392, 250); //G
buzz(4, 349, 250);//F
//1st
buzz(4,523, 250);//C
buzz(4, 523, 250);//C
buzz(4, 440, 250); //A
buzz(4, 440, 250); //A
buzz(4, 440, 250); //A
buzz(4, 440, 250); //A
buzz(4, 440, 250); //A
buzz(4, 440, 250); //A
//2nd
buzz(4, 349, 250);//F
buzz(4, 349, 250);//F
buzz(4, 349, 250);//F
buzz(4, 349, 250);//F
buzz(4, 392, 250); //G
buzz(4, 392, 250); //G
buzz(4, 440, 250); //A
delay(5);
//3rd
buzz(4, 349, 250);//F
buzz(4, 349, 250);//F
buzz(4, 349, 250);//F
buzz(4, 349, 250);//F
buzz(4, 392, 250); //G
buzz(4, 440, 250); //A
buzz(4, 294, 250);//D
buzz(4, 262, 250);//C
//4th
buzz(4, 262, 250);//C
delay(5);
buzz(4, 294, 250);//D
buzz(4, 262, 250);//C
buzz(4, 294, 250);//D
buzz(4, 262, 250);//C
buzz(4, 220, 250); //A
buzz(4, 220, 250); //A
delay(250); // wait a bit between buzzes
}
The `void loop()` function is a built-in function in the Arduino system that runs continuously as long as the Arduino is powered. Within this loop, you have called your user-defined function `buzz`, which takes three arguments: the pin where the buzzer is connected, the frequency of the note being played, and the duration of the note. This code plays a melody using the `buzz` function and the solfa notations of a One Direction song (Best Song Ever by One Direction Chords, Melody, and Music Theory Analysis - Hooktheory) that I found on Google. It uses the notes and compares it to the frequency chart found in this website (Frequencies of Musical Notes, A4 = 440 Hz (mtu.edu)).
The buzz function likely uses a technique called pulse-width modulation (PWM) to generate a square wave at the desired frequency. This square wave rapidly switches the voltage applied to the buzzer between high and low, causing its diaphragm to vibrate and produce sound. The frequency of the square wave determines the pitch of the sound produced by the buzzer.
The delay statements pause the program for a specified amount of time before moving on to the next note. This allows for pauses or rests between notes and can also be used to control the tempo of the sequence.
Code for mp3 player with Arduino.
int dt = 100;
void setup() {
pinMode(4, OUTPUT); // set a pin for buzzer output
}
void loop() {.
buzz(4, 440, 250); //A
buzz(4, 440, 250); //A
buzz(4, 392, 250); //G
buzz(4, 440, 250); //A
buzz(4, 392, 250); //G
buzz(4, 349, 250);//F
//1st
buzz(4,523, 250);//C
buzz(4, 523, 250);//C
buzz(4, 440, 250); //A
buzz(4, 440, 250); //A
buzz(4, 440, 250); //A
buzz(4, 440, 250); //A
buzz(4, 440, 250); //A
buzz(4, 440, 250); //A
//2nd
buzz(4, 349, 250);//F
buzz(4, 349, 250);//F
buzz(4, 349, 250);//F
buzz(4, 349, 250);//F
buzz(4, 392, 250); //G
buzz(4, 392, 250); //G
buzz(4, 440, 250); //A
delay(5);
//3rd
buzz(4, 349, 250);//F
buzz(4, 349, 250);//F
buzz(4, 349, 250);//F
buzz(4, 349, 250);//F
buzz(4, 392, 250); //G
buzz(4, 440, 250); //A
buzz(4, 294, 250);//D
buzz(4, 262, 250);//C
//4th
buzz(4, 262, 250);//C
delay(5);
buzz(4, 294, 250);//D
buzz(4, 262, 250);//C
buzz(4, 294, 250);//D
buzz(4, 262, 250);//C
buzz(4, 220, 250); //A
buzz(4, 220, 250); //A
delay(250); // wait a bit between buzzes
}
void buzz(int targetPin, long frequency, long length) {
long delayValue = 250000/frequency/2; // calculate the delay value between transitions
//// 0.25 second's worth of microseconds, divided by the frequency, then split in half since
//// there are two phases to each cycle
long numCycles = frequency * length/ 250; // calculate the number of cycles for proper timing
//// multiply frequency, which is really cycles per second, by the number of seconds to
//// get the total number of cycles to produce
for (long i=0; i < numCycles; i++){ // for the calculated length of time...
digitalWrite(targetPin,HIGH); // write the buzzer pin high to push out the diaphram
delayMicroseconds(delayValue); // wait for the calculated delay value
digitalWrite(targetPin,LOW); // write the buzzer pin low to pull back the diaphram
delayMicroseconds(delayValue); // wait againf or the calculated delay value
}
}
if you want to watch a video on it check my video on TikTok:
https://mericlre.blogspot.com/2023/03/play-one-republic-song-using-passive.html
No comments: