← Micro:bit

Chapter 4

Generating Sound and Music

การสร้างเสียงออกลำโพง

  • The micro:bit V2 has a small speaker on the board. No extra wires are needed.
  • All the sound blocks live in the Music group: notes, melodies, tempo and ready-made sound effects.

4.1 Sound Blocks & Speaker Control คำสั่งที่เกี่ยวข้องกับการสร้างเสียง

  • Sound is air that vibrates. A higher pitch means faster vibrations.
  • The processor makes an on/off signal (PWM) and the speaker shakes in time with it.
Music block play tone, melody… Processor makes a PWM signal (fast on / off) Built-in speaker ON by default (V2) Pin P0 headphones / buzzer
Where sound comes out: the processor sends one signal to the built-in speaker and to pin P0 at the same time.

Speaker on or off, and which pin

Default speaker ON audio pin P0 Speaker plays. P0 plays too. Speaker OFF set built-in speaker off Board is silent. Only P0 makes sound. Other pin set audio pin P1 The sound moves to the pin you choose.
Two settings decide where you hear the sound.

The five blocks บล็อกคำสั่งเสียง

MakeCode Block What it does
play melody [notes] at tempo [120] (bpm)
  • Plays a row of notes you pick in the 8×8 Editor grid.
  • The Gallery has ready tunes: Scale, Reverse, Mystery, Discovery, Falling, Hopeful.
set built-in speaker to [ON/OFF]
  • V2 only. Turns the speaker on the board on or off.
  • If you never set it, it is ON.
play sound [giggle] until done
  • V2 only. Plays a synthesised sound effect (a mood sound).
  • Synchronous (blocking): the next block waits until the sound ends.
play sound [mysterious]
  • V2 only. Plays a ready-made melody effect.
  • Asynchronous (non-blocking): it plays in the background and the next block starts at once.
set audio pin [P0]
  • In the Pin group. Chooses the pin for an outside speaker.
  • The default is P0.

The Melody Editor grid

time → C5BAGFEDC
In the Melody Editor each column is one step in time and each row is one pitch, from middle C (bottom) to high C (top). This is the scale.
  • Click a square to place a note. Click it again to remove it.
  • Notes are coloured by pitch, so you can see the tune go up and down.

Next: Controlling Sound with Switches →

4.1 Controlling Sound with Switches ควบคุมการเล่นเสียงด้วยสวิตช์

  • The board has two push switches, button A and button B.
  • Here each button plays the same two sounds with play sound, in two different ways.
  • A waits for the first sound to finish. B runs them in the background, at once.
Button A: until done mysterious happy happy starts only when mysterious is done Button B: in the background mysterious happy both start at the same moment, so they collide (robot sound) time
The same two sounds, two timelines. Until done: the next block waits. In the background: both start together. (Bar lengths are only illustrative.)

Step-by-Step Exercise

  1. Step 1 — Drag on button: take on button [A] pressed from the Input group.
  2. Step 2 — Duplicate: right-click it, choose Duplicate, and change the new one to button B.
  3. Step 3 — Add sounds to A: drag two play sound blocks from Music into button A and use the until done version for both:
    play sound mysterious until done
    play sound happy until done
  4. Step 4 — Add sounds to B: do the same in button B with the plain (background) version:
    play sound mysterious
    play sound happy

Behavior Comparison

  • Press A: the first sound finishes, then the second plays. You hear them one after the other.
  • Press B: both play at the same moment, like a synthetic robot voice.

Run it here: press A, then press B

  • Press A and listen. Then press B and compare.
  • A waits for each sound to finish. B starts both at once, so they collide into the robot noise.

💻 4.1 until done versus in the background — the same two sounds, two results

