Deutsch   English   






how to connect a headphone or loudspeaker to the micro:bit to play sound sequences and short melodies.





To hear sounds from the micro:bit, you need a pair of headphones and 2 cables with crocodile clips. Connect the first cable (here with yellow color) to the GND pin and the rear of the headphone plug. The second cable (here with red color) goes from P0 to the front end of the headphone plug. In order to hear the sound in both earpieces, you must try to place the crocodile clip so that it touches the two metallic parts of the plug.






Play a sequence of tones

The command pitch(f, 500) plays a tone with frequency f during 500 milliseconds. To play several notes one after the other, you put the corresponding frequencies in a list and iterate it with a for loop.

You can find a table with tones of the musical scale and their corresponding frequencies in the overlay window:


from music import *

song = [262, 294, 330, 349, 392, 392, 392, 0, 440, 440, 440, 440, 392]
for f in song: 
   pitch(f, 500)
► Copy to clipboard


Play built-in melodies
Some melodies are already installed in TigerJython. You can find their names here or in the documentation. In the next program you select two melodies to play by pressing the A or B button. If you press button A, the melody JUMP_UP will be played, while pressing button B plays the melody JUMP_DOWN.


from microbit import *
from music import *

while True:
    if button_a.was_pressed():
    if button_b.was_pressed():
► Copy to clipboard


Play melodies in musical notation

The notation uses the following rules:

  • The twelve semitones of an octave are written as C, C#, D, D#, E, F, F#, G, G#, A, A#, H  (also with lowercase letters). For a break you use the note "R"
  • To change the octave, you write an octave number behind the note, so C2 for the second octave.  The selected octave then applies to all subsequent notes until an octave number is again specified (default is the 4th octave)
  • Behind the note designator you may place a colon with an indication of the duration (in ticks). All subsequent notes will then be played with this duration until a new indication of duration follows.

To compose a melody, you write the notes in a list, for example in the following program for a major and subsequent minor chord.


from music import *

melody = ['e:4', 'f#', 'g#', 'r:2', 'e', 'f#', 'g:4']
► Copy to clipboard


An acoustic position measuring device

Measuring devices with acoustic outputs are quite popular. In this application you generate a sound signal whose pitch depends on the lateral inclination of the micro:bit. Starting from an initial frequency f0 = 1000, you compute the rising or falling tone frequencies for semitones of the well-tempered scale and play each tone for 100 milliseconds.

With the button B you can turn off the sound.


from music import *
from microbit import *

def beep(n):
    freq = int(f0 * r**n)
    pitch(int(r**n * f0), 100)

f0 = 1000
r = 2**(1/12)
while not button_b.was_pressed():
    n = int(accelerometer.get_x() / 100)
► Copy to clipboard





To play a sound you must connect a headphone (or a loudspeaker with a built-in amplifier) to the outputs labeled GND and P0. The command pitch(f, time).emits a tone of frequency f (in Hz) and duration time (in ms). With play(song) you can play whole melodies, either as a sequence of frequencies or in musical notation.







Write a program that plays the following sequence:

song = [262, 294, 330, 262, 262, 294, 330, 262, 330, 349, 392, 330, 349, 392]

Of course you can also compose your own tone sequences.


You want to interactively change the playback speed:
- If you click button A, the song should be played twice as fast
- If you click button B, the playback will be slower.

Note that in the pitch(f, time) command, the time parameter must be an integer.


You can have a melody played repeatedly by using play() with additional parameters, such as the song birthday

play(BIRTHDAY, wait = False, loop = True)

Because wait = False, the playback is done in the background and the program continues with the next command.

Write a program that plays the song BIRTHDAY endlessly until you click the button A. (Note: You can stop a sound output with the command stop().)


Play the following melody given in musical notation:

Professional hints:

Some sound frequencies:

Tone Frequency Tone Frequency    

h' 494 h'' 988    
a' 440 a'' 880    
g' 392 g'' 784    
f' 349 f'' 698    
e' 330 e'' 660    
d' 294 d'' 588    
c' 262 c'' 524 c''' 1048

Professional hints:

Predifined song lists:


Professional hints:

In the well-tempered tuning the octave (doubling of the frequency) is divided into 12 semitones with the same frequency ratio r. So the following applies: r12 = 2 or
r =

Professional hints:

Instead of headphones, you can also use a cheap, amplifier-powered speaker (such as a "hamburger box") (but not a simle loud speaker without an amplifier).