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.

Day 4 45 to 60 minutes 6 steps Key Stage 3
Start step 1
Music Maker, playing a tune.

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

  1. Step 1: Play a tune

    Start a new project called Music Maker. import music brings 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.

    main.pyMicroPython micro:bit editor
    from microbit import *import music display.show(Image.MUSIC_QUAVERS)music.play(music.ENTERTAINER)
    A 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.
  2. 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.

    main.pyMicroPython micro:bit editor
    from microbit import *import music twinkle = ["C4:4", "C4:4", "G4:4", "G4:4", "A4:4", "A4:4", "G4:8",           "F4:4", "F4:4", "E4:4", "E4:4", "D4:4", "D4:4", "C4:8"] while True:    if button_a.was_pressed():        music.play(twinkle)    sleep(10)
    Press A to hear Twinkle Twinkle.
  3. Step 3: Show the music

    Show the music picture while the tune plays, then clear the lights.

    New: lines 9 and 11.

    main.pyMicroPython micro:bit editor
    from microbit import *import music twinkle = ["C4:4", "C4:4", "G4:4", "G4:4", "A4:4", "A4:4", "G4:8",           "F4:4", "F4:4", "E4:4", "E4:4", "D4:4", "D4:4", "C4:8"] while True:    if button_a.was_pressed():        display.show(Image.MUSIC_QUAVERS)        music.play(twinkle)        display.clear()    sleep(10)
    The music picture shows while Twinkle Twinkle plays.
  4. 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, and music.pitch plays 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.

    main.pyMicroPython micro:bit editor
    from microbit import *import music twinkle = ["C4:4", "C4:4", "G4:4", "G4:4", "A4:4", "A4:4", "G4:8",           "F4:4", "F4:4", "E4:4", "E4:4", "D4:4", "D4:4", "C4:8"] while True:    if button_a.was_pressed():        display.show(Image.MUSIC_QUAVERS)        music.play(twinkle)        display.clear()    elif button_b.is_pressed():        display.show(Image.MUSIC_CROTCHET)        music.pitch(accelerometer.get_x() + 1100, 50)    else:        display.clear()    sleep(10)
    Hold B and tilt: the more you tip it right, the higher the note.
  5. Step 5: Shake for a surprise

    Add a sound effect: a shake plays the built in power up sound.

    New: lines 15 to 17.

    main.pyMicroPython micro:bit editor
    from microbit import *import music twinkle = ["C4:4", "C4:4", "G4:4", "G4:4", "A4:4", "A4:4", "G4:8",           "F4:4", "F4:4", "E4:4", "E4:4", "D4:4", "D4:4", "C4:8"] while True:    if button_a.was_pressed():        display.show(Image.MUSIC_QUAVERS)        music.play(twinkle)        display.clear()    elif button_b.is_pressed():        display.show(Image.MUSIC_CROTCHET)        music.pitch(accelerometer.get_x() + 1100, 50)    elif accelerometer.was_gesture("shake"):        display.show(Image.SURPRISED)        music.play(music.POWER_UP)    else:        display.clear()    sleep(10)
    Shake it for a power up sound.
  6. 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.

    Send 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.

  1. 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.

  2. 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.

  3. Siren

    Make a siren that slides up and down on its own while the logo is touched.

    Show a hint

    A for loop can count up through pitches: for freq in range(880, 1760, 16): then music.pitch(freq, 6). Then count back down.

The finished code

Show the whole program
main.pyMicroPython micro:bit editor
from microbit import *import music twinkle = ["C4:4", "C4:4", "G4:4", "G4:4", "A4:4", "A4:4", "G4:8",           "F4:4", "F4:4", "E4:4", "E4:4", "D4:4", "D4:4", "C4:8"] while True:    if button_a.was_pressed():        display.show(Image.MUSIC_QUAVERS)        music.play(twinkle)        display.clear()    elif button_b.is_pressed():        display.show(Image.MUSIC_CROTCHET)        music.pitch(accelerometer.get_x() + 1100, 50)    elif accelerometer.was_gesture("shake"):        display.show(Image.SURPRISED)        music.play(music.POWER_UP)    else:        display.clear()    sleep(10)
Notes for the teacher

Timing

  1. 10 minutes: step 1. Warn students about volume first.
  2. 20 minutes: steps 2 and 3. Letting them choose their own tune is a good stretch.
  3. 20 minutes: steps 4 to 6.

Practical points

  1. A room of micro:bit speakers is loud. Agree a signal for silence, and try one table at a time.
  2. A V1 micro:bit has no speaker: headphones with crocodile clips on pin 0 and GND work well.
  3. The simulator plays through the computer's speakers or headphones.

Mistakes to look out for

  1. A note written as "C4 4" or "C4;4" instead of "C4:4".
  2. music.play waits until the tune ends, so the buttons do nothing while it plays. That is expected, and a good talking point.
  3. A missing comma in the list of notes joins two notes together.