Project 5Music and games25 min
๐ต Magic Piano
Turn your micro:bit into a piano and play tunes with buttons and gestures.
Workshop projects (5 of 6)
Let's turn your micro:bit into a piano you can play with the buttons! You'll be able to make tunes, play songs you know and even write your own music.
What are we going to do?
Our magic piano will have:
- ๐น Buttons as keys: A and B play different notes
- ๐ต A simple song: "Happy Birthday"
- ๐ถ Fun effects: Sounds when you shake the micro:bit
How does sound work on the micro:bit?
The micro:bit can make music! Each note has a different sound:
- C = low sound
- G = high sound
It's like a piano: some keys sound low and others sound high!
Step 1: Create the project
- Create a new project called "Magic Piano"
- This time we don't need variables! Let's go straight to the music.
Step 2: Getting started
In the "on start" block:
- Show the music icon
- Play tone Middle C for 1 beat (a musical hello)

Step 3: Program the basic keys
Let's make the buttons play notes!
- Go to "Input" (pink) and drag "on button A pressed"
- Go to "Music" (red) and drag "play tone Middle C for 1 beat"
- Go to "Basic" and drag "show icon" with a music note
For button B:
- Same as button A, but change "C" to "G"
- Use a different icon (like a heart)

Step 4: Add fun effects
Let's make the micro:bit make sounds when you move it!
- Go to "Input" and drag "on shake"
- Add "play sound giggle" (or whichever sound you like best)
- Add "show icon" with a happy face

Step 5: Play a simple song
Let's program "Happy Birthday"!
- Go to "Input" and drag "on button A+B pressed"
- Add "show string" with "BIRTHDAY"
- Now add these tones one after another:
- Play tone C for 1 beat
- Play tone C for 1/2 beat
- Play tone D for 1 beat
- Play tone C for 1 beat
- Play tone F for 1 beat
- Play tone E for 2 beats
- Add "show icon" with a heart at the end

Step 6: Try your piano!
- Press button A: Does it play C?
- Press button B: Does it play G?
- Shake the micro:bit: Does it make a funny sound?
- Press A+B: Does it play "Happy Birthday"?
What have you learned?
- โ How to make music with the micro:bit
- โ Using buttons to play notes
- โ Programming a whole song
- โ Adding fun sound effects
Extra improvements
Add more sounds
Experiment with different sounds!
- Go to "Music" and find "play sound"
- Try sounds like "twinkle", "spring", "mysterious"
- Add them to different buttons
More notes
Want more notes?
- Use the tilt gestures to play "E" and "F"
- Add "on tilt left" with the tone E
- Add "on tilt right" with the tone F
Common problems
๐ค You can't hear anything?
- Turn up the simulator's volume
- Check that the blocks are inside the events
๐ค The song sounds strange?
- Make sure the notes are in the right order
- Check that each note has its length
Ready for the grand finale?
Incredible! You've made your own musical instrument. Now let's do the final and biggest project: your very own game! Go to Project 6: Rock, Paper, Scissors.