TigerJython4Kids | robotics |
Deutsch English |
YOU LEARN HERE... |
how to connect a headphone or loudspeaker to the micro:bit to play sound sequences and short melodies. |
SOUND OUTPUT |
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. |
EXAMPLES |
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:Program: from music import * song = [262, 294, 330, 349, 392, 392, 392, 0, 440, 440, 440, 440, 392] for f in song: pitch(f, 500)
Play built-in melodies Program: from microbit import * from music import * while True: if button_a.was_pressed(): play(JUMP_UP) if button_b.was_pressed(): play(JUMP_DOWN) sleep(10)
Play melodies in musical notation The notation uses the following rules:
To compose a melody, you write the notes in a list, for example in the following program for a major and subsequent minor chord. Program: from music import * melody = ['e:4', 'f#', 'g#', 'r:2', 'e', 'f#', 'g:4'] play(melody)
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. Program: from music import * from microbit import * def beep(n): freq = int(f0 * r**n) pitch(int(r**n * f0), 100) sleep(50) f0 = 1000 r = 2**(1/12) while not button_b.was_pressed(): n = int(accelerometer.get_x() / 100) beep(n) sleep(10) |
MEMO |
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. |
EXERCISES |
|
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 |
Predifined song lists:
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 = |
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).