Homemade Electrophone
- VJ

- Jul 18, 2021
- 4 min read

Today I received a project in school where I had to create a music instrument (for my band class). That's when I decided to use a buzzer system that changed electric frequencies in order to make music. On the right is a prototype. The seven buttons each played a note and the final bottom button played a premade melody. For this project I directly plugged in a cable to the Arduino but it is entirely possible to use a battery.
Materials:
Hardware:
For the prototype, we need to connect each button to a digital in pin, and the buzzer to a digital out pin. Those will be defined in the software. Since most of this project takes place in the software, the hardware is fairly simple. The precise pinouts are completely up to the maker, as long as the code matches up.
[fritzing image]
All of the wirings can be directly transferred into a handmade PCB. Just be sure to carefully map out how the solder should be placed before soldering. Additionally be sure not to fry any of the parts. One issue I ran into was multiple false positives resulting in random notes. This was caused by a malfunction in the buttons. When soldering, be sure to avoid heating up the buttons too much or they may also malfunction.
Software:
For the software, I assigned keys to each button, assigned the inputs, and borrowed a method I found online that assigns frequencies to notes in a vocal scale.
The first part of the program is just assigning the button keys, and the melody. The melody can be any assortment of notes, and there is an array to house the beats as well. Together, the notes and beats make up a song. The song can be changed by just changing the contents of the arrays. The tempo is also defined, and that will change how fast the song is played.
int buzzerPin = 12;
int akey;
int bkey;
int ckey;
int dkey;
int ekey;
int fkey;
int gkey;
int music;
char notes[] = "ccdbcdeefedcdcbc"; // musical pattern
const int songLength = 16; // how many notes are in the array above
int beats[] = {2,2,2,4,1,2,2,2,2,4 ,1,2,2,2,2,5}; // length of each note
int tempo = 200;
int duration;
Next we define all the buttons as input and the buzzer as output in the setup method:
void setup()
{
pinMode(buzzerPin, OUTPUT);
pinMode(2, INPUT);
pinMode(3, INPUT);
pinMode(4, INPUT);
pinMode(5, INPUT);
pinMode(6, INPUT);
pinMode(7, INPUT);
pinMode(8, INPUT);
pinMode(13,INPUT);
pinMode(9,OUTPUT);
Serial.begin(9600);
}
After that is complete, I used some borrowed code and added on to it. First is a borrowed loop structure that plays a song based on out parameters. That loop is triggered when the song button is clicked. I also added a while loop so once the song is played it cannot be replayed, and no other notes can be played. This was a bug test and can be removed. After the song structures, there are multiple if statements that trigger different notes.
void loop()
{
akey = digitalRead(2);
bkey = digitalRead(3);
ckey = digitalRead(4);
dkey = digitalRead(5);
ekey = digitalRead(6);
fkey = digitalRead(7);
gkey = digitalRead(8);
music = digitalRead(13);
int i = 500;
if(music ==HIGH){
for (i = 0; i < songLength; i++) // step through the song arrays
{
duration = beats[i] * tempo; // length of note/rest in ms
if (notes[i] == ' ') // is this a rest?
{
delay(duration); // then pause for a moment
}
else // otherwise, play the note
{
digitalWrite(9,HIGH);
tone(buzzerPin, frequency(notes[i]), duration);
delay((2*duration)/3);
digitalWrite(9,LOW);
delay(duration/3);// wait for tone to finish
}
delay(tempo/10); // brief pause between notes
}
while(true){}
}
if(akey == HIGH){
tone(buzzerPin, frequency('a'), i);
delay (i);
}
else if(bkey == HIGH){
tone(buzzerPin, frequency('b'), i);
delay (i);
}
else if(ckey == HIGH){
tone(buzzerPin, frequency('c'), i);
delay (i);
}
else if(dkey == HIGH){
tone(buzzerPin, frequency('d'), i);
delay (i);
}
else if(ekey == HIGH){
tone(buzzerPin, frequency('e'), i);
delay (i);
}
else if(fkey == HIGH){
tone(buzzerPin, frequency('f'), i);
delay (i);
}
else if(gkey == HIGH){
tone(buzzerPin, frequency('g'), i);
delay (i);
}
delay (1000);
}
Finally there is the frequency method that is also borrowed. It just assigns buzzer frequencies to different pitches.
int frequency(char note)
{
// This function takes a note character (a-g), and returns the
// corresponding frequency in Hz for the tone() function.
int i;
const int numNotes = 8; // number of notes we're storing
// The following arrays hold the note characters and their
// corresponding frequencies. The last "C" note is uppercase
// to separate it from the first lowercase "c". If you want to
// add more notes, you'll need to use unique characters.
// For the "char" (character) type, we put single characters
// in single quotes.
char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
int frequencies[] = {262, 294, 330, 349, 392, 440, 494, 523};
// Now we'll search through the letters in the array, and if
// we find it, we'll return the frequency for that note.
for (i = 0; i < numNotes; i++) // Step through the notes
{
if (names[i] == note) // Is this the one?
{
return(frequencies[i]); // Yes! Return the frequency
}
}
return(0); // We looked through everything and didn't find it,
// but we still need to return a value, so return 0.
}
Throughout the loop method, there are some extraneous delays that can be removed or reduced in time. They were just part of bug fixes I conducted due to faulty hardware. One big issue I was facing was misfiring notes. That was because the buttons I were using weren't the highest quality, and they were letting false signals through. As long as you have solid buttons that problem shouldn't happen, and the delays between each loop can be removed or reduced.
Here is the full code:
Topside of PCB:

Bottom of PCB:

This project was just to fill the requirements for my project, but there are many areas that can be improved. For example, more buttons can be added and the frequencies can be tuned a bit. There are also some code barriers that I didnt have the time to navigate. I never knew that buzzers could change frequencies, so this was also a learning experience for me. Hopefully this design can be improved in the future. Good luck!



Comments