Skip to content

Raspberry Pi Piano

Build a working piano keyboard using Raspberry Pi GPIO pins, push buttons, and Python to play musical notes.

  • Raspberry Pi
  • Intermediate
  • 90 minutes
  • 5 steps

Shop this project

Cart

Prices from vilros.com. Tax and shipping at checkout.

Before you start, you'll need

  • Raspberry Pi (any model with GPIO)
  • 12 push buttons (one per note)
  • 12 resistors (10kΩ)
  • Breadboard
  • Jumper wires (male-to-female)
  • Speaker or headphones

Wire 12 buttons for piano keys

About 30 minutes

Connect 12 buttons to GPIO pins.

  1. Place 12 buttons on a breadboard in a row (representing piano keys).
  2. Connect each button with a pull-down resistor (button → 3.3V, other side → GPIO pin + 10kΩ resistor to GND).
  3. Wire buttons to GPIO pins 2-13 (one button per pin).
  4. Arrange buttons like a piano: 8 white keys (C, D, E, F, G, A, B, C) and 4 black keys (C#, D#, F#, G#) between appropriate white keys.

You should see

All 12 buttons are wired to GPIO pins 2-13.

Install pygame for sound

About 10 minutes

Set up Python library for audio playback.

  1. Open a Terminal on Raspberry Pi.
  2. Install pygame: sudo apt-get install python3-pygame
  3. Wait for installation to complete.
  4. Connect a speaker or headphones to the Pi's audio jack.

You should see

Pygame is installed and audio output is connected.

Prepare piano sound files

About 15 minutes

Get or create .wav files for each note.

  1. Create a directory: mkdir ~/piano
  2. Download piano note sound files (.wav format) for notes C4-C5.
  3. Or use pygame's tone generation capabilities.
  4. Name files: C4.wav, C#4.wav, D4.wav, D#4.wav, E4.wav, F4.wav, F#4.wav, G4.wav, G#4.wav, A4.wav, A#4.wav, B4.wav, C5.wav.
  5. Place all sound files in ~/piano directory.

You should see

You have 12 note sound files ready.

Program the piano

About 25 minutes

Create Python code to map buttons to notes.

  1. Create a Python file: nano ~/piano/piano.py
  2. Copy the complete piano code below.
  3. The code uses gpiozero for button detection and pygame for sound.
  4. Save and exit (Ctrl+X, Y, Enter).
python
import pygame
from gpiozero import Button
from signal import pause

pygame.mixer.init()

notes = {
    2: "C4.wav",
    3: "C#4.wav",
    4: "D4.wav",
    5: "D#4.wav",
    6: "E4.wav",
    7: "F4.wav",
    8: "F#4.wav",
    9: "G4.wav",
    10: "G#4.wav",
    11: "A4.wav",
    12: "A#4.wav",
    13: "B4.wav"
}

sounds = {}
for pin, note in notes.items():
    try:
        sounds[pin] = pygame.mixer.Sound(f"/home/pi/piano/{note}")
    except:
        print(f"Could not load {note}")

buttons = {}
for pin in notes.keys():
    buttons[pin] = Button(pin)

def play_note(pin):
    if pin in sounds:
        sounds[pin].play()
        print(f"Playing {notes[pin]}")

for pin in buttons.keys():
    buttons[pin].when_pressed = lambda p=pin: play_note(p)

print("Piano ready! Press buttons to play notes.")
pause()

Adjust file paths if your sound files are in a different location.

You should see

The Python script is saved.

Play your piano

About 10 minutes

Test all keys and play music.

  1. Run the script: python3 ~/piano/piano.py
  2. Press each button to hear its note.
  3. Try playing simple melodies like 'Mary Had a Little Lamb' or 'Twinkle Twinkle'.
  4. Adjust volume using the Pi's audio settings if needed.

You should see

All 12 keys play their assigned notes.

It didn't work
  • No sound plays.

    Check audio output: run 'sudo raspi-config' → System Options → Audio → Force 3.5mm jack. Verify sound files exist and are in .wav format.

  • Notes trigger multiple times or not at all.

    Check button wiring and pull-down resistors. Verify buttons are properly connected to GPIO pins. Add debouncing in code if needed.

What to do next

Stuck? Contact Vilros Support with your board model and what you see on screen.