No related product information!

Lesson 15 Make a Music Player

2005

    In this lesson, we will carry out an interesting experiment to make a music player with a Micro:bit.

15.1 Components to be prepared

image.png

15.2 LED display

The front LED display of Micro:bit consists of 25 5x5 lattice LEDs.

25 LEDs arranged in a 5x5 grid make up the display for showing pictures, words and numbers.

image.png

15.3 How it works

Your BBC micro:bit can be programmed to make a wide variety of sounds - from single notes, tones and beats to your own musical compositions.

1.To make sounds with micro:bit, attach headphones or a speaker. Clip the tip of the headphone plug to pin 0 on micro:bit. Clip the longer part of the headphone plug to the GND pin on micro:bit.

2.Use this program to make your micro:bit play one of its built-in tunes when you press button A.

3.The gold pins on the bottom of the micro:bit are used for inputs and outputs. Here we use pin 0 as an output. The micro:bit sends pulses of electrical signals from pin 0 when it plays the tune. The headphones must also be connected to the GND pin on the micro:bit to complete the electrical circuit.

More projects about music are available in the following link:

https://www.microbit.org/projects/make-it-code-it/?filters=8c2e3990-4b3b-4925-8972-9cdc60e49f10


15.4 Circuit

Connect micro:bit and PC with a Micro USB cable. Clip the tip of the headphone plug to pin 0 on micro:bit. Clip the longer part of the headphone plug to the GND pin on micro:bit.

wps35.jpg

 

The real object connection diagram is as follows:

wps34.jpg


15.5 MakeCode programming

  We will use an online MakeCode Editor to complete the experiment in this lesson, as shown below.

15.5.1 Start programming

(1) Log in to the website

1. You need to enter the URL in the address bar of Google Browser:

https://makecode.microbit.org/

2. After the website is successfully opened, the interface as shown below will appear:

image.png


(2) Import a project

1. In the HOME interface, click the "Import" button to import the external ".hex" file:

image.png


In the pop-up dialog box, select the "Import File", as shown in the following figure:

image.png


Click the "Choose File":

image.png


Find the code file for this lesson:

BBC_Microbit_Sensor\Code\Lesson_15\BlockCode

Select the file in ".hex" format and click the Open:

wps37.jpg


2. Notice whether the file has been loaded into the following window, and then click the "Go ahead!" button, as shown in the following figure:

wps38.jpg


3. Open the file successfully, as shown in the following figure:

wps39.jpg


    [The music score is shown below]:

wps40.jpg


15.5.2 Run the program

1. After the program is written, connect micro:bit and PC with a Micro USB cable.

2. After micro:bit is connected to the computer, you need to first "Pair device". Click the image.png button on the right of image.png in the lower left corner, and then click the image.png option, as shown in the following figure:

image.png


Then click image.png in the lower right corner:

image.png


Then the following dialog box will pop up, select image.png, and then click image.png

image.png


After the device is successfully paired, the image.png button changes to image.png

image.png


3.Then click the wps41.jpg button to directly download the program into the Micro:bit. After the download is finished, your Micro:bit will restart and run the program you just downloaded. Press the button A, connect your headphones to the Micro:bit and test whether the playing sound can be heard, as shown in the following figure:

wps42.jpg


[Note]:

If Micro:bit doesn't respond after clicking the image.png, you need to click the image.png button on the right of the image.png, and then click the image.png , and observe the situation of the Micro:bit again, as shown in the following figure:

image.png


 If you have problems, please send us an email: support@adeept.com


15.5.3 Learn the code program

In the program, we use the following instruction blocks, which are explained as follows:


Block

Function


wps46.jpg

 

This is an instruction block that can edit various musical tempos. You can adjust the tempos by pressing wps44.jpg.

wps45.jpg

 

This is an instruction block that repeats the loop, and the number indicates that the loop is repeated four times.

15.6 Python programming

15.6.1 Run the program

1.Connect micro:bit and PC with a Micro USB cable.