on button A pressed
play sound mysterious until done
play sound happy until done
on button B pressed
play sound mysterious
play sound happy
See it in JavaScript
input.onButtonPressed(Button.A, function () {
	music.play(music.builtinPlayableSoundEffect(soundExpression.mysterious), music.PlaybackMode.UntilDone)
	music.play(music.builtinPlayableSoundEffect(soundExpression.happy), music.PlaybackMode.UntilDone)
})
input.onButtonPressed(Button.B, function () {
	music.play(music.builtinPlayableSoundEffect(soundExpression.mysterious), music.PlaybackMode.InBackground)
	music.play(music.builtinPlayableSoundEffect(soundExpression.happy), music.PlaybackMode.InBackground)
})

Next: 4.2 Star Wars & Iconic Music Themes →

4.2 Multiple Sound Variations & Themes การสร้างเสียงหลายรูปแบบ

  • These programs play music when you press A, B, A+B, or shake the board.
  • They work on both micro:bit V1 and V2.

Before the tunes: notes and tempo โน้ตและจังหวะ

C 262 Hz D 294 Hz E 330 Hz F 349 Hz G 392 Hz A 440 Hz B 494 Hz C5 523 Hz C# D# F# G# A# MakeCode names: C = "Middle C", C5 = "High C"
Each note is a frequency in hertz (Hz, vibrations per second). Higher key, higher number. Black keys are the sharps.
At 120 bpm 1 beat = 500 ms 1/4 beat = 125 ms 1/8 1/4 note + 1/8 rest = 187.5 ms 0 ms 125 ms 250 ms 375 ms 500 ms Faster tempo = bigger bpm = shorter beat (240 bpm → 250 ms)
Tempo is beats per minute. At 120 bpm one beat is 60 000 ms ÷ 120 = 500 ms, and the other note lengths are fractions of that.
  • set tempo to (bpm) 120 changes the length of every beat after it.
  • rest is a silent gap with the same beat lengths.

1. Star Wars Opening Theme @ 120 BPM (button A)

  • Press A to play the Star Wars theme at 120 bpm.
  • The repeat 4 times block plays the whole tune 4 times.
G G G C G ¼ beat each, ⅛ rests 1 beat each 0 ms 500 ms 1000 ms 1500 ms outlined gaps = rests
The first notes of the theme on a timeline: three short G notes with tiny rests, then a long C and a long G (1 beat each).

💻 4.2 (1) Star Wars opening theme at 120 bpm — button A

