Skip to content

Sound

On this page we will learn

  • how to write notes for tunes
  • how to play built-in and custom tunes and tones
  • how to make the micro:bit speak and sing
  • how to measure sound with the microphone

Terminology

  • speaker – an output device that turns electrical signals into sound.
  • microphone – an input device that detects sound and measures how loud it is.
  • list – a collection of values, written in square brackets, that is stored in order under one name.
  • octave – a range of musical notes, numbered from 0 (very low) to 8 (very high), where octave 4 contains middle C.
  • duration – how long a note or tone lasts.
  • frequency – how many times a sound wave vibrates each second, which sets how high or low a tone sounds.
  • hertz – the unit of frequency (Hz), meaning one vibration per second.
  • speech synthesiser – software that creates spoken words from text, which makes the micro:bit's voice sound robotic.
  • phoneme – a single sound that makes up part of a spoken word, used to tell the micro:bit exactly how to say it.
  • pitch – how high or low a sound or voice is.

The micro:bit v2 has a built-in speaker that plays tunes, tones and robotic speech, and a microphone that measures how loud it is.

Possible uses:

  • game sound effects
  • alarms
  • talking projects
  • sound-activated lights

Connect it

The speaker and microphone are built into the micro:bit v2, so we don't need to connect anything.

  • Each example is a main.py file. See Your First Program for how to create, upload and run it.
  • The microphone is on the front of the board. The LED next to it lights up while the microphone is in use.

microphone location

Set it up

Volume and the microphone are part of the microbit library. Music and speech each need their own module:

1
2
3
from microbit import *
import music
import speech

Only import the modules your program uses.

Writing notes

Custom tunes are lists of notes written as strings: NOTE[octave][:duration]

  • note → C to B. Add # for sharp or b for flat. R is a rest (silence).
  • octave → 0 (very low) to 8 (very high). Octave 4 contains middle C.
  • duration → a higher number lasts longer. 4 lasts twice as long as 2.

For example, "A1:4" is note A, octave 1, duration 4.

Speech pitch

speech.sing() sets the pitch of each sound with # and a pitch number before the phoneme. The chart shows which pitch numbers match which notes.

speech-pitch

Methods

Method Parameters Returns Description
set_volume(level) level: 0–255 none Sets the speaker volume
music.play(music) music: built-in tune or list of notes none Plays a tune
music.pitch(frequency, duration) frequency: Hz
duration: ms
none Plays a tone at a frequency
speech.say(words, pitch=64, speed=72) words: English string none Speaks English words
speech.pronounce(phonemes) phonemes: phoneme string none Speaks exact sounds written as phonemes
speech.sing(phonemes) phonemes: phonemes with pitch numbers none Sings phonemes at set pitches
microphone.current_event() none SoundEvent SoundEvent.LOUD or SoundEvent.QUIET
microphone.sound_level() none int (0–255) How loud the sound is

set_volume()

Sets how loud the speaker is, from 0 (silent) to 255 (loudest).

from microbit import *
import music

# Setup
set_volume(50)

# Main loop
while True:
    music.play(music.BA_DING)
    sleep(1000)

PRIMM

  1. Predict what you think will happen. Be specific.
  2. Run the program.
  3. Time to investigate the code. What does each line do?
Code explanation
  • line 1 → imports all the commands from the microbit library.
  • line 2 → imports the music module.
  • line 5 → sets the volume to 50 (out of 255).
  • line 8 → starts an endless loop.
  • line 9 → plays the built-in BA_DING sound at the new volume.
  • line 10 → waits 1 second before the loop repeats.

music.play() — built-in tunes

Plays a tune through the speaker.

The micro:bit has many built-in tunes.

1
2
3
4
5
6
from microbit import *
import music

# Main loop
while True:
    music.play(music.NYAN)

PRIMM

  1. Predict what you think will happen. Be specific.
  2. Run the program.
  3. Time to investigate the code. What does each line do?
Code explanation
  • line 1 → imports all the commands from the microbit library.
  • line 2 → imports the music module.
  • line 5 → starts an endless loop.
  • line 6 → plays the built-in tune NYAN.

music.play() — custom tunes

music.play() can also play your own tune, written as a list of notes (see Writing notes).

1
2
3
4
5
6
7
8
9
from microbit import *
import music

# Setup
tune = ["C4:4", "D4:4", "E4:4", "C4:4"]

# Main loop
while True:
    music.play(tune)

PRIMM

  1. Predict what you think will happen. Be specific.
  2. Run the program.
  3. Time to investigate the code. What does each line do?
Code explanation
  • line 1 → imports all the commands from the microbit library.
  • line 2 → imports the music module.
  • line 5 → stores four notes, all in octave 4 with duration 4, in a list called tune.
  • line 8 → starts an endless loop.
  • line 9 → plays the notes in tune.

music.pitch()

Plays a tone at any frequency, not just musical notes, which is useful for sound effects. 440 Hz is the note A that musicians tune to.