2. Open the Mu Editor installed on the computer, and click the button [Load] in the upper left corner to open the source code program of this lesson:

image.png 


Find the code file for this lesson:

BBC_Microbit_Sensor\Code\Lesson_15\PythonCode

Select the file in ".py" format and click the Open:

image.png

 

3. Click the [Flash] button to download the program to Micro:bit, as shown in the following figure:

wps48.jpg

 

  

4.After the program is downloaded, your Micro:bit will restart and run the program you just downloaded. Press the button A, connect your headphones to the Micro:bit and test whether the playing sound can be heard, as shown in the following figure:

wps49.jpg

 

15.6.2 How it works

Your BBC micro:bit can be programmed to make a wide variety of sounds - from single notes, tones and beats to your own musical compositions.

1.To make sounds with micro:bit, attach headphones or a speaker. Clip the tip of the headphone plug to pin 0 on micro:bit. Clip the longer part of the headphone plug to the GND pin on micro:bit.

2.Use this program to make your micro:bit play one of its built-in tunes when you press button A.

3.The gold pins on the bottom of the micro:bit are used for inputs and outputs. Here we use pin 0 as an output. The micro:bit sends pulses of electrical signals from pin 0 when it plays the tune. The headphones must also be connected to the GND pin on the micro:bit to complete the electrical circuit.

More projects about music are available in the following link:

https://www.microbit.org/projects/make-it-code-it/?filters=8c2e3990-4b3b-4925-8972-9cdc60e49f10


    If you have problems, please send us an email: support@adeept.com


15.6.3 Learn the code program

The source codes are as follows:


1

2

3

4

5

6

7

8

9

10

11

12

13

14

from microbit import *

import music

 

tune = ["E4:4", "E4:4", "E4:8", "E4:4", "E4:4", "E4:8", "E4:4", "G4:4", "C4:4", "D4:4", "E4:8", "F4:4", "F4:4", "F4:4", "F4:4", "F4:4", "E4:4", "E4:4", "E4:2", "E4:2", "E4:4", "D4:4", "D4:4", "E4:4", "D4:8", "G4:8", "E4:4", "E4:4", "E4:8", "E4:4", "E4:4", "E4:8", "E4:4", "G4:4", "C4:4", "D4:4", "E4:8", "F4:4", "F4:4", "F4:4", "F4:4", "F4:4", "E4:4", "E4:4", "E4:2", "E4:2", "G4:4", "G4:4", "F4:4", "D4:4", "C4:8"]

while True:

    if button_a.is_pressed():

        music.play(tune)


More information about the Musical Notation is available in the following link:

https://microbit-micropython.readthedocs.io/en/v1.0.1/music.html


  (1) Import a music library.


2

import music

 

   (2) When the button A is pressed, the music in the tune will be played.


13

14

if button_a.is_pressed():

    music.play(tune)

 

    (3) In the tune, the edited tempos are stored. For example, A1:4 refers to the note “A” in octave 1 that lasts for four ticks (a tick is an arbitrary length of time defined by a tempo setting function - see below). If the note name R is used then it is treated as a rest (silence).


4

5

6

7

8

9

10

11

tune = ["E4:4", "E4:4", "E4:8", "E4:4", "E4:4", "E4:8", "E4:4", "G4:4", "C4:4", "D4:4", "E4:8", "F4:4", "F4:4", "F4:4", "F4:4", "F4:4", "E4:4", "E4:4", "E4:2", "E4:2", "E4:4", "D4:4", "D4:4", "E4:4", "D4:8", "G4:8", "E4:4", "E4:4", "E4:8", "E4:4", "E4:4", "E4:8", "E4:4", "G4:4", "C4:4", "D4:4", "E4:8", "F4:4", "F4:4", "F4:4", "F4:4", "F4:4", "E4:4", "E4:4", "E4:2", "E4:2", "G4:4", "G4:4", "F4:4", "D4:4", "C4:8"]