BBC micro:bit
Music Maker
Play a tune you write note by note, then bend the pitch by tilting the micro:bit, like a real instrument.
Today the micro:bit makes music. You will play a famous tune, write your own note by note, and turn the micro:bit into an instrument that you play by tilting it from side to side.
You will learn
- Play the built in tunes
- Write a tune as a list of notes
- Change a sound's pitch with a sensor
- Add sound effects to events
You will need
- A computer with Chrome or Edge
- A micro:bit V2, which has a speaker, or a V1 with headphones
- The simulator plays sound through the computer's speakers
Words to know
- pitch
- How high or low a sound is, measured in hertz: vibrations a second.
- tempo
- How fast music plays, in beats a minute.
- octave
- The number after a note's letter: C5 is one octave higher than C4.
- sensor
- Something that measures the world around it, such as the accelerometer.
Step by step
Step 1: Play a tune
Start a new project called Music Maker.
import musicbrings in the micro:bit's music, and it knows plenty of tunes already. Play one, with a music picture while it plays. Turn your speakers up and press play in the simulator.1from microbit import *2import music3 4display.show(Image.MUSIC_QUAVERS)5music.play(music.ENTERTAINER)ReferenceIdeasAPIProjectMusic Maker1from microbit import *2import music3 4display.show(Image.MUSIC_QUAVERS)5music.play(music.ENTERTAINER)Send to micro:bitSaveOpen…SimulatorA music picture while the tune plays. Tip A micro:bit V2 plays sound from its own speaker. A V1 needs headphones or a speaker clipped to pin 0 and GND.Step 2: Write your own tune
A tune is a list of notes. Each note is a letter, the octave and how long it lasts:
"G4:8"is a G that lasts twice as long as"G4:4". This is Twinkle Twinkle Little Star. It plays each time you press A. Replace the two lines that played the tune at the start.New: lines 4 to 10.
1from microbit import *2import music3 4twinkle = ["C4:4", "C4:4", "G4:4", "G4:4", "A4:4", "A4:4", "G4:8",5 "F4:4", "F4:4", "E4:4", "E4:4", "D4:4", "D4:4", "C4:8"]6 7while True:8 if button_a.was_pressed():9 music.play(twinkle)10 sleep(10)ReferenceIdeasAPIProjectMusic Maker1from microbit import *2import music3 4twinkle = ["C4:4", "C4:4", "G4:4", "G4:4", "A4:4", "A4:4", "G4:8",5 "F4:4", "F4:4", "E4:4", "E4:4", "D4:4", "D4:4", "C4:8"]6 7while True:8 if button_a.was_pressed():9 music.play(twinkle)10 sleep(10)Send to micro:bitSaveOpen…SimulatorPress A to hear Twinkle Twinkle. Step 3: Show the music
Show the music picture while the tune plays, then clear the lights.
New: lines 9 and 11.
1from microbit import *2import music3 4twinkle = ["C4:4", "C4:4", "G4:4", "G4:4", "A4:4", "A4:4", "G4:8",5 "F4:4", "F4:4", "E4:4", "E4:4", "D4:4", "D4:4", "C4:8"]6 7while True:8 if button_a.was_pressed():9 display.show(Image.MUSIC_QUAVERS)10 music.play(twinkle)11 display.clear()12 sleep(10)ReferenceIdeasAPIProjectMusic Maker1from microbit import *2import music3 4twinkle = ["C4:4", "C4:4", "G4:4", "G4:4", "A4:4", "A4:4", "G4:8",5 "F4:4", "F4:4", "E4:4", "E4:4", "D4:4", "D4:4", "C4:8"]6 7while True:8 if button_a.was_pressed():9 display.show(Image.MUSIC_QUAVERS)10 music.play(twinkle)11 display.clear()12 sleep(10)Send to micro:bitSaveOpen…SimulatorThe music picture shows while Twinkle Twinkle plays. Step 4: Tilt to play
Now make an instrument. While B is held,
accelerometer.get_x()measures the tilt from side to side: 0 when the micro:bit is level, up to about 1024 when it is tipped right, and below zero when it is tipped left, down to about 1024 below zero. Adding 1100 turns that into a pitch from very low to very high, andmusic.pitchplays it for 50 milliseconds, again and again as the loop goes round. In the simulator, hold B and slide the x control under Accelerometer.New: lines 12 to 16.
1from microbit import *2import music3 4twinkle = ["C4:4", "C4:4", "G4:4", "G4:4", "A4:4", "A4:4", "G4:8",5 "F4:4", "F4:4", "E4:4", "E4:4", "D4:4", "D4:4", "C4:8"]6 7while True:8 if button_a.was_pressed():9 display.show(Image.MUSIC_QUAVERS)10 music.play(twinkle)11 display.clear()12 elif button_b.is_pressed():13 display.show(Image.MUSIC_CROTCHET)14 music.pitch(accelerometer.get_x() + 1100, 50)15 else:16 display.clear()17 sleep(10)ReferenceIdeasAPIProjectMusic Maker5 "F4:4", "F4:4", "E4:4", "E4:4", "D4:4", "D4:4", "C4:8"]6 7while True:8 if button_a.was_pressed():9 display.show(Image.MUSIC_QUAVERS)10 music.play(twinkle)11 display.clear()12 elif button_b.is_pressed():13 display.show(Image.MUSIC_CROTCHET)14 music.pitch(accelerometer.get_x() + 1100, 50)15 else:16 display.clear()17 sleep(10)Send to micro:bitSaveOpen…SimulatorAccelerometerxHold B and tilt: the more you tip it right, the higher the note. Step 5: Shake for a surprise
Add a sound effect: a shake plays the built in power up sound.
New: lines 15 to 17.
1from microbit import *2import music3 4twinkle = ["C4:4", "C4:4", "G4:4", "G4:4", "A4:4", "A4:4", "G4:8",5 "F4:4", "F4:4", "E4:4", "E4:4", "D4:4", "D4:4", "C4:8"]6 7while True:8 if button_a.was_pressed():9 display.show(Image.MUSIC_QUAVERS)10 music.play(twinkle)11 display.clear()12 elif button_b.is_pressed():13 display.show(Image.MUSIC_CROTCHET)14 music.pitch(accelerometer.get_x() + 1100, 50)15 elif accelerometer.was_gesture("shake"):16 display.show(Image.SURPRISED)17 music.play(music.POWER_UP)18 else:19 display.clear()20 sleep(10)ReferenceIdeasAPIProjectMusic Maker6 7while True:8 if button_a.was_pressed():9 display.show(Image.MUSIC_QUAVERS)10 music.play(twinkle)11 display.clear()12 elif button_b.is_pressed():13 display.show(Image.MUSIC_CROTCHET)14 music.pitch(accelerometer.get_x() + 1100, 50)15 elif accelerometer.was_gesture("shake"):16 display.show(Image.SURPRISED)17 music.play(music.POWER_UP)18 else:Send to micro:bitSaveOpen…SimulatorAccelerometershakeSend gestureShake it for a power up sound. Step 6: Play the real thing
Send it to your micro:bit. Practise Twinkle Twinkle on the tilt instrument, then play a duet with a friend: one presses A for the tune, the other tilts along.
ReferenceIdeasAPIProjectMusic Maker1from microbit import *2import music3 4twinkle = ["C4:4", "C4:4", "G4:4", "G4:4", "A4:4", "A4:4", "G4:8",5 "F4:4", "F4:4", "E4:4", "E4:4", "D4:4", "D4:4", "C4:8"]6 7while True:8 if button_a.was_pressed():9 display.show(Image.MUSIC_QUAVERS)10 music.play(twinkle)11 display.clear()12 elif button_b.is_pressed():13 display.show(Image.MUSIC_CROTCHET)Send to micro:bitSaveOpen…SimulatorSend it, then play your micro:bit like an instrument.
Test it
Tick each one when it works. If one does not, look back at the step it comes from.
Stretch challenges
Finished early? Try these. Each one has a hint if you get stuck.
Your own song
Write a different tune, such as Happy Birthday, and play it on button A instead.
Show a hint
Notes are A to G with an octave number, such as
"C4:4"."R:4"is a rest, a silent note. Hum the tune and work out each note.Speed it up
Make Twinkle Twinkle play twice as fast.
Show a hint
music.set_tempo(bpm=240)doubles the speed, from the normal 120 beats a minute.Siren
Make a siren that slides up and down on its own while the logo is touched.
Show a hint
A
forloop can count up through pitches:for freq in range(880, 1760, 16):thenmusic.pitch(freq, 6). Then count back down.
The finished code
Show the whole program
1from microbit import *2import music3 4twinkle = ["C4:4", "C4:4", "G4:4", "G4:4", "A4:4", "A4:4", "G4:8",5 "F4:4", "F4:4", "E4:4", "E4:4", "D4:4", "D4:4", "C4:8"]6 7while True:8 if button_a.was_pressed():9 display.show(Image.MUSIC_QUAVERS)10 music.play(twinkle)11 display.clear()12 elif button_b.is_pressed():13 display.show(Image.MUSIC_CROTCHET)14 music.pitch(accelerometer.get_x() + 1100, 50)15 elif accelerometer.was_gesture("shake"):16 display.show(Image.SURPRISED)17 music.play(music.POWER_UP)18 else:19 display.clear()20 sleep(10)Notes for the teacher
Timing
- 10 minutes: step 1. Warn students about volume first.
- 20 minutes: steps 2 and 3. Letting them choose their own tune is a good stretch.
- 20 minutes: steps 4 to 6.
Practical points
- A room of micro:bit speakers is loud. Agree a signal for silence, and try one table at a time.
- A V1 micro:bit has no speaker: headphones with crocodile clips on pin 0 and GND work well.
- The simulator plays through the computer's speakers or headphones.
Mistakes to look out for
- A note written as
"C4 4"or"C4;4"instead of"C4:4". music.playwaits until the tune ends, so the buttons do nothing while it plays. That is expected, and a good talking point.- A missing comma in the list of notes joins two notes together.