Introduction: Arduino Piezo Buzzer Piano
Here we will make an Arduino piano that uses a piezo buzzer as a speaker. This project is easily scalable and can work with more or less notes, depending on you! We will build it with only four buttons/keys for simplicity. This is fun and easy project that requires little skill, but can be made into something much more complex or larger.
Supplies
- 1x Arduino Uno (other Arduino boards should be fine but were not tested)
- 1x Half size or larger bread board
- 1x Active piezo buzzer
- 4x momentary push button
- 11x bread board jumper wires (6 black for negative and 5 colorful for buttons & buzzer
Step 1: Wiring
To get started, we will place 4 buttons near each other and the piezo buzzer on the other side of the bread board, as shown on the first picture. Next, we will connect the negative wires. First, we will connect the negative rail on the bread board to a negative pin labeled "GND" on the Arduino. Then, we connect one leg of each button to the negative rail. The piezo buzzer has one leg shorter, which is the negative one. We will also connect it to the negative rail.
Now it's time to connect the rest of the wires. We will connect the other legs of the buttons to pins 2-5, as shown in the picture. Lastly, we will connect the positive leg of the piezo buzzer (the longer one) to pin 10. The pin numbers can be easily changed in the code later on. Please review the images for clearer wiring.
Step 2: Programming
The code is pretty simple and self explanatory. At the top, we assign pin numbers to the variables. Then, we declare each one as an input or output. Lastly, we state what to do when a certain button is pressed. The buttons labeled but1-but4 each correspond to a frequency to be played when they are pushed. But1 is the lowest frequency of 100hz, while but 4 has the highest frequency of 400hz. We use the tone() function to play the tones in Hertz. It is structured like this:
tone(buzzerPin, [frequency in hertz], [duration]);
If you want to add more buttons, then you have to create a new variable and a new 'if' statement for when it is pressed. It is very easy to duplicate.
Keep in mind however, that the Arduino can only play one tone at a time. If you press multiple buttons at once, the sound will not be correct because Arduino is rapidly switching between different frequencies.
Attachments
Step 3: Viewing the Tones on an Oscilloscope.
When we connect an oscilloscope to the negative rail and buzzer pin, we get a few different square waves. The higher the frequency, the closer together the spikes are. The first picture shows the highest frequency in our program (400hz), and the last picture the lowest frequency (100hz). The square waves get further and further apart when the frequency lowers. Examine the pictures to see the effect.
From left to right:
400hz, 300hz, 200hz, and 100hz
Step 4: Actuall Piano Keys?
If you have access to a 3D printer, you may be interested in making some keys for your Arduino piezo buzzer keyboard. These give the tiny push buttons a better feel. You can find them here on prusaprinters.org.
Step 5: Conclusion
I hope you enjoyed creating an Arduino piezo buzzer keyboard, and I also encourage you to tweak the code. If you liked this project, please post your make down below or leave a comment. Thank you! :D