Music

This is a quick guide to some of the things you can do with micro:bit music. The idea is that you can use this information to experiment and create something for yourselves. You can use the micro:bit to play simple tunes, provided that you connect a speaker to your board, or for V2, by using the built-in speaker.

If you are using a speaker, you can connect your micro:bit using crocodile clips like this:

Warning

You cannot control the volume of the sound level from the micro:bit. Please be very careful if you are using headphones. A speaker is a better choice for work with sound.

If you are using headphones you can use crocodile clips to connect your micro:bit to some headphones like this:

../_images/connect_headphones.jpg

If you are using the new micro:bit V2, there is built-in speaker at the back, so no need to connect to an external speaker/headphone.

../_images/microbit-speaker-v2.png

If you are using an external speaker connected to pin0, you can turned off the built-in speaker by adding this line:

>>> microbit.speaker.off()

To turn on the built-in speaker:

>>> microbit.speaker.on()

Basic Functions

Play a tune

Let’s play some music:

.. code-block:: python

from microbit import * import music

music.play(music.NYAN)

Note

You must import the music module to play and control sound.

MicroPython has quite a lot of built-in melodies. Here’s some of them, try them out:

  • music.``DADADADUM``
  • music.``WEDDING``
  • music.``BIRTHDAY``
  • music.``ODE``
  • music.``NYAN``
  • music.``RINGTONE``

Make your own tune

You can write your own tune, here is a snippet of code showing how to play a sound. The number after the note is the octave and an octave can be a number from 1 to 8. The number after the colon says how long the note will last:

from microbit import *
import music

# Play a 'C'
music.play('C')

# Play a 'C' for 4 beats long
music.play('C:4')

# Play a 'C' in octave number 3 for 4 beats long
music.play('C3:4')

Playing a series of notes one after the other is easy, you just put the notes you want to play in a list:

from microbit import *
import music

# Tune: Mary Had A Little Lamb
tune = ["E:4", "D:4", "C:4", "D:4","E:4","E:4","E:4",
    "D:4","D:4","D:4","E:4","G:4","G:4","R:4",
    "E:4", "D:4", "C:4", "D:4","E:4","E:4","E:4",
    "E:4","D:4","D:4","E:4","D:4", "C:4","R:4",]
music.play(tune)

Notice how the octave and duration values only change when they have to. It’s a lot less typing and simpler to read.

For the new micro:bit V2, you can use the built-in sounds using audio.play()

>>> audio.play(Sound.GIGGLE)
play(source, wait=True, pin=pin0, return_pin=None)

Play the source to completion.

Parameters:
  • source

    Sound: The microbit module contains a list of built-in sounds that your can pass to audio.play().

    AudioFrame: The source agrument can also be an iterable of AudioFrame elements as described below.

  • wait – If wait is True, this function will block until the source is exhausted.
  • pin – An optional argument to specify the output pin can be used to override the default of pin0. If we do not want any sound to play we can use pin=None.
  • return_pin – specifies a differential edge connector pin to connect to an external speaker instead of ground. This is ignored for the V2 revision.
is_playing()
Returns:True if audio is playing, otherwise returns False.
stop()

Stops all audio playback.

Note

audio function is only for the new micro:bit V2.

Other built-in sounds:
  • Sound.``GIGGLE``
  • Sound.``HAPPY``
  • Sound.``HELLO``
  • Sound.``MYSTERIOUS``
  • Sound.``SAD``
  • Sound.``SLIDE``
  • Sound.``SOARING``
  • Sound.``SPRING``
  • Sound.``TWINKLE``
  • Sound.``YAWN``

Advanced Functions

You can also specify the note you want to play as a frequency. Take a look at this example where we make a police siren. The clever thing here is that the frequency or note is controlled by a for loop:

while True:
        for freq in range(880, 1760, 16):
                music.pitch(freq, 6)
        for freq in range(1760, 880, -16):
                music.pitch(freq, 6)

Can you guess what this does? Each time around the loop a new frequency is calculated by adding (or subtracting) 16.

Exercises

  1. Make up your own tune and play using the speaker.
  2. Make a musical instrument. Change the pitch of the sound played based on the readings from the accelerometer.