Turn 7 push buttons into a playable piano. Map frequencies to musical notes on your breadboard.
Sound is just vibration at a specific frequency. The Arduino's tone() function can drive a piezo buzzer at any frequency, making it a perfect musical instrument controller.
What you'll learn: tone() and noTone() functions, frequency-to-note mapping, button arrays, and for-loop scanning.
Estimated time: 40–50 minutes. Difficulty: ⭐⭐ Easy.
| Component | Specification | Qty | Notes |
|---|---|---|---|
| Arduino Uno R3 | 5V | 1 | |
| Piezo Buzzer | Active/Passive 5V | 1 | Passive buzzer recommended |
| Push Buttons | Momentary SPST | 7 | One per note (C-D-E-F-G-A-B) |
| Resistors | 10kΩ pull-down | 7 | One per button |
| Breadboard + Wires | Full-size | 1 |
// Buzzer Piano — Volt X
const int buzzer = 9;
const int buttons[] = {2, 3, 4, 5, 6, 7, 8};
const int notes[] = {262, 294, 330, 349, 392, 440, 494}; // C D E F G A B
void setup() {
for (int i = 0; i < 7; i++) {
pinMode(buttons[i], INPUT);
}
pinMode(buzzer, OUTPUT);
}
void loop() {
bool anyPressed = false;
for (int i = 0; i < 7; i++) {
if (digitalRead(buttons[i]) == HIGH) {
tone(buzzer, notes[i]);
anyPressed = true;
break;
}
}
if (!anyPressed) noTone(buzzer);
}Sign in to leave a review
No reviews yet. Be the first to share your experience!