on button A pressed
repeat 4 times
set tempo to (bpm) 120
play tone Middle G for 1/4 beat
rest (ms) 1/8 beat
play tone Middle G for 1/4 beat
rest (ms) 1/8 beat
play tone Middle G for 1/4 beat
rest (ms) 1/8 beat
play tone Middle C for 1 beat
play tone Middle G for 1 beat
play tone Middle F for 1/4 beat
rest (ms) 1/8 beat
play tone Middle E for 1/4 beat
rest (ms) 1/8 beat
play tone Middle D for 1/4 beat
play tone High C for 1 beat
play tone Middle G for 1 beat
play tone Middle F for 1/4 beat
rest (ms) 1/8 beat
play tone Middle E for 1/4 beat
rest (ms) 1/8 beat
play tone Middle D for 1/4 beat
play tone High C for 1 beat
play tone Middle G for 1 beat
play tone Middle F for 1/4 beat
rest (ms) 1/8 beat
play tone Middle E for 1/4 beat
rest (ms) 1/8 beat
play tone Middle F for 1/4 beat
rest (ms) 1/8 beat
play tone Middle D for 1 beat
play tone Middle D for 1 beat
See it in JavaScript
input.onButtonPressed(Button.A, function () {
	for (let i = 0; i < 4; i++) {
		music.setTempo(120)
		music.playTone(Note.G, music.beat(BeatFraction.Quarter))
		music.rest(music.beat(BeatFraction.Eighth))
		music.playTone(Note.G, music.beat(BeatFraction.Quarter))
		music.rest(music.beat(BeatFraction.Eighth))
		music.playTone(Note.G, music.beat(BeatFraction.Quarter))
		music.rest(music.beat(BeatFraction.Eighth))
		music.playTone(Note.C, music.beat(BeatFraction.Whole))
		music.playTone(Note.G, music.beat(BeatFraction.Whole))
		music.playTone(Note.F, music.beat(BeatFraction.Quarter))
		music.rest(music.beat(BeatFraction.Eighth))
		music.playTone(Note.E, music.beat(BeatFraction.Quarter))
		music.rest(music.beat(BeatFraction.Eighth))
		music.playTone(Note.D, music.beat(BeatFraction.Quarter))
		music.playTone(Note.C5, music.beat(BeatFraction.Whole))
		music.playTone(Note.G, music.beat(BeatFraction.Whole))
		music.playTone(Note.F, music.beat(BeatFraction.Quarter))
		music.rest(music.beat(BeatFraction.Eighth))
		music.playTone(Note.E, music.beat(BeatFraction.Quarter))
		music.rest(music.beat(BeatFraction.Eighth))
		music.playTone(Note.D, music.beat(BeatFraction.Quarter))
		music.playTone(Note.C5, music.beat(BeatFraction.Whole))
		music.playTone(Note.G, music.beat(BeatFraction.Whole))
		music.playTone(Note.F, music.beat(BeatFraction.Quarter))
		music.rest(music.beat(BeatFraction.Eighth))
		music.playTone(Note.E, music.beat(BeatFraction.Quarter))
		music.rest(music.beat(BeatFraction.Eighth))
		music.playTone(Note.F, music.beat(BeatFraction.Quarter))
		music.rest(music.beat(BeatFraction.Eighth))
		music.playTone(Note.D, music.beat(BeatFraction.Whole))
		music.playTone(Note.D, music.beat(BeatFraction.Whole))
	}
})

2. Wedding March (button B)

  • Press B to play one of the ready-made melodies, here wedding.
  • It plays once.
Press B
wedding melody

💻 4.2 (2) The wedding march from the melody list — button B

on button B pressed
start melody wedding repeating once
See it in JavaScript
input.onButtonPressed(Button.B, function () {
	music.startMelody(music.builtInMelody(Melodies.Wedding), MelodyOptions.Once)
})

3. Ascending & Descending Scales (A + B together)

  • Press A and B together to play play melody twice.
  • First the notes go from low to high, then from high to low.
high low C D E F G A B C5 B A G F E D C block 1: up block 2: down
Up the scale then back down: two play melody blocks, one after the other.

💻 4.2 (3) Up the scale and back down — buttons A + B together

on button A+B pressed
play melody C D E F G A B C5 at tempo 120 (bpm)
play melody C5 B A G F E D C at tempo 120 (bpm)
See it in JavaScript
input.onButtonPressed(Button.AB, function () {
	music.playMelody("C D E F G A B C5", 120)
	music.playMelody("C5 B A G F E D C", 120)
})

4. Pink Panther Theme (on shake)

  • Shake the board to play the Pink Panther theme at 120 bpm.
  • on shake is an event: the code inside waits until the board is shaken.
Waiting… on shake board shaken set tempo 120 beat = 500 ms play tone / rest in order, top to bottom tune ends, then wait for the next shake
Event flow: nothing plays until a shake happens; then the whole tune runs from the top and the program waits again.

💻 4.2 (4) The Pink Panther theme — shake the board

