The micro:bit can output sound to a pair of headphones or a speaker by sending a signal on Pin0. 


Common ways to do this are by using a set of analogue headphones with a jack plug on the end or using an inexpensive piezo speaker


Connection


micro:bit connected to headphone jackmicro:bit connected to piezo speaker

Headphones/Audio jack

Connect Pin0 to the tip  and GND to the sleeve of the jack. If you have four rings on the headphones, choose the top and bottom rings.


Piezo speaker

Connect Pin0 to the red lead and GND to the black lead of the speaker.


        

Programming

MakeCode

Place a start melody block from the Music menu underneath the on start block to play a melody when the micro:bit is powered on or reset.


https://makecode.microbit.org/_agcJHgi4vTJb

 

Python 

Music

Import the music module and then use music.play() to play a melody when the micro:bit is powered on or reset.

import music
music.play(music.ENTERTAINER)


 Audio

It is also possible to play audio from sound files available to the micro:bit. Add the files attached to this article to the Python Editor filesystem. Copy this program into the editor then flash this program to the micro:bit.

import audio

# read the files from the microbit filesystem
def read_frame(f_list, frame):
    for file in f_list:
        ln = file.readinto(frame)
        while ln:
            yield frame
            ln = file.readinto(frame)

# craete a function to open and play each file in turn
def play_file(f):
    with open(f + "-01.raw", "rb") as file1, \
         open(f + "-02.raw", "rb") as file2, \
         open(f + "-03.raw", "rb") as file3, \
         open(f + "-04.raw", "rb") as file4:
        f_list = [file1, file2, file3, file4]
        audio.play(read_frame(f_list, frame), wait=True)


# Allocate memory outside the interrupt
frame = audio.AudioFrame()
ln = -1
file = 1

# play the files
play_file("robot")

Thanks to fizban for the example.


Speech

Micropython also contains a speech synthesiser to enable the micro:bit to talk and sing.

This program uses speech.say() and speech.pronounce() with some parameters as an example of what we can do with the speech module. Copy this program into the editor and then Download/Flash this program to the micro:bit:

from microbit import *
import speech

while True:
speech.say('I am a BBC microbit`)
        sleep(100)
        speech.pronounce('BIHDDIY BIHDDIY BIHDDIY  BIHDDIY', speed=60, pitch=255)
    sleep(100)


You might want to use a powered speaker for this as the speech module is quiet.


How the micro:bit produces sound via PWM

The micro:bit uses Pulse Width Modulation (PWM) as a way to simulate an analogue output on a digital pin. It sends a series of high speed on/off electronic pulses to a speaker which can convert this to physical vibrations to create sound waves. 

The variation in the length of the on pulse, the Duty Cycle creates an average voltage output. A 50% duty cycle (often called a square wave) sets an equal time for pulse on and off.



To convey the frequency of sound(Hz) that the digital signal is trying to reproduce we vary the time between the signal voltage being high or low.


To do this,  the period(ms) of the note needs to be found, which is the amount of time it takes for the wave to cycle once. This is done by taking the inverse of the frequency of the note:


Period(ms)=1/Frequency(Hz)


For example, The note A4 maps to 440hz so using our calculation:


1/440 = 2.727


Once we have the value of the period, to represent the pitch in PWM, we just hold the signal high for half the length of the period (1.13636ms), and low for the other half(1.13636ms)



The Analogue Pitch block in MakeCode lets us set this frequency for PWM on the desired pin.