Creating a Music Box with micro:bit

Click on the “Mushroom Cloud Creation” above to follow us!

The Story of Mike

“Twinkle, twinkle, little star, how I wonder what you are…” Mike’s favorite class at school is music! On this day, after finishing music class, he learned the song “Twinkle, Twinkle, Little Star”. When he returned home, he suddenly remembered what Teacher Xiaomi said about how programming could make the micro:bit board play beautiful music. Is that true? Mike began to study seriously and wanted to make the micro:bit board sing “Twinkle, Twinkle, Little Star”.

Creating a Music Box with micro:bit

Required Components

Creating a Music Box with micro:bit

1× micro:bit main control board

Creating a Music Box with micro:bit

1× micro:bit expansion board

1× motion sensor module

Creating a Music Box with micro:bit

1× ordinary headphones

Creating a Music Box with micro:bit
Creating a Music Box with micro:bit

1× MicroUSB connection cable

Task 1: Electronic Music Box

Function:Make the micro:bit main board play music.

Detailed Steps

STEP1: Hardware Connection

Connect the micro:bit main board to the computer using a data cable and plug the headphones into the headphone jack;

Connect the motion sensor module to the P1 interface of the expansion board.

Note: The headphone jack for audio playback occupies the P0 port, so the P0 port cannot be used while playing sound.

Creating a Music Box with micro:bit

STEP2: Write the Program

1. Drag the “play sound on P0 until done” block from the micro:bit module collection to the script area.

Creating a Music Box with micro:bit

There are many built-in melodies to choose from in the play sound block. Here we choose “dadadadum”.

Creating a Music Box with micro:bit

2. Write the program, the final reference program is as follows:

Creating a Music Box with micro:bit

3. Try rotating the “small knob” right above the headphone jack on the expansion board to adjust the volume of the sound played by the micro:bit board.

Creating a Music Box with micro:bit

Task 2: Intelligent Music Box

Function: When someone approaches, make the micro:bit main board play a piece of music composed by itself; when no one is near, do not play music.

Write the Program

STEP1: Write a piece of music for “Twinkle, Twinkle, Little Star”. First, try to write the code that can play the first line of the song. Let’s take a look at the sheet music for this song; the pitch and notes vary, and the required sound frequencies are different.

Creating a Music Box with micro:bit

Taking the middle C as an example, the sound frequencies corresponding to the notes are shown in the table below (the number after the letter indicates the pitch of the note, e.g., C4 represents middle C, while C#2 indicates the sharp of C2).

Creating a Music Box with micro:bit

STEP2: Drag the “play note-C2-1 beat” block from the micro:bit module collection to the script area. This block can play different notes by adjusting the parameters (C2) and can adjust the beat by changing the parameter (1). (C2 refers to playing in bass, and you can change it to C4, G4, A4 for middle voice).

Creating a Music Box with micro:bit

STEP3: After composing the tune for “Twinkle, Twinkle, Little Star”, our micro:bit has learned to sing! But how do we make it smart? At this point, we need to use the motion sensor. It’s quite simple; we just need to add a conditional statement we learned before to make the intelligent music box work! Let’s take a look at the code:

Creating a Music Box with micro:bit

——End of Text——
Creating a Music Box with micro:bit

Call for Contributions

Dear Friends:

Mind+ is back with an open attitude! To better understand your needs, we have added a custom electronic module service. We sincerely invite you to witness every growth of Mind+ with us. Leave a message to tell us what modules you want and their uses, and we will prioritize arranging custom electronic modules for you in the Mind+ software.

Participate in the activity now:

http://mc.dfrobot.com.cn/forum.php?mod=viewthread&tid=37485

Creating a Music Box with micro:bit

Participate in the activity for a chance to win a signed limited edition micro:bit Go kit + the smallest micro:bit mate expansion board!

Creating a Music Box with micro:bit

Recommended Reading:

Creating with micro:bit Part 06— Electronic Candle

Creating with micro:bit Kit Part 05— Variable Speed Fan

Creating with micro:bit Kit Part 04— Hand-Controlled Signal Light

Creating with micro:bit Part 03— Flashing LED

Creating with micro:bit Part 02— Homemade Emoji Package

Creating with micro:bit Kit Part 01— Introduction

Creating a Music Box with micro:bit

Reply in the background【mb】 to download micro:bit Chinese learning materials

Reply 【mc】 to get the official version of the makecode software download link

Reply 【mind】 to get the graphical programming software download link

Reply【2018】 to download the complete content of the new curriculum standard

Reply【IoT】 to view the OBLOQ tutorial for IoT learning module

Reply 【Books】 to see recommended materials for maker education

Reply 【Competition】 to view college self-recruitment recognition events

↓↓↓ Click “Read the original text” to participate in the activity immediately!

Leave a Comment

×