on shake
set tempo to (bpm) 120
play tone Middle D# for 1/4 beat
play tone Middle E for 1/4 beat
rest (ms) 1 beat
play tone Middle F# for 1/4 beat
play tone Middle G for 1/4 beat
rest (ms) 1 beat
play tone Middle D# for 1/4 beat
play tone Middle E for 1/4 beat
rest (ms) 1/8 beat
play tone Middle F# for 1/4 beat
play tone Middle G for 1/4 beat
rest (ms) 1/8 beat
play tone High C for 1/4 beat
play tone Middle B for 1/4 beat
rest (ms) 1/8 beat
play tone Middle D# for 1/4 beat
play tone Middle E for 1/4 beat
rest (ms) 1/8 beat
play tone Middle B for 1/4 beat
play tone Middle A# for 1 beat
rest (ms) 1 beat
play tone Middle A for 1/8 beat
play tone Middle G for 1/8 beat
play tone Middle E for 1/8 beat
play tone Middle D for 1/8 beat
play tone Middle E for 1 beat
See it in JavaScript
input.onGesture(Gesture.Shake, function () {
	music.setTempo(120)
	music.playTone(Note.DSharp, music.beat(BeatFraction.Quarter))
	music.playTone(Note.E, music.beat(BeatFraction.Quarter))
	music.rest(music.beat(BeatFraction.Whole))
	music.playTone(Note.FSharp, music.beat(BeatFraction.Quarter))
	music.playTone(Note.G, music.beat(BeatFraction.Quarter))
	music.rest(music.beat(BeatFraction.Whole))
	music.playTone(Note.DSharp, music.beat(BeatFraction.Quarter))
	music.playTone(Note.E, music.beat(BeatFraction.Quarter))
	music.rest(music.beat(BeatFraction.Eighth))
	music.playTone(Note.FSharp, music.beat(BeatFraction.Quarter))
	music.playTone(Note.G, music.beat(BeatFraction.Quarter))
	music.rest(music.beat(BeatFraction.Eighth))
	music.playTone(Note.C5, music.beat(BeatFraction.Quarter))
	music.playTone(Note.B, music.beat(BeatFraction.Quarter))
	music.rest(music.beat(BeatFraction.Eighth))
	music.playTone(Note.DSharp, music.beat(BeatFraction.Quarter))
	music.playTone(Note.E, music.beat(BeatFraction.Quarter))
	music.rest(music.beat(BeatFraction.Eighth))
	music.playTone(Note.B, music.beat(BeatFraction.Quarter))
	music.playTone(Note.ASharp, music.beat(BeatFraction.Whole))
	music.rest(music.beat(BeatFraction.Whole))
	music.playTone(Note.A, music.beat(BeatFraction.Sixteenth))
	music.playTone(Note.G, music.beat(BeatFraction.Sixteenth))
	music.playTone(Note.E, music.beat(BeatFraction.Sixteenth))
	music.playTone(Note.D, music.beat(BeatFraction.Sixteenth))
	music.playTone(Note.E, music.beat(BeatFraction.Whole))
})

Next: Interactive Audio Hardware Explorer →

Interactive micro:bit V2 Audio Hardware Map

Hover over or tap any audio part on the real micro:bit V2 photo to see what it does.

🔍

micro:bit V2 Onboard Speaker & Audio Subsystem

Interactive Map
micro:bit V2 Back View - Audio Hardware
👉

Hover or tap the Piezo Speaker, MEMS Mic, nRF52833 Synthesizer, or Pin P0 to inspect audio components.

Next: Test Your Knowledge with the Quiz →

Chapter 4 Review & Self-Check

Pitch frequency in Hz C 262 → C5 523 Tempo beats per minute 120 bpm = 500 ms Waiting until done: next waits background: no wait
Chapter 4 in one picture: pitch comes from frequency, timing from tempo, and the next block either waits or does not.

Check what you know about the built-in speaker, tempo, blocking vs non-blocking sounds and note scores.

1. Where is the built-in speaker located on the micro:bit V2 board?
2. What is the key functional difference between `play sound ... until done` and `play sound ...`?
3. How many notes and octaves are visualised in MakeCode’s interactive Melody Composer grid?
4. You connect headphones to pin P0 and want the onboard speaker silent. Which block does it?
5. Which gesture plays the Pink Panther theme in the Section 4.2 project?
6. What does `set tempo to (bpm) 120` change?
7. At 120 bpm, how long does one beat last?
8. Which block plays a ready-made tune such as the wedding march?