Arduino Mini Piano with 7 Buttons and a Buzzer (Code)

Arduino ile Mini Piyano Yapımı – Butonlu Org projesi kapak görseli

🇹🇷 Bu yazıyı Türkçe okuyun

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

PartQty
Arduino Uno (or compatible)1
Push button7
Passive buzzer1
Breadboard (full size recommended)1
Male-to-male jumper wires~16
USB cable1

Wiring

Arduino mini piano with buttons and buzzer wiring diagram
ButtonNoteFrequencyArduino pin
1C4 (Do)262 HzD2
2D4 (Re)294 HzD3
3E4 (Mi)330 HzD4
4F4 (Fa)349 HzD5
5G4 (Sol)392 HzD6
6A4 (La)440 HzD7
7B4 (Si)494 HzD8
  • 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

  1. Line up the buttons. Place the 7 buttons side by side across the gap in the middle of the breadboard, like piano keys.
  2. 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.
  3. 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.
  4. Place the buzzer. Connect its long leg to D9 and its short leg to the – rail.
  5. Upload the code. Choose your board and port, then click Upload. You should hear a short tune at start-up.
  6. 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.

Video

The narration is in Turkish. English subtitles are available: click CC on the video.

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

Bir Cevap Yazın