What does this project do?
We build a small piano with seven buttons that plays the notes from C to B (Do to Si). Each button plays one note; the buzzer plays that note for as long as you hold the button down. At start-up the piano also plays a short tune to say “I’m ready”. It’s a fun project that brings music and coding together.
- Age group: 9 and up
- Level: Beginner
- Time: About 40 minutes
- What you will learn: sound frequencies and musical notes, the
tone()function, reading several buttons with arrays
New to Arduino? Have a look at the What is Arduino and how does it work? guide first. All projects, sorted from easy to hard, are on the Getting Started with Arduino page.
Parts list
| Part | Qty |
|---|---|
| Arduino Uno (or compatible) | 1 |
| Push button | 7 |
| Passive buzzer | 1 |
| Breadboard (full size recommended) | 1 |
| Male-to-male jumper wires | ~16 |
| USB cable | 1 |
Wiring

| Button | Note | Frequency | Arduino pin |
|---|---|---|---|
| 1 | C4 (Do) | 262 Hz | D2 |
| 2 | D4 (Re) | 294 Hz | D3 |
| 3 | E4 (Mi) | 330 Hz | D4 |
| 4 | F4 (Fa) | 349 Hz | D5 |
| 5 | G4 (Sol) | 392 Hz | D6 |
| 6 | A4 (La) | 440 Hz | D7 |
| 7 | B4 (Si) | 494 Hz | D8 |
- One leg of each button goes to the pin in the table, and the diagonally opposite leg goes to the GND rail.
- The buzzer’s + (long) leg goes to D9 and its – leg to GND.
Code
// Arduino Mini Piano - ozandamci.com
const int buttons[7] = {2, 3, 4, 5, 6, 7, 8};
const int notes[7] = {262, 294, 330, 349, 392, 440, 494}; // C D E F G A B
const int buzzerPin = 9;
void setup() {
for (int i = 0; i < 7; i++) {
pinMode(buttons[i], INPUT_PULLUP);
}
pinMode(buzzerPin, OUTPUT);
startupTune();
}
void startupTune() {
int melody[] = {262, 330, 392, 523};
for (int i = 0; i < 4; i++) {
tone(buzzerPin, melody[i], 150);
delay(200);
}
}
void loop() {
int noteToPlay = 0;
// Find the first button that is pressed
for (int i = 0; i < 7; i++) {
if (digitalRead(buttons[i]) == LOW) {
noteToPlay = notes[i];
break;
}
}
if (noteToPlay > 0) {
tone(buzzerPin, noteToPlay);
} else {
noTone(buzzerPin);
}
}
No Arduino? Try this circuit in your browser with the Wokwi simulation → (free, nothing to install).
Step-by-step instructions
- Line up the buttons. Place the 7 buttons side by side across the gap in the middle of the breadboard, like piano keys.
- Connect the GND rail. Connect the Arduino’s GND pin to the – rail of the breadboard. Connect one leg of each button to this rail.
- Connect the buttons to the pins. Connect the diagonally opposite leg of each button to D2 through D8, in the order shown in the table.
- Place the buzzer. Connect its long leg to D9 and its short leg to the – rail.
- Upload the code. Choose your board and port, then click Upload. You should hear a short tune at start-up.
- Play. Press the buttons one after another to play C-D-E-F-G-A-B. Then try the easy song below.
Easy song – Twinkle Twinkle Little Star (first line):
C C G G A A G – F F E E D D C
Troubleshooting
- The buzzer only clicks and doesn’t play notes: You may be using an active buzzer; you need a passive buzzer to play notes.
- One button doesn’t work: Make sure you are using diagonally opposite legs of that button and that it is connected to the right pin.
Try it yourself
- Add an eighth button for high C (523 Hz) to make a full octave.
- Add a “record” button and make a piano that records the tune you play and plays it back.
For teachers: a two-page activity sheet for using this project in class: lesson plan, discussion questions, answer key and a student worksheet.





“Arduino Mini Piano with 7 Buttons and a Buzzer (Code)” için 2 yorum