Project 9:Speaker

1. Description
Micro: Bit main board has an built-in speaker, which makes adding sound to the programs easier. It can also be programmed to produce all kinds of tones, like playing the song Ode to Joy.
2. Preparation
A. Attach the micro:bit main board to your computer via the USB cable
B. Open the offline version of Mu.
3. Test Code
Enter Mu software and open the file“Speaker.py”to import code. You can also input code in the editing window yourself.
(Note: All words and symbols must be written in English.)

from microbit import *
import audio
display.show(Image.MUSIC_QUAVER)
while True:
audio.play(Sound.GIGGLE)
sleep(1000)
audio.play(Sound.HAPPY)
sleep(1000)
audio.play(Sound.HELLO)
sleep(1000)
audio.play(Sound.YAWN)
sleep(1000)
Click“Check”to examine errors in the code. The program proves wrong if underlines and cursors are shown.

If the code is correct, connect the micro:bit to your computer and click“Flash”to download the code to the micro:bit board.

4. Test Result
After downloading the code to the board successfully, power on via micro USB cable or external power supply(turn the DIP switch to ON),and press the reset button on micro:bit.

The speaker utters sound and the LED dot matrix shows the logo of music.
5. Code Explanation
