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
CartPrices 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
Install pygame for sound
About 10 minutes
Set up Python library for audio playback.
- Open a Terminal on Raspberry Pi.
- Install pygame: sudo apt-get install python3-pygame
- Wait for installation to complete.
- 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.
- Create a directory: mkdir ~/piano
- Download piano note sound files (.wav format) for notes C4-C5.
- Or use pygame's tone generation capabilities.
- 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.
- 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.
- Create a Python file: nano ~/piano/piano.py
- Copy the complete piano code below.
- The code uses gpiozero for button detection and pygame for sound.
- Save and exit (Ctrl+X, Y, Enter).
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.
- Run the script: python3 ~/piano/piano.py
- Press each button to hear its note.
- Try playing simple melodies like 'Mary Had a Little Lamb' or 'Twinkle Twinkle'.
- 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
- Build-A-BandCreate another music project with Scratch.
- More Raspberry Pi projectsExplore intermediate Raspberry Pi builds.
Stuck? Contact Vilros Support with your board model and what you see on screen.