Illustration: beside a ukulele, five buttons in a row like piano keys and a buzzer; a child presses one.Games
Illustration (made with AI): roughly what the finished project looks like.

A Little Button Piano

Five buttons, five notes: C, D, E, F and G. Press a button and the buzzer plays its note, enough for simple tunes.

Difficulty
Beginner
Time
40 min
Age
8+
Parts
5, 5 on the site

Adapted from “Simple keyboard using the tone() function” by Arduino. Licence: CC BY-SA 4.0. This translation is shared under the same licence. The parts list and the tips are RoboHub's.

Variations

Choose a variation and the parts list updates.

Instructions

  1. Buttons

    Place five buttons in a row on the breadboard. Connect one leg of each button to the ground rail and the other to pins 2, 3, 4, 5 and 6. On a four-leg button use two diagonally opposite legs: the two legs on one side are always connected inside.

    D2 is the board's digital pin 2, GND is ground. Red wires are power, black ground, the other colours signals (on a real servo the ground wire is often brown). In the steps, what a step adds is in colour and what is already connected in grey.

    • Button: C 1 → Arduino D2
    • Button: C 2 → Arduino GND
    • Button: D 1 → Arduino D3
    • Button: D 2 → Arduino GND
    • Button: E 1 → Arduino D4
    • Button: E 2 → Arduino GND
    • Button: F 1 → Arduino D5
    • Button: F 2 → Arduino GND
    • Button: G 1 → Arduino D6
    • Button: G 2 → Arduino GND
  2. Buzzer

    Connect the passive buzzer's “+” to pin 8 and “−” to ground. An active buzzer (one with its own oscillator) plays only one tone, so you need a passive buzzer or a piezo element here.

    D2 is the board's digital pin 2, GND is ground. Red wires are power, black ground, the other colours signals (on a real servo the ground wire is often brown). In the steps, what a step adds is in colour and what is already connected in grey.

    • Passive buzzer + → Arduino D8
    • Passive buzzer − → Arduino GND
  3. Upload the code

    tone() makes the buzzer sound at a given frequency: 262 Hz is C, 392 Hz is G. noTone() stops the sound when you let go of the button.

    Arduino C++
    const int BUZZER = 8;
    const int KEYS[] = {2, 3, 4, 5, 6};               // one button per note
    const int NOTES[] = {262, 294, 330, 349, 392};    // C D E F G (Hz)
    const int COUNT = 5;
    int playing = -1;                                  // the note that sounds now, -1 = none
    
    void setup() {
      for (int i = 0; i < COUNT; i++) pinMode(KEYS[i], INPUT_PULLUP);   // pressed = LOW
    }
    
    void loop() {
      int pressed = -1;
      for (int i = 0; i < COUNT; i++) {
        if (digitalRead(KEYS[i]) == LOW) { pressed = i; break; }
      }
      if (pressed != playing) {            // change the sound only when the key changes
        if (pressed < 0) noTone(BUZZER);
        else tone(BUZZER, NOTES[pressed]);
        playing = pressed;
      }
      delay(5);
    }
  4. Play

    Try E, D, C, D, E, E, E. For other notes change the frequencies in the NOTES array: F sharp is 370 Hz, A is 440 Hz.

    Illustration: five buttons in a row like piano keys, and a buzzer.

    Illustration, made with AI

  • Games
    Beginner45 min10+

    A Two-Player Reaction Game

    An LED lights at a random moment. Whoever presses their button first wins. Built from Student Kit parts in 45 minutes.

    6/6 parts on the site
  • Lights and displays
    Beginner45 min10+

    Pedestrian Crossing with a Button

    Cars have green until a pedestrian presses the button. Then the cars get red, the pedestrians get green and a buzzer ticks, just like at a real crossing.

    6/6 parts on the site
  • Home and IoT
    Beginner30 min10+

    A Motion Alarm

    A PIR sensor notices a warm body moving. As soon as someone walks into the room, a siren sounds and a red light flashes.

    6/6 parts on the site