Aegidius
 Plüss   Aplulogo
   
serverstatus
 www.aplu.ch
 www.java-online.eu
     Print Text 
© 2018, V10.0
 
  MicroBit Remote Development
 
Sound / Music
 

The source code of all examples can be downloaded from a link in the right side bar.

Connect your headphones directly to GND and one if the freely available output ports, e.g. P0.

Caution:
It is not safe to connect a loudspeaker directly to the micro:bit because of the high current drain and possible inductive voltage overshoots. If you do so, it this is at your own risk. Better invest some money into a mini USB speaker powered by a battery, an extra USB jack or a 230V power supply. If you know some basics of electronics you may also build a cheap sound system with a loudspeaker driven by a single transistor amplifier. Connect the GND and both channels (Left/Right) to the same pin (e.g. P0) using crocodile cables.

Cheap audio amplifier kits may also be purchased from several sources, among others Kitronik.

 

green

Example 1: Play a chromatic scale

 

Aim:
By importing the music module you get access to some nice functions to play tones. Use the function pitch(freq, duration) to play one octave of the chromatic scale starting from the standard pitch (kammerton) f0 = 440 Hz.

Program:

# ChromaticScale.py

from music import *

f0 = 440
r = 2**(1/12)
for n in range(13): 
   pitch(int(r**n * f0), 500)

Remarks:
Since an octave corresponds to doubling the frequency and contains 12 half-tone steps with equal multiplier r, the half-tone multiplier r is the twelfth root of 2.


 

green

Example 2: An accoustical spirit-level

 

Aim:
Often a sound signal is more adequate as human feedback from a measurement than a visual indication. Produce a sound signal whose pitch depends on the tilt angle of the micro:bit.

Program:

# TiltDetector.py

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():
    v = int(accelerometer.get_x() / 100)
    beep(v)

Remarks:
Here the chromatic scale is used, but another less music-based acceleration-to-pitch conversion may give better results. Try it!

 

 

green

Example 3: Compose a melody

 

A melody is played be creating a melody list or tuple were the pitch and the duration of each tone is are defined by consecutive strings. The pitch in musical notation and the duration in number of ticks. If no ticks is given, the value 1 is assumed. A typical examples is the following melody tuple that is predefined as variable music.ENTERTAINER:

('d4:1', 'd#', 'e', 'c5:2', 'e4:1', 'c5:2', 'e4:1', 'c5:3', 'c:1', 'd', 'd#', 'e', 'c', 'd', 'e:2', 'b4:1', 'd5:2', 'c:4')

(The case of musical notes does not matter.)

Aim:
Play the ENTERTAINER melody each time you press the button A. When button B is pressed the program finishes.

Program:

# Entertainer.py

from microbit import *
from music import *

print(ENTERTAINER)
while not button_b.is_pressed():
    play(ENTERTAINER)
    while not button_a.is_pressed() and not button_b.is_pressed():
        sleep(10)

Remarks:

button_a.is_pressed() and button_b.is_pressed() returns True as long as one of the button is pressed and hold down. Try to understand why button_b.was_pressed() could not replace button_b.is_pressed() in this example. The speed of the melody can be adapted by the set_tempo() function. For more information consult the microbit API (link in the right side bar).