1
2
3
4
5
6
7
from microbit import *
import music

# Main loop
while True:
    music.pitch(440, 500)
    sleep(500)

PRIMM

  1. Predict what you think will happen. Be specific.
  2. Run the program.
  3. Time to investigate the code. What does each line do?
Code explanation
  • line 1 → imports all the commands from the microbit library.
  • line 2 → imports the music module.
  • line 5 → starts an endless loop.
  • line 6 → plays a 440 Hz tone for 500 milliseconds.
  • line 7 → waits 500 milliseconds, so the tone beeps.

speech.say()

Speaks English words through the speaker.

The speech sounds robotic because it uses a simple speech synthesiser. You can change the voice with the pitch and speed parameters: a higher number gives a lower or slower voice.

1
2
3
4
5
6
7
from microbit import *
import speech

# Main loop
while True:
    speech.say("Hello world")
    sleep(1000)

PRIMM

  1. Predict what you think will happen. Be specific.
  2. Run the program.
  3. Time to investigate the code. What does each line do?
Code explanation
  • line 1 → imports all the commands from the microbit library.
  • line 2 → imports the speech module.
  • line 5 → starts an endless loop.
  • line 6 → says "Hello world".
  • line 7 → waits 1 second before the loop repeats.

speech.pronounce()

Speaks exact sounds, written as phonemes.

When say() doesn't sound right, write the word the way it sounds using phonemes. speech.translate() gives you a starting point to edit.

1
2
3
4
5
6
7
from microbit import *
import speech

# Main loop
while True:
    speech.pronounce("MAOREHTUN BEY5 BOYZ KAALIY4J")
    sleep(1000)

PRIMM

  1. Predict what you think will happen. Be specific.
  2. Run the program.
  3. Time to investigate the code. What does each line do?
Code explanation
  • line 1 → imports all the commands from the microbit library.
  • line 2 → imports the speech module.
  • line 5 → starts an endless loop.
  • line 6 → speaks the phonemes for "Moreton Bay Boys' College".
  • line 7 → waits 1 second before the loop repeats.

speech.sing()

Sings phonemes at set pitches.

Each note is a pitch number after #, followed by the sound to sing. To make a note last longer, repeat its vowel sounds, for example "DOWWWWWW".

1
2
3
4
5
6
7
from microbit import *
import speech

# Main loop
while True:
    speech.sing("#115DOWWWWWW#103REYYYYYY#94MIYYYYYY")
    sleep(1000)

PRIMM

  1. Predict what you think will happen. Be specific.
  2. Run the program.
  3. Time to investigate the code. What does each line do?
Code explanation
  • line 1 → imports all the commands from the microbit library.
  • line 2 → imports the speech module.
  • line 5 → starts an endless loop.
  • line 6 → sings three notes: "doh" at pitch 115, "re" at pitch 103 and "mi" at pitch 94.
  • line 7 → waits 1 second before the loop repeats.

microphone.current_event()

Returns SoundEvent.LOUD when the sound has just changed from quiet to loud, or SoundEvent.QUIET when it has just changed from loud to quiet.

1
2
3
4
5
6
7
8
9
from microbit import *

# Main loop
while True:
    if microphone.current_event() == SoundEvent.LOUD:
        display.show(Image.HEART)
        sleep(200)
    else:
        display.show(Image.HEART_SMALL)

PRIMM

  1. Predict what you think will happen. Be specific.
  2. Run the program.
  3. Time to investigate the code. What does each line do?
Code explanation
  • line 1 → imports all the commands from the microbit library.
  • line 4 → starts an endless loop.
  • line 5 → checks if the sound has just changed from quiet to loud.
  • line 6 → if it has, shows a big heart.
  • line 7 → keeps the big heart on for 200 milliseconds.
  • line 8 → if it hasn't…
  • line 9 → …shows a small heart.

microphone.sound_level()

Returns how loud the sound around the micro:bit is, from 0 (silent) to 255 (loud).

1
2
3
4
5
from microbit import *

# Main loop
while True:
    display.scroll(microphone.sound_level())

PRIMM

  1. Predict what you think will happen. Be specific.
  2. Run the program.
  3. Time to investigate the code. What does each line do?
Code explanation
  • line 1 → imports all the commands from the microbit library.
  • line 4 → starts an endless loop.
  • line 5 → gets the sound level and scrolls it across the display.

Documentation

Documentation is the official guide written by the people who made the code library, and we can use it to look up every method and its parameters, including ones that aren't covered on this page.

Exercises

PRIMM

Time to modify the code and see what happens.

Starter files are in the sound folder of your tutorial files. Solutions are on the Exercise Solutions page.

Exercise 1

Starter: sound/ex1_my_tune

Can you create a program that plays your own tune?

Exercise 2

Starter: sound/ex2_college_song

Can you create a program that sings the College Song?

Exercise 3

Starter: sound/ex3_sound_meter

Can you light up the display based on how loud the sound is, with more LEDs for louder sounds?