In this chapter, you will learn how to use the Music package of the MakeCode for micro:bit to build and play simple tunes. MakeCode allows you to build music by combining music tones, octaves, beats (duration), accidentals (flats and sharps), and so forth. You can also use the built-in melodies with your applications.
11-1. Connecting a Speaker to Pin 0
Problem
You want to connect a speaker to the micro:bit pin 0.
Solution
Connect one wire to pin0 and the other wire to ground pin. A speaker will work either way around.
- Once completed, your hardware setup should look like that shown in Figure 11-1 .
- In the Toolbox, click on the Music category. Then click and drag the ring tone (Hz) block over, and place it inside the on start block (Figure 11-2 ).
How It Works
By default, MakeCode expects the speaker to be connected through micro:bit’s pin 0 through the edge connector using alligator (crocodile) clips. A speaker has two wires: positive (usually red) and negative (usually black). Some speakers use different color codes for positive and negative leads. With some speakers, you must solder wires to the solder tabs before using them.
Pin 0 is the default pin used to generate music.
11-2. Connecting a Speaker to Other Pins
Problem
You want to connect a speaker to a micro:bit pin1.
Solution
Connect the positive lead of the speaker to the micro:bit pin 1.
Connect the negative lead of the speaker to the micro:bit pinging.
- Once completed, your hardware setup should look like that shown in Figure 11-3 .
In the Toolbox, click on the Pins category. Next, click and drag the analog set pitch pin block over, and place it inside the on start block. Then select P1 from the drop-down menu.
- In the Toolbox, click on the Music category. Then click and drag the ring tone (Hz) block over, and place it inside the on start block underneath the analog set pitch pin block (Figure 11-4 ).
How It Works
P0
P1
P2
P3
P4
P10
11-3. Using Earphones
Problem
You want to connect an earphone with the micro:bit to listen to music.
Solution
Take two alligator (crocodile) leads (black and red).
Connect one end of the black alligator lead to the micro:bit GND and the other end to the base of your earphone jack.
Connect one end of the red crocodile lead to the micro:bit pin 0 and the other end to the tip of the earphone jack.
How It Works
If you don’t have a speaker, you can still use your micro:bit with earphones. Earphones produce quiet music and are better for testing purposes. If you don’t have alligator leads, just cut off the earphone jack and connect the leads to the edge connector (tip to pin 0 and base to GND) of the micro:bit. However, pre-built audio cables are available to quickly connect earphone or headphones to the micro:bit. You can simply plug the earphone jack to the 3.5 mm socket of the audio cable and connect two crocodile clips to the edge connector of the micro:bit.
11-4. Using Amplifiers
Problem
You want to play tunes loudly with a micro:bit .
Solution
Wiring Between MonkMakes Speaker and micro:bit
Speaker | Micro:bit |
---|---|
IN | Pin 0 |
3V | 3V |
GND | GND |
How It Works
Some vendors offer speakers with a built-in amplifier to make louder music. MonkMakes ( https://www.monkmakes.com/ ) manufactures a speaker breakout module with a built-in amplifier to produce loud music. It also has a built-in LED to indicate power. The MonkMakes speaker module uses three wires for connectivity and draws additional power from the micro:bit’s 3V pin.
11-5. Playing Built-In Melodies
Problem
You want to play a built-in melody .
Solution
- In the Toolbox, click on the Music category. Next, click and drag the start melody block over, and place it inside the on start block. Then choose birthday from the drop-down menu (Figure 11-7 ).
How It Works
dadadadum
entertainer
prelude
ode
nyan
ringtone
funk
blues
birthday
wedding
funeral
punchline
python
baddy
chase
ba ding
wawawawaa
jump up
jump down
power up
power down
once - plays the melody in the foreground one time.
forever - plays the melody in the foreground and keeps repeating it.
once in background - plays the melody in the background one time.
forever in background - plays the melody in the background and keeps repeating it.
11-6. Playing a Tone or Note
Problem
You want to play the note Middle C when button A is pressed.
Solution
In the Toolbox, click on the Input category. Then click on the on button A pressed event block.
- In the Toolbox, click on the Music category. Then click and drag the ring tone (Hz) and place it inside the on button A pressed event block (Figure 11-8 ).
When you play a note or tone inside the forever block, you will hear crappy sound.
How It Works
All notes are tones but not all tones are notes. In other words, notes are taken from the frequency range (20 Hz to 20 kHz) that humans can hear.
Notes and Their Frequencies
Note | Frequency (Hz) |
---|---|
Low C | 131 |
Low C# | 139 |
Low D | 147 |
Low D# | 156 |
Low E | 165 |
Low F | 175 |
Low F# | 185 |
Low G | 196 |
Low G# | 208 |
Low A | 220 |
Low A# | 233 |
Low B | 247 |
Middle C | 262 |
Middle C# | 277 |
Middle D | 294 |
Middle D# | 311 |
Middle E | 330 |
Middle F | 349 |
Middle F# | 370 |
Middle G | 392 |
Middle G# | 415 |
Middle A | 440 |
Middle A# | 466 |
Middle B | 494 |
High C | 523 |
High C# | 554 |
High D | 587 |
High D# | 622 |
High E | 659 |
High F | 698 |
High F# | 740 |
High G | 784 |
High G# | 831 |
High A | 880 |
High A# | 932 |
High B | 988 |
In music, sharp (#) means higher in pitch. More specifically, in musical notation, sharp means "higher in pitch by one semitone (half step)." Sharp is the opposite of flat, which is a lowering of pitch. As an example, the Middle C# resides halfway between Middle C (262 Hz) and Middle D (294 Hz).
Musical notes can have flats and sharps known as accidentals. A flat can be written as b (lowercase), and a sharp can be written as # (hash).
C# - C Sharp
Ab - A Flat
Cb - C Flat
11-7. Using Octaves
Problem
You want to play the musical note C in octave 3 .
Solution
In the Toolbox, click on the Arrays category. Next, click and drag the set text list to block over, and place it inside the on start block. Then replace each text box with c, c4, and c3, respectively.
In the Toolbox, click on the Music category. Then click and drag the start melody block over, and place it underneath the set text list to block.
- In the Toolbox, click on the Variables category. Then click and drag the text list variable over, and place it on the melody list of the start melody block (Figure 11-15 ).
How It Works
An octave has seven musical notes (C, D, E, F, G, A, B). A musical note can present with its octave (octave number) to indicate the position of the key on the keyboard. You can write a musical note with its octave as follows.
NOTE[octave]
As an example, the musical note C in octave 3 can be written as C3.
By default, micro:bit plays musical notes in octave 4 unless you explicitly mention it. As an example, the musical note C is equivalent to C4.
11-8. Playing a Note or Tone for Given Duration
Problem
You want to play a note for 4 beats.
Solution
- In the Toolbox, click on the Music category. Next, click and drag the play tone for block over, and place it inside the on start block. Then select 4 from the drop-down menu (Figure 11-17 ).
How It Works
1
1/2
1/4
1/8
1/16
2
4
By default, the duration of a beat is 500 milliseconds.
When you run the above code, the Middle C will play for 2 seconds.
The duration of a beat specifies the arbitrary length of time defined by tempo.
- Write the musical note followed by a colon followed by the number of beats. Figure 11-18 shows an example to play three musical notes sequentially. Here are the musical notes you can hear.
C4#:3 - Plays the note C Sharp in octave 4 for 3 beats. If the duration of a beat is 500 milliseconds, the C4#:3 will play for 1.5 seconds.
Ab:4 - Plays the note A Flat IN octave 4 for 4 beats. If the duration of a beat is 500 milliseconds, the Ab:4 will play for 2 seconds.
D3b:2 - Plays the note D Flat in octave 3 for 2 beats. If the duration of a beat is 500 milliseconds, the D3b:2 will play for 1 second.
- Use time as milliseconds instead of beat. Figure 11-19 shows an example to set 400 milliseconds duration for the musical note Middle C. Originally, the play tone block presents the duration in beats with a drop-down box. To type the value 400, first you should replace it with a value box. You can get an empty value box from the Math category.
11-9. Setting the Tempo
Problem
You want to set the tempo to 400 for your music.
Solution
In the Toolbox, click on the Music category. Next, click and drag the set tempo to (bpm) block over, and place it inside the on start block. Then type 400 in the value box. Alternatively, you can use the slider to change the value.
- In the Toolbox, click on the Music category again. Then click and drag the play tone for block over, and place it inside the on start block underneath the set tempo to (bpm) block (Figure 11-20 ).
How It Works
Tempo defines the speed of a piece of music. In your code, the default amount of the tempo is 120. Tempo can be expressed in bpm (beats per minute). You can set the tempo for the music using the set tempo to (bpm) block. You can type any positive value for the tempo, but MakeCode recommends 4 to 400.
When you change the tempo, the duration of a beat gets changed accordingly. By default, the duration of a beat is 500 milliseconds for the tempo, 120. You can calculate the duration of a beat in milliseconds for a given tempo as follows.
Example: Calculate the duration of a beat for tempo 120.
= 60,000 milliseconds / 120
= 500 milliseconds
= 60,000 milliseconds / 400
= 150 milliseconds
The larger the tempo value, the faster the notes (tunes) will play.
11-10. Getting the Tempo
Problem
You want to get the current tempo in beats per minute.
Solution
In the Toolbox, click on the Basic category. Then click and drag the show number block over, and place it inside the on start block.
- In the Toolbox, click on the Music category. Then click and drag the tempo (bpm) block over, and place it inside the placeholder of the show number block (Figure 11-22 ).
How It Works
The tempo (bpm) block returns the tempo in beats per minute.
11-11. Getting the Duration of a Beat
Problem
You want to get the duration of a beat in milliseconds.
Solution
In the Toolbox, click on the Basic category. Then click and drag the show number block over, and place it inside the on start block.
- In the Toolbox, click on the Music category. Then click and drag the beat block over, and place it inside the placeholder of the show number block (Figure 11-23 ).
How It Works
By default, the beat block returns the duration of a beat in milliseconds. It also returns the duration of 1/2, 1/4, 1/8, 1/16, 2, and 4 beats.
11-12. Using Music Events
Problem
You want to display a happy icon once the happy birthday melody has ended.
Solution
In the Toolbox, click on the Music category. Next, click and drag the start melody block over, and place it inside the on start block. Then choose, birthday from the drop-down menu.
In the Toolbox, click on the Music category. Next, click and drag the music on event block . Then choose melody ended from the drop-down menu.
- In the Toolbox, click on the Basic category. Next, click and drag the show icon block over, and place it inside the music on block. Then choose happy from the drop-down menu (Figure 11-24 ).
How It Works
melody note played
melody started
melody ended
melody repeated
background melody note played
background melody started
background melody ended
background melody repeated
background melody paused
background melody resumed
11-13. Adding Silence Between Notes and Tones
Problem
You want to add 2 seconds of silence between two notes.
Solution
In the Toolbox, click on the Music category. Then click and drag the play tone for block over, and place it inside the on start block.
In the Toolbox, click on the Music category. Next, click and drag the reset (ms) block over, and place it inside the on start block underneath the play tone for block. Then choose 4 for the beat from the drop-down menu.
- Right-click on the play tone for block, and from the shortcut menu, click Duplicate. Next, click and drag the duplicated play tone for block and place it underneath the reset (ms) block . Then select the tone (note) Middle D from the visual piano keyboard (Figure 11-25 ).