DIY Raspberry Pi Electronic Piano: Fun STEM Project (2026)
DIY Mini Electronic Piano : Overview
The DIY Mini Electronic Piano is an engaging STEM project for students from Grades 5–12. It demonstrates how a computer can read button inputs and generate different musical tones through a buzzer. Each push button represents a musical note, allowing students to build a small playable piano using simple electronic components and Python programming.
The Electronic Piano project combines basic electronics, GPIO programming, input handling, sound generation, and creative experimentation. Students can begin with a few notes and later expand the project with additional keys, a display, recording features, or a simple melody mode.
Innovation
Transforms a Raspberry Pi into a compact electronic piano by assigning a different musical frequency to each push button. Pressing a key sends a GPIO input to the Raspberry Pi, which generates the corresponding tone through a buzzer.
Materials Required
- Raspberry Pi 4/5 or similar Raspberry Pi computer
- microSD card with Raspberry Pi OS
- Breadboard
- 8 push buttons
- Passive buzzer suitable for PWM
- Jumper wires
- USB power supply
- Keyboard, mouse and monitor for setup
- Python / Thonny IDE
Learning Objectives
- Understand digital input and output using GPIO.
- Learn how push buttons can act as electronic keys.
- Understand that different sound frequencies produce different musical notes.
- Practice Python variables, functions, loops and event checking.
- Learn basic breadboard wiring and common-ground connections.
- Develop problem-solving and creative STEM skills.
Working Principle
Each push button is connected to a Raspberry Pi GPIO input. When a button is pressed, the Raspberry Pi detects the input and runs the corresponding note function. The program then generates a selected frequency through the buzzer.
Push Button → GPIO Input → Python Program → Musical Frequency → Buzzer Sound
Suggested Piano Notes
|
Key |
Note | Frequency |
|
1 |
C4 | 262 Hz |
|
2 |
D4 | 294 Hz |
|
3 |
E4 |
330 Hz |
| 4 | F4 |
349 Hz |
| 5 | G4 |
392 Hz |
| 6 | A4 |
440 Hz |
| 7 | B4 |
494 Hz |
| 8 | C5 |
523 Hz |
Detailed Step-by-Step Procedure
Step 1: Prepare the Raspberry Pi
Install Raspberry Pi OS on the microSD card and complete the initial setup. Connect the Raspberry Pi to a monitor, keyboard and mouse, then open the Thonny Python IDE.
Step 2: Prepare the Breadboard
Place the eight push buttons on the breadboard with enough space between them. Use a common ground rail.
Connect one side of each button to a Raspberry Pi GPIO input and the other side to GND. The example program uses the Raspberry Pi’s internal pull-up resistors.
Step 4: Connect the Buzzer
Connect the buzzer signal to GPIO18 and its ground to Raspberry Pi GND. Use an appropriate driver circuit if the selected buzzer requires more current than a GPIO can safely provide.
Step 5: Use These GPIO Connections
Key 1 C4 = GPIO5; Key 2 D4 = GPIO6; Key 3 E4 = GPIO13; Key 4 F4 = GPIO19; Key 5 G4 = GPIO26; Key 6 A4 = GPIO16; Key 7 B4 = GPIO20; Key 8 C5 = GPIO21; Buzzer = GPIO18.
Step 6: Create the Python Program
Open Thonny and create mini_piano.py. Enter the complete program below.
Step 7: Run the Program
Save the file and run it from Thonny, or use python3 mini_piano.py in the terminal.
Step 8: Test Each Key
Press each button individually. Verify that the corresponding note is produced and check wiring if a key does not respond.
Step 9: Play a Melody
After all keys work, press a sequence of keys to play a familiar melody and experiment with rhythm.
Step 10: Improve the Piano
Add more keys, a display, melody playback, recording/replay, volume or tempo controls, or LEDs that show the selected key.
Complete Python Code
How It Works
When no key is pressed, the buzzer output is inactive. When a button is detected, the program looks up its frequency, changes the PWM frequency and activates the buzzer. When the button is released, the sound stops.
Expected Output
The completed project behaves like a small electronic piano. Pressing different buttons produces different musical notes through the buzzer. Students can play individual notes, create melodies and experiment with rhythm.
Applications
- STEM classroom activities
- Music and technology demonstrations
- Coding clubs
- Electronics workshops
- Science exhibitions
- Beginner embedded-systems activities
- Interactive learning projects
Advantages
- Simple and interactive
- Uses inexpensive components
- Introduces Python and GPIO programming
- Combines music with electronics and coding
- Easy to customize
- Suitable for beginner and intermediate STEM learners
Safety Precautions
- Use safe low-voltage circuits.
- Do not apply 5V directly to Raspberry Pi GPIO input pins.
- Do not connect high-current loads directly to GPIO pins.
- Use a suitable driver if the buzzer requires more current than GPIO can provide.
- Switch off power before changing wiring.
- Check all connections before powering the Raspberry Pi.
Conclusion
The DIY Mini Electronic Piano connects programming, electronics and music in a hands-on STEM activity. Students learn how physical buttons become digital inputs and how software converts those inputs into different sound frequencies. The project provides a practical foundation for GPIO, PWM, event handling, embedded systems and interactive device design.
FAQs
- What is the purpose of this Electronic Piano project?
It demonstrates how push buttons, GPIO programming and a buzzer can create a simple electronic piano.
- Which programming language is used?
Python is used to read the buttons and control the buzzer through Raspberry Pi GPIO.
- How many notes can the piano play?
The example uses eight keys, from C4 to C5. More keys can be added.
- Can beginners build this Electronic Piano project?
Yes. With guided wiring and programming, it is suitable for beginner STEM learners.
- Can it play complete songs?
Yes. A melody mode can store note sequences and timing information.
- Can LEDs be added?
Yes. LEDs can indicate which piano key is being played.

















